-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
97 lines (81 loc) · 2.96 KB
/
content.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Autofocus when loading a website
chrome.storage.local.get((storage) => {
const extensionOn = storage.power ? storage.power.status : true;
// Autofocus on?
chrome.runtime.sendMessage("getUrl", function (response) {
const autofocus = storage.autofocus || {};
thisSite = response.url;
// If current url is in autofocus list, focus the search box
if (extensionOn && autofocus[thisSite]) {
focusSearchBox();
}
});
});
// Listen for Tab Press
document.addEventListener("keydown", (e) => {
chrome.storage.local.get((storage) => {
const extensionOn = storage.power ? storage.power.status : true;
const tabOn = storage.tabulation ? storage.tabulation.status : true;
const tabList = storage.tabList || {};
// Autofocus required
chrome.runtime.sendMessage("getUrl", function (response) {
thisSite = response.url;
if (extensionOn && tabOn && !tabList[thisSite]) {
const searchBoxNotFocused = document.activeElement.tagName !== "INPUT";
// If the searchbar is already focused don't focus it again, instead let people tab through the list of suggestions
if (searchBoxNotFocused && e.key === "Tab") {
focusSearchBox();
e.preventDefault();
}
}
});
});
});
// Listen for messages
chrome.runtime.onMessage.addListener((msg) => {
if (msg === "focus") focusSearchBox();
});
function getStyle(element, name) {
return element.currentStyle ? element.currentStyle[name] : window.getComputedStyle ? window.getComputedStyle(element, null).getPropertyValue(name) : null;
};
function checkUrlForSearchBox() {
chrome.runtime.sendMessage("getUrl", function (response) {
switch (response.url) {
case "www.dictionary.cambridge.org":
return applyFocus(document.getElementById("searchword"));
default:
return null;
}
});
}
function checkHtmlForSearchBox() {
if (document.activeElement.tagName != "INPUT") {
const inputs = document.body.getElementsByTagName("input");
for (let i = 0; i < inputs.length; i++) {
const input = inputs[i];
const isHiddenByDisplay = getStyle(input, 'display') === 'none';
const isHiddenByVisibility = getStyle(input, 'visibility') === 'hidden';
const isHidden = isHiddenByDisplay || isHiddenByVisibility;
const validInputTypes = ['text', 'search', 'email', 'number', 'password', 'tel', 'url'];
const isValidFocusableField = validInputTypes.find(x => x === input.type);
const isDisabledOrReadonly = input.disabled || input.readOnly;
if (!isHidden && !isDisabledOrReadonly && isValidFocusableField) {
return applyFocus(input);
}
}
if (inputs.length > 0) {
for (let i = 0; i < inputs.length; i++) {
return applyFocus(inputs[i]);
}
}
}
}
function focusSearchBox() {
const urlFound = checkUrlForSearchBox();
if (!urlFound) checkHtmlForSearchBox();
}
function applyFocus(searchBox) {
searchBox.focus();
searchBox.select();
return true;
}