Skip to content

Commit

Permalink
Added failed load detection
Browse files Browse the repository at this point in the history
  • Loading branch information
naueramant committed Jul 5, 2020
1 parent 7ab536c commit 1764b55
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ YAML configurable web dashboard viewer written in Go
- Cron jobs
- System commands
- Message flashing
- Detect failed page loads and retry

## About

Expand Down
17 changes: 17 additions & 0 deletions internal/browser/browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import (
"context"
"log"

"github.com/chromedp/cdproto/network"
"github.com/chromedp/cdproto/target"
"github.com/chromedp/chromedp"
"github.com/sirupsen/logrus"
)

type Browser struct {
Expand Down Expand Up @@ -39,6 +42,20 @@ func (b *Browser) NewTab() Tab {
Context: ctx,
}

chromedp.ListenTarget(ctx, func(ev interface{}) {
if e, ok := ev.(*network.EventLoadingFailed); ok {
if e.Type == network.ResourceTypeDocument {
logrus.Infof("Tab failed to load, reloading in %v seconds\n", FailedLoadReloadDelay.Seconds())

go t.delayedReload()
}
}

if _, ok := ev.(*target.EventTargetDestroyed); ok {
b.removeTab(t)
}
})

t.Close = func() {
b.removeTab(t)
close()
Expand Down
18 changes: 17 additions & 1 deletion internal/browser/tab.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,25 @@ package browser
import (
"context"
"encoding/base64"
"time"

"github.com/chromedp/cdproto/network"
"github.com/chromedp/cdproto/page"
"github.com/chromedp/cdproto/runtime"
"github.com/chromedp/chromedp"
)

var (
FailedLoadReloadDelay = time.Second * 10
)

type Tab struct {
Browser Browser
Context context.Context

Close func()

isReloading bool
}

type BasicAuthCredentials struct {
Expand All @@ -31,7 +38,7 @@ func (t *Tab) Reload() {
}

func (t *Tab) Navigate(url string) {
chromedp.Run(t.Context, chromedp.Navigate(url))
chromedp.Run(t.Context, network.Enable(), chromedp.Navigate(url))
}

func (t *Tab) NavigateWithBasicAuth(url string, creds BasicAuthCredentials) {
Expand Down Expand Up @@ -69,3 +76,12 @@ func (t *Tab) AddJS(script string) {
chromedp.EvaluateAsDevTools(script, &executed),
)
}

func (t *Tab) delayedReload() {
if !t.isReloading {
t.isReloading = true
time.Sleep(FailedLoadReloadDelay)
t.isReloading = false
t.Reload()
}
}

0 comments on commit 1764b55

Please sign in to comment.