Fixing NavigateBack and NavigateForward implementation.

Removing entries loop to find current navigation entry, as GetNavigationHistory already returning
index of current navigation history entry.
This commit is contained in:
Randy Cahyana 2017-02-21 09:10:41 +07:00
parent 4c16faa34d
commit 05cd03c3d4

26
nav.go
View File

@ -47,18 +47,11 @@ func NavigateBack(ctxt context.Context, h cdp.Handler) error {
return err return err
} }
var i int if cur <= 0 || cur > int64(len(entries)-1) {
for ; i < len(entries); i++ { return errors.New("invalid navigation entry")
if entries[i].ID == cur {
break
}
} }
if i == 0 { return page.NavigateToHistoryEntry(entries[cur-1].ID).Do(ctxt, h)
return errors.New("already on oldest navigation entry")
}
return page.NavigateToHistoryEntry(entries[i-1].ID).Do(ctxt, h)
} }
// NavigateForward navigates the current frame forwards in its history. // NavigateForward navigates the current frame forwards in its history.
@ -68,18 +61,11 @@ func NavigateForward(ctxt context.Context, h cdp.Handler) error {
return err return err
} }
i := len(entries) - 1 if cur < 0 || cur >= int64(len(entries)-1) {
for ; i > 0; i-- { return errors.New("invalid navigation entry")
if entries[i].ID == cur {
break
}
} }
if i == len(entries)-1 { return page.NavigateToHistoryEntry(entries[cur+1].ID).Do(ctxt, h)
return errors.New("already on newest navigation entry")
}
return page.NavigateToHistoryEntry(entries[i+1].ID).Do(ctxt, h)
} }
// Stop stops all navigation and pending resource retrieval. // Stop stops all navigation and pending resource retrieval.