-
Notifications
You must be signed in to change notification settings - Fork 1
/
inject.js
42 lines (32 loc) · 1.11 KB
/
inject.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
"use strict";
(bestNode => {
if(!bestNode) {
let nodes = document.querySelectorAll(
"input:not([disabled]):not([readonly]):not([type=submit]):not([type=button]):not([type=reset]):not([type=hidden]):not([type=checkbox]):not([type=radio])"
);
let width = window.innerWidth;
let height = window.innerHeight;
let halfWidth = width/2;
let halfHeight = height/2;
let bestScore;
for(let n of nodes) {
if(!("selectionStart" in n)) continue;
let rect = n.getBoundingClientRect();
let x = rect.left + rect.width/2;
let y = rect.top + rect.height/2;
if(x >= 0 && x <= width && y >= 0 && y <= height) {
let score = Math.pow((halfWidth - x)/width, 2) + Math.pow((halfHeight - y)/height, 2);
if(n.type === "password") score *= 1.5;
if(!bestNode || score < bestScore) {
bestNode = n;
bestScore = score;
}
}
}
}
if(bestNode) {
bestNode.focus();
} else {
alert(chrome.i18n.getMessage("alertNoFocus"));
}
})(document.querySelector("input[autofocus]"));