-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
247 lines (217 loc) · 8.39 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
let chatWindow = document.querySelector("[data-a-target='chat-scroller'");
let chatInput = document.querySelector("[data-a-target='chat-input'");
let chatSendButton = document.querySelector("[data-a-target='chat-send-button']");
let chatLeaderBoard = document.querySelector("[data-test-selector='channel-leaderboard-container']");
let twitchKeysButton = document.createElement("button");
let keysMap = {
"1": "1",
"2": "2",
"3": "3",
"4": "4",
"5": "5",
"6": "6",
"7": "7",
"8": "8",
"9": "9",
"0": "10"
};
// HYDRATE MENU WITH VALUES
const getInitValues = (inputs) => {
for(let i = 0; i < inputs.length; i++) {
let input = inputs[i];
chrome.storage.sync.get([input.name], (value) => {
let userValue = value[input.name];
if(!userValue) {
input.value = "";
return;
}
input.value = userValue;
});
}
};
// TRIGGER HEADER MESSAGE
const triggerHeaderMessage = (headerMessage, headerTimeout, msg) => {
if(headerMessage.classList.contains("tkextension__header__message--active")) headerMessage.classList.remove("tkextension__header__message--active");
window.clearTimeout(headerTimeout);
headerMessage.offsetHeight;
headerMessage.textContent = msg;
headerMessage.classList.add("tkextension__header__message--active");
headerTimeout = window.setTimeout(() => {headerMessage.classList.remove("tkextension__header__message--active")}, 2000);
};
// ADD TWITCHKEYS BUTTON TO DOM
const addTwitchKeysButton = () => {
twitchKeysButton.classList.add("tw-core-button", "tw-button-icon", "tkextension__twitch-keys-button");
twitchKeysButton.title = "TwitchKeys Menu";
chatSendButton.parentNode.insertBefore(twitchKeysButton, chatSendButton.parentNode.firstChild);
};
// GET SAVED KEY VALUE IN STORAGE
const getKeyValue = (key) => {
return new Promise((res, rej) => {
try {
chrome.storage.sync.get(`key${key}`, (value) => {
if(value[`key${key}`] === undefined) res("");
res(value[`key${key}`]);
});
}
catch (err) {
rej(err);
}
});
};
// ADD KEY VALUE TO CHAT INPUT
const addHotKeyValue = (emote) => {
chatInput.value += emote + " ";
chatInput.dispatchEvent(new Event('input', { bubbles: true }));
return;
};
// CHECK IF CTRL IS HELD DOWN
const handleChatKeyDown = (e) => {
if(e.ctrlKey && keysMap[e.key]) {
e.preventDefault();
isOn = true;
}
};
// CHECK IF HOTKEY IS PRESSED
const handleChatKeyUp = async (e) => {
let value = "";
if(!e.ctrlKey) return;
if(isOn && keysMap[e.key]) {
value = await getKeyValue(keysMap[e.key]);
if(!value.length) return;
addHotKeyValue(value);
}
};
// DETECT DOM ADDING/REMOVAL AND RESELECT DOM ELEMENTS (Site is an SPA)
const observer = new window.MutationObserver(function(mutations_list) {
for(const {addedNodes, removedNodes} of mutations_list) {
// REMOVE UNNECESSARY MUTATIONS
if (!addedNodes || !removedNodes || (addedNodes.length === 0 && removedNodes.length === 0)) {
continue;
}
// CHECK REMOVED NODES AND REMOVE LISTENERS
for(const node of removedNodes) {
if(node.nodeType !== Node.ELEMENT_NODE) continue;
if(node.childElementCount === 0) continue;
for(const childNode of node.querySelectorAll("[id],[class]")) {
if(childNode.getAttribute("data-test-selector") === "chat-input-buttons-container") {
chatInput.removeEventListener("keydown", handleChatKeyDown);
chatInput.removeEventListener("keyup", handleChatKeyUp);
}
}
}
// CHECK ADDED NODES AND RESELECT DOM ELEMENTS
for(const node of addedNodes) {
if(node.nodeType !== Node.ELEMENT_NODE) continue;
if(node.childElementCount === 0) continue;
for(const childNode of node.querySelectorAll("[id],[class]")) {
if(childNode.getAttribute("data-test-selector") === "chat-input-buttons-container") {
chatInput = document.querySelector("[data-a-target='chat-input'");
chatSendButton = document.querySelector("[data-a-target='chat-send-button']");
chatWindow = document.querySelector("[data-a-target='chat-scroller'");
chatLeaderBoard = document.querySelector("[data-test-selector='channel-leaderboard-container']");
chatInput.addEventListener("keydown", handleChatKeyDown);
chatInput.addEventListener("keyup", handleChatKeyUp);
addTwitchKeysButton();
}
}
}
}
});
// TWITCHKEYS HOTKEYS LOGIC
addTwitchKeysButton();
chatInput.addEventListener("keydown", handleChatKeyDown);
chatInput.addEventListener("keyup", handleChatKeyUp);
observer.observe(document.getElementById("root"), { childList: true, subtree: true });
// INJECT HTML AND JS INTO PAGE
fetch(chrome.runtime.getURL("/content.html")).then(res => {
return res.text();
}).then(html => {
document.body.insertAdjacentHTML('beforeend', html);
// INSERT TWITCHKEYS BUTTON
let container = document.querySelector(".tkextension__container");
let twitchKeysButton = document.querySelector(".tkextension__twitch-keys-button");
// SELECTED ELEMENTS
let form = document.getElementById("tkextension__keys-form");
let inputs = document.querySelectorAll("#tkextension__keys-form .tkextension__input");
let formButtonsContainer = document.querySelector(".tkextension__form__buttons");
let saveButton = document.getElementById("tkextension__hotkeys__save");
let clearButton = document.getElementById("tkextension__hotkeys__clear--all");
let formButtonsMessage = document.querySelector(".tkextension__form__buttons__confirm");
let headerMessage = document.querySelector(".tkextension__header__message");
let headerTimeout = null;
// ADJUST TWITCHKEYS MENU SIZE DEPENDING ON CHAT SIZE
const setContainerSize = () => {
chatLeaderBoard = document.querySelector("[data-test-selector='channel-leaderboard-container']");
if(chatLeaderBoard !== null) {
container.style.top = `calc(100px + ${chatLeaderBoard.getBoundingClientRect().height}px)`;
}
else {
container.style.top = `100px`;
}
container.style.width = `calc(${chatWindow.getBoundingClientRect().width}px - 4px)`;
container.style.height = `${chatWindow.getBoundingClientRect().height}px`;
};
// CHECK IF ELEMENT IS VISIBLE
const isVisible = elem => !!elem && !!( elem.offsetWidth || elem.offsetHeight || elem.getClientRects().length );
// TOGGLE CLEAR BUTTON PROMPT BUTTONS
const clearButtonConfirmationMessage = () => {
saveButton.classList.toggle("tkextension__hidden");
clearButton.classList.toggle("tkextension__hidden");
formButtonsMessage.classList.toggle("tkextension__hidden");
};
// TWITCHKEYS POPUP MENU LOGIC
getInitValues(inputs);
setContainerSize();
// RESIZE TWITCHKEYS MENU ON RESIZE
window.addEventListener("resize", (e) => {
setContainerSize();
});
// CLOSE ON OUTSIDE CLICK
window.addEventListener("click", e => {
let target = e.target;
if(!container.contains(target) && isVisible(container)) {
container.classList.toggle("tkextension__container--visible");
}
});
// HANDLE TWITCHKEYS BUTTON CLICK
twitchKeysButton.addEventListener("click", (e) => {
e.stopPropagation();
setContainerSize();
container.classList.toggle("tkextension__container--visible");
});
// HANDLE WHEN ENTER KEY IS PRESSED ON INPUT
form.addEventListener("keyup", (e) => {
if(e.key === "Enter") saveButton.click();
});
// HANDLE BUTTON CLICKS IN TWITCHKEYS MENU
formButtonsContainer.addEventListener("click", e => {
let input = "";
let target = e.target;
if(target.closest("button")) {
// IF SAVE BUTTON
if(target.id === "tkextension__hotkeys__save") {
for(let i = 0; i < inputs.length; i++) {
input = inputs[i];
chrome.storage.sync.set({[input.name]: input.value});
}
triggerHeaderMessage(headerMessage, headerTimeout, "Saved!");
}
// IF CLEAR BUTTON
else if(target.id === "tkextension__hotkeys__clear--all") {
clearButtonConfirmationMessage();
}
// IF CLEAR CANCEL BUTTON
else if(target.id === "tkextension__hotkeys__clear--cancel") {
clearButtonConfirmationMessage();
}
// IF CLEAR CONFIRM BUTTON
else if(target.id === "tkextension__hotkeys__clear--confirm") {
chrome.storage.sync.clear(() => {
getInitValues(inputs);
clearButtonConfirmationMessage();
triggerHeaderMessage(headerMessage, headerTimeout, "Cleared!");
});
}
}
});
});