aboutsummaryrefslogtreecommitdiff
path: root/src/content
diff options
context:
space:
mode:
authorShin'ya Ueoka <ueokande@i-beam.org>2018-01-18 10:41:08 +0000
committerGitHub <noreply@github.com>2018-01-18 10:41:08 +0000
commit1f57547c0ab1dab1aa615e2b543067855719ab6f (patch)
treef87441ed3a9889a75a698225151e7b10a07ae1b6 /src/content
parentcc634d0e7fc33d60e67df5a0cbbcfbf365120c5a (diff)
parentd1a81a877f0e7364b7d69e214ec74151d76dd25c (diff)
Merge pull request #308 from ueokande/focus-input
Focus input
Diffstat (limited to 'src/content')
-rw-r--r--src/content/actions/operation.js3
-rw-r--r--src/content/focuses.js13
-rw-r--r--src/content/scrolls.js24
3 files changed, 19 insertions, 21 deletions
diff --git a/src/content/actions/operation.js b/src/content/actions/operation.js
index b4b2e38..5fd0f48 100644
--- a/src/content/actions/operation.js
+++ b/src/content/actions/operation.js
@@ -2,6 +2,7 @@ import operations from 'shared/operations';
import messages from 'shared/messages';
import * as scrolls from 'content/scrolls';
import * as navigates from 'content/navigates';
+import * as focuses from 'content/focuses';
import * as urls from 'content/urls';
import * as consoleFrames from 'content/console-frames';
import * as addonActions from './addon';
@@ -57,6 +58,8 @@ const exec = (operation, repeat, settings) => {
return navigates.parent(window);
case operations.NAVIGATE_ROOT:
return navigates.root(window);
+ case operations.FOCUS_INPUT:
+ return focuses.focusInput();
case operations.URLS_YANK:
urls.yank(window);
return consoleFrames.postMessage(window.document, {
diff --git a/src/content/focuses.js b/src/content/focuses.js
new file mode 100644
index 0000000..a6f6cc8
--- /dev/null
+++ b/src/content/focuses.js
@@ -0,0 +1,13 @@
+import * as doms from 'shared/utils/dom';
+
+const focusInput = () => {
+ let inputTypes = ['email', 'number', 'search', 'tel', 'text', 'url'];
+ let inputSelector = inputTypes.map(type => `input[type=${type}]`).join(',');
+ let targets = window.document.querySelectorAll(inputSelector + ',textarea');
+ let target = Array.from(targets).find(doms.isVisible);
+ if (target) {
+ target.focus();
+ }
+};
+
+export { focusInput };
diff --git a/src/content/scrolls.js b/src/content/scrolls.js
index e8e9642..0d1f7c8 100644
--- a/src/content/scrolls.js
+++ b/src/content/scrolls.js
@@ -1,3 +1,5 @@
+import * as doms from 'shared/utils/dom';
+
const SCROLL_DELTA_X = 48;
const SCROLL_DELTA_Y = 48;
const SMOOTH_SCROLL_DURATION = 150;
@@ -5,25 +7,6 @@ const SMOOTH_SCROLL_DURATION = 150;
// dirty way to store scrolling state on globally
let scrolling = [false];
-const isVisible = (element) => {
- let rect = element.getBoundingClientRect();
- if (rect.width === 0 || rect.height === 0) {
- return false;
- }
- if (rect.right < 0 && rect.bottom < 0) {
- return false;
- }
- if (window.innerWidth < rect.left && window.innerHeight < rect.top) {
- return false;
- }
-
- let { display, visibility } = window.getComputedStyle(element);
- if (display === 'none' || visibility === 'hidden') {
- return false;
- }
- return true;
-};
-
const isScrollableStyle = (element) => {
let { overflowX, overflowY } = window.getComputedStyle(element);
return !(overflowX !== 'scroll' && overflowX !== 'auto' &&
@@ -44,8 +27,7 @@ const findScrollable = (element) => {
return element;
}
- let children = Array.prototype
- .filter.call(element.children, e => isVisible(e));
+ let children = Array.from(element.children).filter(doms.isVisible);
for (let child of children) {
let scrollable = findScrollable(child);
if (scrollable) {