From ad981199cbb0d3542550713f7658a1726c0c8b24 Mon Sep 17 00:00:00 2001 From: Jez Ng Date: Thu, 26 Jan 2012 09:31:14 -0500 Subject: Next / prev links should be visible. Also, links later in the document are more likely to be the ones we want as they are further down the page, so favor the former. --- vimiumFrontend.js | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/vimiumFrontend.js b/vimiumFrontend.js index d70c2009..1a46a58a 100644 --- a/vimiumFrontend.js +++ b/vimiumFrontend.js @@ -877,11 +877,9 @@ function getLinkFromSelection() { // used by the findAndFollow* functions. function followLink(link) { - domUtils.simulateClick(link); - // blur the element just in case it already has focus. then when we re-focus it, the browser will scroll - // such that it is visible. - link.blur(); + link.scrollIntoView(); link.focus(); + domUtils.simulateClick(link); } /** @@ -896,8 +894,20 @@ function findAndFollowLink(linkStrings) { var shortestLinks = []; var shortestLinkLength = null; - for (var i = 0, count = links.snapshotLength; i < count; i++) { + // at the end of this loop, shortestLinks will be populated with a list of candidates + // links lower in the page are more likely to be the ones we want, so we loop through the snapshot backwards + for (var i = links.snapshotLength - 1; i >= 0; i--) { var link = links.snapshotItem(i); + + // ensure link is visible (we don't mind if it is scrolled offscreen) + var boundingClientRect = link.getBoundingClientRect(); + if (boundingClientRect.width == 0 || boundingClientRect.height == 0) + continue; + var computedStyle = window.getComputedStyle(link, null); + if (computedStyle.getPropertyValue('visibility') != 'visible' || + computedStyle.getPropertyValue('display') == 'none') + continue; + var linkMatches = false; for (var j = 0; j < linkStrings.length; j++) { if (link.innerText.toLowerCase().indexOf(linkStrings[j]) !== -1) { @@ -917,6 +927,7 @@ function findAndFollowLink(linkStrings) { } } + // try to get exact word matches first for (var i = 0; i < linkStrings.length; i++) for (var j = 0; j < shortestLinks.length; j++) { var exactWordRegex = new RegExp("\\b" + linkStrings[i] + "\\b", "i"); -- cgit v1.2.3