From 05cd03c3d4cc0f330650f5cd3fd7ce9ed5590640 Mon Sep 17 00:00:00 2001 From: Randy Cahyana Date: Tue, 21 Feb 2017 09:10:41 +0700 Subject: [PATCH] Fixing NavigateBack and NavigateForward implementation. Removing entries loop to find current navigation entry, as GetNavigationHistory already returning index of current navigation history entry. --- nav.go | 26 ++++++-------------------- 1 file changed, 6 insertions(+), 20 deletions(-) diff --git a/nav.go b/nav.go index 7d5a07d..13f7c5a 100644 --- a/nav.go +++ b/nav.go @@ -47,18 +47,11 @@ func NavigateBack(ctxt context.Context, h cdp.Handler) error { return err } - var i int - for ; i < len(entries); i++ { - if entries[i].ID == cur { - break - } + if cur <= 0 || cur > int64(len(entries)-1) { + return errors.New("invalid navigation entry") } - if i == 0 { - return errors.New("already on oldest navigation entry") - } - - return page.NavigateToHistoryEntry(entries[i-1].ID).Do(ctxt, h) + return page.NavigateToHistoryEntry(entries[cur-1].ID).Do(ctxt, h) } // NavigateForward navigates the current frame forwards in its history. @@ -68,18 +61,11 @@ func NavigateForward(ctxt context.Context, h cdp.Handler) error { return err } - i := len(entries) - 1 - for ; i > 0; i-- { - if entries[i].ID == cur { - break - } + if cur < 0 || cur >= int64(len(entries)-1) { + return errors.New("invalid navigation entry") } - if i == len(entries)-1 { - return errors.New("already on newest navigation entry") - } - - return page.NavigateToHistoryEntry(entries[i+1].ID).Do(ctxt, h) + return page.NavigateToHistoryEntry(entries[cur+1].ID).Do(ctxt, h) } // Stop stops all navigation and pending resource retrieval.