-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGlobal.user.js
63 lines (54 loc) · 2.01 KB
/
Global.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
// ==UserScript==
// @name Global
// @namespace http://tampermonkey.net/
// @version 1.0
// @description Открытие, очистка, нажатие кнопки и закрытие CodeMirror на Cainiao
// @author SaintAsk
// @match https://global.cainiao.com/*
// @grant none
// ==/UserScript==
(function () {
'use strict';
let isCodeMirrorOpen = false;
const getElement = (selector) => document.querySelector(selector);
const toggleCodeMirror = (show) => {
const wrapper = getElement('.codemirror-wrapper');
const codeMirror = getElement('.CodeMirror');
if (wrapper && codeMirror) {
wrapper.style.opacity = show ? '1' : '0';
wrapper.style.height = show ? 'auto' : '40px';
codeMirror.style.display = show ? 'block' : 'none';
isCodeMirrorOpen = show;
}
};
const clearCodeMirrorText = () => {
const codeMirror = getElement('.CodeMirror');
if (codeMirror?.CodeMirror) codeMirror.CodeMirror.setValue('');
};
const clickQueryButton = () => getElement('.next-btn.track-btn')?.click();
const setupInputFocusListener = () => {
const inputField = getElement('input[name="search"]');
if (inputField) {
inputField.addEventListener('focus', () => {
if (!isCodeMirrorOpen && document.visibilityState === 'visible') {
toggleCodeMirror(true);
clearCodeMirrorText();
}
});
}
};
document.addEventListener('keydown', (event) => {
if (event.key === 'Enter') {
event.preventDefault();
clickQueryButton();
toggleCodeMirror(false);
getElement('input[name="search"]')?.focus();
}
});
const waitForInputField = setInterval(() => {
if (getElement('input[name="search"]')) {
clearInterval(waitForInputField);
setupInputFocusListener();
}
}, 100);
})();