From eeba8a63c120b39c48fa7d37e8d1a3c9afcb0270 Mon Sep 17 00:00:00 2001 From: jez Date: Thu, 19 May 2011 15:02:44 +0000 Subject: Focus links after having found them. Closes #132. Specifically, we focus a link when: 1. The user exits find mode with the link selected. 2. The user presses 'n' or 'N'. Also rename 'findModeQueryHasResults' to 'hasResults' to avoid confusion with the global variable of the same name. --- vimiumFrontend.js | 48 ++++++++++++++++++++++++++++++++++++------------ 1 file changed, 36 insertions(+), 12 deletions(-) (limited to 'vimiumFrontend.js') diff --git a/vimiumFrontend.js b/vimiumFrontend.js index b79abfde..ee19d90f 100644 --- a/vimiumFrontend.js +++ b/vimiumFrontend.js @@ -613,31 +613,54 @@ function performFindInPlace() { // backwards. window.scrollTo(cachedScrollX, cachedScrollY); - performFind(); + executeFind(); +} + +function executeFind(backwards) { + findModeQueryHasResults = window.find(findModeQuery, false, backwards, true, false, true, false); +} + +function focusFoundLink() { + if (findModeQueryHasResults) { + var link = getLinkFromSelection(); + if (link) link.focus(); + } +} + +function findAndFocus(backwards) { + executeFind(backwards); + focusFoundLink(); } function performFind() { - findModeQueryHasResults = window.find(findModeQuery, false, false, true, false, true, false); + findAndFocus(); } function performBackwardsFind() { - findModeQueryHasResults = window.find(findModeQuery, false, true, true, false, true, false); + findAndFocus(true); +} + +function getLinkFromSelection() { + var node = window.getSelection().anchorNode; + while (node.nodeName.toLowerCase() !== 'body') { + if (node.nodeName.toLowerCase() === 'a') return node; + node = node.parentNode; + } + return null; } function findAndFollowLink(linkStrings) { for (i = 0; i < linkStrings.length; i++) { - var findModeQueryHasResults = window.find(linkStrings[i], false, true, true, false, true, false); - if (findModeQueryHasResults) { - var node = window.getSelection().anchorNode; - while (node.nodeName.toLowerCase() != 'body') { - if (node.nodeName.toLowerCase() == 'a') { - window.location = node.href; - return true; - } - node = node.parentNode; + var hasResults = window.find(linkStrings[i], false, true, true, false, true, false); + if (hasResults) { + var link = getLinkFromSelection(); + if (link) { + window.location = link.href; + return true; } } } + return false; } function findAndFollowRel(value) { @@ -698,6 +721,7 @@ function enterFindMode() { function exitFindMode() { findMode = false; + focusFoundLink(); HUD.hide(); } -- cgit v1.2.3