From 324cf32c5bd5d7e7ce89a9aaa7497454a7dfdbb2 Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Sun, 17 Sep 2017 18:21:07 +0900 Subject: [PATCH] focus form items by following --- README.md | 2 +- src/content/follow.js | 19 ++++++------------- src/content/index.js | 31 ++++++++++++++++++++++++++----- 3 files changed, 33 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 28ff1a9..c5e2ffe 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,7 @@ Firefox by WebExtensions API. - [ ] hints - [x] open a link - [x] open a link in new tab - - [ ] activate input form + - [x] activate input form - [ ] misc - [ ] configurable keymaps - [ ] .rc file diff --git a/src/content/follow.js b/src/content/follow.js index 7e1cd01..f9cc380 100644 --- a/src/content/follow.js +++ b/src/content/follow.js @@ -125,20 +125,13 @@ export default class Follow { } static getTargetElements(doc) { - let all = doc.querySelectorAll('a'); - let filtered = Array.prototype.filter.call(all, (e) => { - return Follow.isVisibleElement(e); + let all = doc.querySelectorAll('a,button,input,textarea'); + let filtered = Array.prototype.filter.call(all, (element) => { + let style = window.getComputedStyle(element); + return style.display !== 'none' && + style.visibility !== 'hidden' && + element.type !== 'hidden'; }); return filtered; } - - static isVisibleElement(element) { - let style = window.getComputedStyle(element); - if (style.display === 'none') { - return false; - } else if (style.visibility === 'hidden') { - return false; - } - return true; - } } diff --git a/src/content/index.js b/src/content/index.js index 8848875..159429e 100644 --- a/src/content/index.js +++ b/src/content/index.js @@ -11,11 +11,32 @@ consoleFrames.initialize(window.document); const startFollows = (newTab) => { let follow = new Follow(window.document, newTab); follow.onActivated((element) => { - browser.runtime.sendMessage({ - type: messages.OPEN_URL, - url: element.href, - newTab - }); + switch (element.tagName.toLowerCase()) { + case 'a': + return browser.runtime.sendMessage({ + type: messages.OPEN_URL, + url: element.href, + newTab + }); + case 'input': + switch (element.type) { + case 'file': + case 'checkbox': + case 'radio': + case 'submit': + case 'reset': + case 'button': + case 'image': + case 'color': + return element.click(); + default: + return element.focus(); + } + case 'textarea': + return element.focus(); + case 'button': + return element.click(); + } }); };