forked from AnneTrue/nexus-clash-interface-tweaks
-
Notifications
You must be signed in to change notification settings - Fork 2
/
safeSpeech.js
42 lines (39 loc) · 1.46 KB
/
safeSpeech.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
const safeSpeech = {
module: async (api) => {
const mod = await api.registerModule(
'safespeech',
'Safe Speech Buttons',
'global',
'Places a safety on speech and bullhorn buttons so that you cannot accidentally send an empty message.',
);
const enableSpeechForm = (e) => {
const button = e.target.previousElementSibling;
if (e.target.value !== '') {
button.disabled = false;
} else {
button.disabled = true;
}
}
const safeSpeech = async (mod) => {
const form = document.evaluate(
"//form[@name='speak' or @name='bullhorn']/input[@type='submit']",
document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null
);
const len = form.snapshotLength
if (len === 0) { return; }
for (let i = 0; i < len; i++) {
let inputButton = form.snapshotItem(i);
inputButton.disabled = true;
inputButton = document.evaluate(
"input[@type='text']",
inputButton.parentNode, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null
);
inputButton.snapshotItem(0).addEventListener('input', enableSpeechForm, false);
}
}
await mod.registerMethod(
'async',
safeSpeech
);
}
}