-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwitch.tv-hitsquadgodfather.user.js
More file actions
382 lines (330 loc) · 11.6 KB
/
twitch.tv-hitsquadgodfather.user.js
File metadata and controls
382 lines (330 loc) · 11.6 KB
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
// ==UserScript==
// @name Twitch - hitsquadgodfather
// @namespace ngoclong19.github.io
// @version 0.1.0
// @description 🤖 Auto send chat commands on button click!
// @description:vi 🤖 Tự động gửi lệnh trò chuyện khi nhấp vào nút!
// @author ngoclong19
// @match https://www.twitch.tv/hitsquadgodfather
// @icon https://www.twitch.tv/favicon.ico
// @grant none
// @source https://github.com/ngoclong19/userscripts
// @require https://cdn.jsdelivr.net/npm/eventemitter3@5.0.1/dist/eventemitter3.umd.min.js
// ==/UserScript==
// References
// https://github.com/night/betterttv/blob/e60337f43371475254a52c248610c65b6cd802c0/src/modules/emote_menu/twitch/EmoteMenu.jsx
// https://github.com/night/betterttv/blob/b544aee0395f04af17521fd936b79da9a0755b94/src/observers/dom.js
// https://github.com/night/betterttv/blob/24f21e5595e105694038ade472229e0798e10b1c/src/utils/safe-event-emitter.js
// https://github.com/night/betterttv/blob/0ee4e02a7e22d633d6fe1d7bc7ecf7fb1f67eaf0/src/utils/twitch.js
'use strict';
/* global globalThis */
(function () {
const EventEmitter = globalThis.EventEmitter3; // from npm package eventemitter3
const CHAT_CONTAINER =
'section[data-test-selector="chat-room-component-layout"]';
const CHAT_SETTINGS_BUTTON_CONTAINER_SELECTOR =
'.chat-input div[data-test-selector="chat-input-buttons-container"]';
const IGNORED_HTML_TAGS = new Set([
'BR',
'HEAD',
'LINK',
'META',
'SCRIPT',
'STYLE',
]);
// const MSG_INTERVAL = 500 * 5;
let observer;
const observedIds = Object.create(null);
const observedClassNames = Object.create(null);
const observedTestSelectors = Object.create(null);
const attributeObservers = new Map();
/**
* @param {HTMLElement} element
* @returns {Object | null}
*/
const getReactInstance = (element) => {
for (const key in element) {
if (key.startsWith('__reactInternalInstance$')) {
return /** @type {Object} */ (
element[/** @type {keyof HTMLElement} */ (key)]
);
}
}
return null;
};
const searchReactParents = (node, predicate, maxDepth = 15, depth = 0) => {
try {
if (predicate(node)) {
return node;
}
} catch (_) {}
if (!node || depth > maxDepth) {
return null;
}
const { return: parent } = node;
if (parent) {
return searchReactParents(parent, predicate, maxDepth, depth + 1);
}
return null;
};
const getCurrentChat = () => {
let currentChat;
try {
const node = searchReactParents(
getReactInstance(
/** @type {HTMLElement} */ (document.querySelector(CHAT_CONTAINER))
),
(n) =>
n.stateNode && n.stateNode.props && n.stateNode.props.onSendMessage
);
currentChat = node.stateNode;
} catch (_) {}
return currentChat;
};
const sendChatMessage = (message) => {
const currentChat = getCurrentChat();
if (!currentChat) return;
currentChat.props.onSendMessage(message);
};
const clickHandler = async () => {
sendChatMessage('!hitsquad');
};
const loadLegacyButton = () => {
const container = document.querySelector(
CHAT_SETTINGS_BUTTON_CONTAINER_SELECTOR
);
if (container == null) {
console.log('`container` not found');
return;
}
const rightContainer = container.lastChild;
const buttonContainer = document.createElement('div');
rightContainer.insertBefore(buttonContainer, rightContainer.firstChild);
const button = document.createElement('button');
// https://fontawesome.com/icons/robot?s=solid&f=classic
button.innerHTML =
'<svg xmlns="http://www.w3.org/2000/svg" height="1em" viewBox="0 0 640 512"><!--! Font Awesome Free 6.4.2 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license (Commercial License) Copyright 2023 Fonticons, Inc. --><path d="M320 0c17.7 0 32 14.3 32 32V96H472c39.8 0 72 32.2 72 72V440c0 39.8-32.2 72-72 72H168c-39.8 0-72-32.2-72-72V168c0-39.8 32.2-72 72-72H288V32c0-17.7 14.3-32 32-32zM208 384c-8.8 0-16 7.2-16 16s7.2 16 16 16h32c8.8 0 16-7.2 16-16s-7.2-16-16-16H208zm96 0c-8.8 0-16 7.2-16 16s7.2 16 16 16h32c8.8 0 16-7.2 16-16s-7.2-16-16-16H304zm96 0c-8.8 0-16 7.2-16 16s7.2 16 16 16h32c8.8 0 16-7.2 16-16s-7.2-16-16-16H400zM264 256a40 40 0 1 0 -80 0 40 40 0 1 0 80 0zm152 40a40 40 0 1 0 0-80 40 40 0 1 0 0 80zM48 224H64V416H48c-26.5 0-48-21.5-48-48V272c0-26.5 21.5-48 48-48zm544 0c26.5 0 48 21.5 48 48v96c0 26.5-21.5 48-48 48H576V224h16z"/></svg>';
button.style.width = button.style.height = '2rem';
button.firstChild.style.fill = 'currentcolor';
button.onclick = clickHandler;
buttonContainer.append(button);
};
const newListener = (listener, ...args) => {
try {
listener(...args);
} catch (e) {
console.error('Failed executing listener callback', e.stack);
}
};
const parseSelector = (selector) => {
const partialSelectors = selector.split(',').map((s) => s.trim());
const ids = [];
const classNames = [];
const testSelectors = [];
for (const partialSelector of partialSelectors) {
if (partialSelector.startsWith('#')) {
ids.push({
key: partialSelector.split(' ')[0].split('#')[1],
partialSelector,
});
} else if (partialSelector.startsWith('.')) {
classNames.push({
key: partialSelector.split(' ')[0].split('.')[1],
partialSelector,
});
} else if (partialSelector.includes('[data-test-selector')) {
testSelectors.push({
key: partialSelector
.split(' ')[0]
.split('[data-test-selector="')[1]
.split('"]')[0],
partialSelector,
});
}
}
return {
ids,
classNames,
testSelectors,
};
};
const startAttributeObserver = (observedType, emitter, node) => {
const attributeObserver = new window.MutationObserver(() =>
emitter.emit(observedType.selector, node, node.isConnected)
);
attributeObserver.observe(node, { attributes: true, subtree: true });
attributeObservers.set(observedType, attributeObserver);
};
const stopAttributeObserver = (observedType) => {
const attributeObserver = attributeObservers.get(observedType);
if (!attributeObserver) {
return;
}
attributeObserver.disconnect();
attributeObservers.delete(observedType);
};
const processObservedResults = (emitter, target, node, results) => {
if (!results || results.length === 0) {
return;
}
for (const observedType of results) {
const { partialSelector, selector, options } = observedType;
let foundNode = partialSelector.includes(' ')
? node.querySelector(selector)
: node;
if (!foundNode) {
continue;
}
if (options && options.useParentNode) {
foundNode = node;
}
if (options && options.useTargetNode) {
foundNode = target;
}
const { isConnected } = foundNode;
if (options && options.attributes) {
if (isConnected) {
startAttributeObserver(observedType, emitter, foundNode);
} else {
stopAttributeObserver(observedType);
}
}
emitter.emit(selector, foundNode, isConnected);
}
};
const processMutations = (emitter, nodes) => {
if (!nodes || nodes.length === 0) {
return;
}
for (const [target, node] of nodes) {
let nodeId = node.id;
if (typeof nodeId === 'string' && nodeId.length > 0) {
nodeId = nodeId.trim();
processObservedResults(emitter, target, node, observedIds[nodeId]);
}
let testSelector = node.getAttribute('data-test-selector');
if (typeof testSelector === 'string' && testSelector.length > 0) {
testSelector = testSelector.trim();
processObservedResults(
emitter,
target,
node,
observedTestSelectors[testSelector]
);
}
const nodeClassList = node.classList;
if (nodeClassList && nodeClassList.length > 0) {
for (let className of nodeClassList) {
className = className.trim();
processObservedResults(
emitter,
target,
node,
observedClassNames[className]
);
}
}
}
};
class SafeEventEmitter extends EventEmitter {
constructor() {
super();
}
on(type, listener) {
const callback = newListener.bind(this, listener);
super.on(type, callback);
return () => super.off(type, callback);
}
}
class DOMObserver extends SafeEventEmitter {
constructor() {
super();
observer = new window.MutationObserver((mutations) => {
const pendingNodes = [];
for (const { addedNodes, removedNodes, target } of mutations) {
if (
!addedNodes ||
!removedNodes ||
(addedNodes.length === 0 && removedNodes.length === 0)
) {
continue;
}
for (let i = 0; i < 2; i++) {
const nodes = i === 0 ? addedNodes : removedNodes;
for (const node of nodes) {
if (
node.nodeType !== Node.ELEMENT_NODE ||
IGNORED_HTML_TAGS.has(node.nodeName)
) {
continue;
}
pendingNodes.push([target, node]);
if (node.childElementCount === 0) {
continue;
}
for (const childNode of node.querySelectorAll('[id],[class]')) {
pendingNodes.push([target, childNode]);
}
}
}
}
if (pendingNodes.length === 0) {
return;
}
processMutations(this, pendingNodes);
});
observer.observe(document, { childList: true, subtree: true });
}
on(selector, callback, options) {
const parsedSelector = parseSelector(selector);
const initialNodes = [];
for (const selectorType of Object.keys(parsedSelector)) {
let observedSelectorType;
switch (selectorType) {
case 'ids':
observedSelectorType = observedIds;
break;
case 'classNames':
observedSelectorType = observedClassNames;
break;
case 'testSelectors':
observedSelectorType = observedTestSelectors;
break;
default:
break;
}
for (const { key, partialSelector } of parsedSelector[selectorType]) {
const currentObservedTypeSelectors = observedSelectorType[key];
const observedType = { partialSelector, selector, options };
if (!currentObservedTypeSelectors) {
observedSelectorType[key] = [observedType];
} else {
currentObservedTypeSelectors.push(observedType);
}
if (observedSelectorType === observedIds) {
initialNodes.push(...document.querySelectorAll(`#${key}`));
} else if (observedSelectorType === observedClassNames) {
initialNodes.push(...document.getElementsByClassName(key));
}
}
}
const result = super.on(selector, callback);
// trigger dom mutations for existing elements for on page
processMutations(
this,
initialNodes.map((node) => [node.parentElement, node])
);
return result;
}
}
const domObserver = new DOMObserver();
domObserver.on(
CHAT_SETTINGS_BUTTON_CONTAINER_SELECTOR,
(_node, isConnected) => {
if (!isConnected) {
return;
}
loadLegacyButton();
}
);
})();