forked from ryanodd/nyt-pencil-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent-script.js
76 lines (70 loc) · 2.07 KB
/
content-script.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
// returns pencil button HTMLButtonElement or null
function getPencilButton() {
const pencilButtonIconInactive = document.querySelector(
".xwd__toolbar_icon--pencil"
);
const pencilButtonIconActive = document.querySelector(
".xwd__toolbar_icon--pencil-active"
);
const pencilButtonAcrostic = document.querySelector(".acrostic-tool__pencil"); // inactive or active
let pencilButton = null;
if (pencilButtonIconInactive) {
pencilButton = pencilButtonIconInactive.closest("button");
}
if (pencilButtonIconActive) {
pencilButton = pencilButtonIconActive.closest("button");
}
if (pencilButtonAcrostic) {
pencilButton = pencilButtonAcrostic.closest("button");
}
return pencilButton ?? null;
}
function togglePencilButton() {
const pencilButton = getPencilButton();
pencilButton?.click();
}
const defaultKeyCode = "ShiftLeft";
let keyCode = defaultKeyCode;
let tabShifted = false;
function onKeyUp(event) {
if((keyCode === "AltLeft" || keyCode === "AltRight") && event.key === "Alt"){
event.preventDefault(); //Alt key navigates to 3 dots top right in Chrome
}
if (event.code === "Tab" && event.shiftKey) {
//keeps track if tab was clicked during shift, to prevent conflict between shift pencil hotkey and shift+tab to navigate crossword
tabShifted = true;
}
if (event.code === keyCode && !tabShifted) {
togglePencilButton();
}
if (event.key === "Shift") {
tabShifted = false;
}
}
function addKeyListener() {
window.addEventListener("keyup", onKeyUp);
}
const getOptions = () => {
try {
chrome.storage.sync.get(["NYTPencilExtensionOptions"], (result) => {
const settings = result.NYTPencilExtensionOptions;
if (settings) {
keyCode = settings.pencilKeyCode ?? defaultKeyCode;
} else {
keyCode = defaultKeyCode;
}
});
} catch (error) {
keyCode = defaultKeyCode;
console.error(
"Error getting NYTPencilExtensionOptions from storage",
error
);
}
};
try {
getOptions();
addKeyListener();
} catch (error) {
console.error("Content-script error:", error);
}