From 31ddc715c9cae4502e2e0b18aeea11ce9198bc47 Mon Sep 17 00:00:00 2001 From: ilya Date: Fri, 10 Sep 2010 22:20:21 -0700 Subject: Make 'gi' compatible with repetition, i.e. 5gi will focus the fifth input box on the page. --- README.markdown | 2 +- background_page.html | 7 +++++-- commands.js | 18 ++++++++++++------ test_harnesses/form.html | 4 +++- vimiumFrontend.js | 30 +++++++++++++++++++++++------- 5 files changed, 44 insertions(+), 17 deletions(-) diff --git a/README.markdown b/README.markdown index cba2bd8a..2e144110 100644 --- a/README.markdown +++ b/README.markdown @@ -83,7 +83,7 @@ Release Notes - In link hints mode, holding down the shift key will now toggle between opening in the current tab and opening in a new tab. - Two new commands (`zH` and `zL`) to scroll to the left and right edges of the page. -- A new command (`gi`) to focus the first text input box on the page. +- A new command (`gi`) to focus the first (or n-th) text input box on the page. - Bug fixes. 1.19 (06/29/2010) diff --git a/background_page.html b/background_page.html index 349212ef..94595b2b 100644 --- a/background_page.html +++ b/background_page.html @@ -507,8 +507,11 @@ if (!registryEntry.isBackgroundCommand) { var port = chrome.tabs.connect(tabId, { name: "executePageCommand" }); - port.postMessage({ command: registryEntry.command, count: count, - completionKeys: generateCompletionKeys("") }); + port.postMessage({ command: registryEntry.command, + count: count, + passCountToFunction: registryEntry.passCountToFunction, + completionKeys: generateCompletionKeys("") + }); refreshedCompletionKeys = true; } else { diff --git a/commands.js b/commands.js index fa24dee8..a9fa0aba 100644 --- a/commands.js +++ b/commands.js @@ -1,14 +1,17 @@ var availableCommands = {}; var keyToCommandRegistry = {}; -function addCommand(command, description, isBackgroundCommand) { +function addCommand(command, description, isBackgroundCommand, passCountToFunction) { if (availableCommands[command]) { console.log(command, "is already defined! Check commands.js for duplicates."); return; } - availableCommands[command] = { description: description, isBackgroundCommand: isBackgroundCommand }; + availableCommands[command] = { description: description, + isBackgroundCommand: isBackgroundCommand, + passCountToFunction: passCountToFunction + }; } function mapKeyToCommand(key, command) { @@ -18,7 +21,10 @@ function mapKeyToCommand(key, command) { return; } - keyToCommandRegistry[key] = { command: command, isBackgroundCommand: availableCommands[command].isBackgroundCommand }; + keyToCommandRegistry[key] = { command: command, + isBackgroundCommand: availableCommands[command].isBackgroundCommand, + passCountToFunction: availableCommands[command].passCountToFunction + }; } function unmapKey(key) { delete keyToCommandRegistry[key]; } @@ -102,7 +108,7 @@ function clearKeyMappingsAndSetDefaults() { mapKeyToCommand('zi', 'zoomIn'); mapKeyToCommand('zo', 'zoomOut'); - mapKeyToCommand('gi', 'focusFirstInput'); + mapKeyToCommand('gi', 'focusInput'); mapKeyToCommand('f', 'activateLinkHintsMode'); mapKeyToCommand('F', 'activateLinkHintsModeToOpenInNewTab'); @@ -146,7 +152,7 @@ addCommand('copyCurrentUrl', 'Copy the current URL to the clipboard'); addCommand('enterInsertMode', 'Enter insert mode'); -addCommand('focusFirstInput', 'Focus the first text box on the page'); +addCommand('focusInput', 'Focus the first (or n-th) text box on the page', false, true); addCommand('activateLinkHintsMode', 'Enter link hints mode to open links in current tab'); addCommand('activateLinkHintsModeToOpenInNewTab', 'Enter link hints mode to open links in new tab'); @@ -178,7 +184,7 @@ var commandGroups = { "scrollToTop", "scrollToBottom", "scrollToLeft", "scrollToRight", "scrollPageDown", "scrollPageUp", "scrollFullPageDown", "reload", "toggleViewSource", "zoomIn", "zoomOut", "copyCurrentUrl", "goUp", - "enterInsertMode", "focusFirstInput", + "enterInsertMode", "focusInput", "activateLinkHintsMode", "activateLinkHintsModeToOpenInNewTab", "enterFindMode", "performFind", "performBackwardsFind"], historyNavigation: diff --git a/test_harnesses/form.html b/test_harnesses/form.html index 52ac9308..740edb46 100644 --- a/test_harnesses/form.html +++ b/test_harnesses/form.html @@ -9,9 +9,11 @@

Text: + Text 2:

Search: + Search 2:

Radio:
@@ -32,4 +34,4 @@

- \ No newline at end of file + diff --git a/vimiumFrontend.js b/vimiumFrontend.js index d74c6d53..7677f96c 100644 --- a/vimiumFrontend.js +++ b/vimiumFrontend.js @@ -76,7 +76,11 @@ function initializePreDomReady() { if (port.name == "executePageCommand") { port.onMessage.addListener(function(args) { if (this[args.command]) { - for (var i = 0; i < args.count; i++) { this[args.command].call(); } + if (args.passCountToFunction) { + this[args.command].call(null, args.count); + } else { + for (var i = 0; i < args.count; i++) { this[args.command].call(); } + } } refreshCompletionKeys(args.completionKeys); @@ -198,13 +202,25 @@ function scrollFullPageDown() { window.scrollBy(0, window.innerHeight); } function scrollLeft() { window.scrollBy(-1 * settings["scrollStepSize"], 0); } function scrollRight() { window.scrollBy(settings["scrollStepSize"], 0); } -function focusFirstInput() { +function focusInput(count) { var xpath = '//input[@type="text" or @type="search"]'; - var result = document.evaluate(xpath, document.documentElement, null, - XPathResult.FIRST_ORDERED_NODE_TYPE, null); - if (!result.singleNodeValue) - return; - result.singleNodeValue.focus(); + var results = document.evaluate(xpath, document.documentElement, null, + XPathResult.ORDERED_NODE_ITERATOR_TYPE, null); + + var lastInputBox; + var i = 0; + + while (i < count) + { + i += 1; + + var currentInputBox = results.iterateNext(); + if (!currentInputBox) { break; } + + lastInputBox = currentInputBox; + } + + if (lastInputBox) { lastInputBox.focus(); } } function reload() { window.location.reload(); } -- cgit v1.2.3