-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgreet-final.user.js
75 lines (65 loc) · 2.16 KB
/
greet-final.user.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
(function () {
const buttonHtml = '<button id="insertTextButton" class="btn btn-default btn-sm">👋</button>';
// Adding button
function addButton() {
const PREVIEW_BUTTON = document.querySelector('[data-marker="preview/button"]');
const existingButton = document.querySelector('#insertTextButton');
if (existingButton) {
return;
}
if (!PREVIEW_BUTTON) {
return;
}
// Глазик
PREVIEW_BUTTON.insertAdjacentHTML('afterend', buttonHtml);
const BTN = document.querySelector('#insertTextButton');
BTN.addEventListener('click', onButtonClick);
}
function onButtonClick(e) {
e.preventDefault();
const TEXT_FIELD = getTextFieldOrHandleMutation();
if (TEXT_FIELD) {
TEXT_FIELD.focus();
sxSaveReactInput(TEXT_FIELD, "Здравствуйте.\n\n" + TEXT_FIELD.value);
}
}
function sxSaveReactInput(input, newVal) {
let lastValue = input.value;
input.value = newVal;
let event = new Event('input', { bubbles: true });
event.simulated = true;
let tracker = input._valueTracker;
if (tracker) {
tracker.setValue(lastValue);
}
input.dispatchEvent(event);
}
// getting textarea field
function getTextFieldOrHandleMutation() {
let isFieldChecked = false;
const TEXT_FIELD = document.querySelector('[data-userscript-marker="comment-field/textarea"]');
if (TEXT_FIELD) {
if (!isFieldChecked) {
addButton();
isFieldChecked = true;
}
} else {
isFieldChecked = false;
}
return TEXT_FIELD;
}
// observer
const observer = new MutationObserver((mutationsList) => {
for (const mutation of mutationsList) {
getTextFieldOrHandleMutation();
}
});
window.addEventListener('load', () => {
getTextFieldOrHandleMutation();
observer.observe(document.body, {
childList: true,
subtree: true,
attributes: true,
});
});
})();