-
Notifications
You must be signed in to change notification settings - Fork 1
/
selector.js
168 lines (143 loc) · 5.12 KB
/
selector.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
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
document.addEventListener("DOMContentLoaded", function(event) {
document.getElementById("emote-search").oninput = updateSearchSchedule;
document.getElementById("emote-delete").onclick = deleteEmote;
document.getElementById("emote-delete").ondblclick = clearEmotes;
document.getElementById("emote-overwrite").onclick = overwriteEmote;
browser.storage.local.get().then(res => {
update(res.emotes);
});
document.addEventListener("keydown", function(e) {
if (e.key == "Escape") {
window.close();
}
});
document.getElementById("emote-search").focus();
});
function emotesearch() {
let search = document.getElementById("emote-search").value;
if (!search) {
browser.storage.local.get().then(res => {
update(res.emotes);
});
return false;
}
browser.storage.local.get().then(res => {
let searchinstance = new Fuse(res.emotes, {keys: ["name"]});
let result = searchinstance.search(search);
for (let i in result) {
result[i] = result[i].item;
};
update(result);
});
};
function update(emotes) {
/*
* updates the selector emote list, given a list of emotes
*/
let emotelist = document.getElementById("emote-list");
emotelist.innerHTML = "";
for (let value of Object.values(emotes)) {
let emote = document.createElement("img");
emote.src = value.url;
emote.alt = value.name;
emote.title = value.name;
emote.id = value.id;
emote.classList.add("emote");
emote.tabIndex = 0;
emote.onclick = function() {selectEmote(value.id); previewEmote()};
emote.ondblclick = function() {navigator.clipboard.writeText(value.url); recentEmote(value.id); window.close();};
emotelist.appendChild(emote);
};
};
function selectEmote(emoteId) {
let emotes = document.getElementsByClassName("emote");
for (let i in emotes) {
if (typeof emotes[i] == "object") {
emotes[i].classList.remove("selected");
}
};
document.getElementById(emoteId).classList.add("selected");
};
function deleteEmote() {
let selected = document.getElementsByClassName("selected");
if (selected.length == 0) {
return;
}
let emoteId = selected[0].id;
browser.storage.local.get().then(res => {
res.emotes.splice(emoteId, 1);
browser.storage.local.set({emotes: res.emotes}).then(() => {
// re-order emote ids
browser.storage.local.get().then(res => {
let emotes = res.emotes;
for (let i in emotes) {
emotes[i].id = i;
}
browser.storage.local.set({emotes: emotes}).then(() => {
browser.storage.local.get().then(res => {update(res.emotes)});
});
});
});
});
};
function previewEmote() {
let selected = document.getElementsByClassName("selected");
if (selected.length == 0) {
return;
};
let emoteId = selected[0].id;
browser.storage.local.get().then(res => {
let emote = res.emotes[emoteId];
document.getElementById("emote-preview").style = "background-image: url("+ emote.url +"); background-repeat: no-repeat; background-position: center; width: 48px; height: 48px;"
document.getElementById("emote-pr-name").value = emote.name;
document.getElementById("emote-pr-url").value = emote.url;
});
};
function overwriteEmote() {
let selected = document.getElementsByClassName("selected");
if (selected.length == 0) {
return;
};
let emoteId = selected[0].id;
let name = escapeHTML(document.getElementById("emote-pr-name").value);
let url = escapeHTML(document.getElementById("emote-pr-url").value);
browser.storage.local.get().then(res => {
res.emotes[emoteId].name = name;
res.emotes[emoteId].url = url;
browser.storage.local.set({emotes: res.emotes}).then(() => {
update(res.emotes);
});
});
};
function recentEmote(id) {
browser.storage.local.get().then(res => {
res.emotes[id].count += 1;
browser.storage.local.set({emotes: res.emotes});
});
};
function clearEmotes() {
if (window.confirm("Are you sure you want to clear all emotes?")) {
browser.storage.local.set({emotes: []}).then(() => {
browser.storage.local.get().then(res => {update(res.emotes)});
});
};
};
let updateSearchTask = null;
function updateSearchSchedule(){
if (updateSearchTask != null) {
clearTimeout(updateSearchTask);
}
updateSearchTask = setTimeout(function(){
updateSearchTask = null;
emotesearch();
}, 500);
}
// https://gist.github.com/Rob--W/ec23b9d6db9e56b7e4563f1544e0d546
function escapeHTML(str) {
// Note: string cast using String; may throw if `str` is non-serializable, e.g. a Symbol.
// Most often this is not the case though.
return String(str)
.replace(/&/g, '&')
.replace(/"/g, '"').replace(/'/g, ''')
.replace(/</g, '<').replace(/>/g, '>');
}