-
Notifications
You must be signed in to change notification settings - Fork 0
/
tag-groups.js
67 lines (55 loc) · 1.82 KB
/
tag-groups.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
const KEY = "cohostTagGroups";
const extension = (typeof browser === "undefined") ? chrome : browser;
let mapping = {};
extension.storage.sync.get(KEY).then(res => {
mapping = res[KEY];
});
extension.storage.onChanged.addListener(e => {
mapping = e.cohostTagGroups.newValue;
});
const hiddenStyle = `
div[role="combobox"] ul {
display: none;
}
`.replace(/\n/g, "");
const styleSheet = document.createElement("style");
styleSheet.type = "text/css";
styleSheet.innerText = "";
document.head.appendChild(styleSheet);
function keyPress(input, key) {
["keypress", "keydown"].forEach(eventType => {
input.dispatchEvent(
new KeyboardEvent(eventType, { key: key, bubbles: true, cancelable: true })
);
})
}
function inputTag(tags, currentIndex) {
if (currentIndex >= tags.length) {
setTimeout(() => {
styleSheet.innerText = "";
}, 100);
return;
}
input.value = tags[currentIndex];
input.dispatchEvent(
new Event("input", { bubbles: true, cancelable: true })
);
setTimeout(() => {
input.click();
keyPress(input, "Tab");
}, 1);
setTimeout(inputTag, 2, tags, ++currentIndex);
}
let input = document.querySelector('input[placeholder$="tags"]');
const ENTER_KEYS = new Set(["Enter", "Comma", "Semicolon", ",", ";", "Tab"]);
window.addEventListener("keydown", e => {
if (ENTER_KEYS.has(e.key) && e.target.placeholder === "#add tags") {
input = document.querySelector('input[placeholder$="tags"]');
const value = e.target.value;
if (value in mapping) {
styleSheet.innerText = hiddenStyle;
const tags = mapping[value].filter(v => v !== value);
setTimeout(inputTag, 2, tags, 0);
}
}
}, true);