-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpopup.js
More file actions
365 lines (313 loc) · 17.6 KB
/
Copy pathpopup.js
File metadata and controls
365 lines (313 loc) · 17.6 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
let text_decoration = true;
function matchRuleShort(str, rule) {
const escapeRegex = (str) => str.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
return new RegExp("^" + rule.split("*").map(escapeRegex).join(".*") + "$").test(str);
}
function onError(error) {
console.error(`Error: ${error}`);
}
class Custom_option {
constructor(url, background, color, shadowActivated, shadowColor, shadowBlur, decorationActivated, decorationType, decorationColor) {
this.url = url;
this.color = color;
this.background = background;
this.shadowActivated = shadowActivated;
this.shadowColor = shadowColor;
this.shadowBlur = shadowBlur;
this.decorationActivated = decorationActivated;
this.decorationType = decorationType;
this.decorationColor = decorationColor;
}
}
async function addCustom() {
try {
const result = await chrome.storage.local.get();
const custom_url = document.querySelector("#add_url").value;
document.querySelector("#add_url").value = "";
const customSettings = new Custom_option(
custom_url,
document.querySelector("#background_color").value || "#007EF3",
document.querySelector("#color").value || "white",
document.querySelector("input#activate_textShadow").checked || false,
document.querySelector("#shadow-color").value || "none",
document.querySelector("#shadow-blur").value || "0",
document.querySelector("input#activate_textDecoration").checked || false,
document.querySelector("#decoration_select").value || "none",
document.querySelector("#decoration-color").value || "#007EF3FF"
);
const customs = result.customOptions || [];
customs.push(customSettings);
await chrome.storage.local.set({ customOptions: customs });
const optionToAdd = document.createElement("option");
optionToAdd.textContent = custom_url;
optionToAdd.value = custom_url;
const select = document.querySelector("#custom_select");
select.appendChild(optionToAdd);
select.value = custom_url;
select.dispatchEvent(new Event('change'));
chrome.runtime.sendMessage({ request: "update-action-icon" });
} catch (error) {
onError(error);
}
}
async function removeCustom() {
try {
const result = await chrome.storage.local.get();
const selectIndex = document.querySelector("#custom_select").selectedIndex - 1;
const customs = result.customOptions;
customs.splice(selectIndex, 1);
await chrome.storage.local.set({ customOptions: customs });
const customSettingsSelect = document.querySelector("#custom_select");
customSettingsSelect.remove(document.querySelector("#custom_select").selectedIndex);
document.querySelector("#url_div").style.display = "none";
document.querySelector("#remove_custom").style.display = "none";
// Restore default settings
document.querySelector("#background_color").jscolor.fromString(result.background_color || "#007EF3");
document.querySelector("#color").jscolor.fromString(result.color || "white");
document.querySelector("#shadow-color").jscolor.fromString(result.shadowColor || "#ffffff");
document.querySelector("#shadow-blur").value = result.shadowBlur || "0px";
document.querySelector("input#activate_textShadow").checked = result.shadowActivated;
document.querySelector("input#activate_textDecoration").checked = result.decorationActivated;
document.querySelector("#decoration_select").value = result.decorationType;
document.querySelector("#decoration-color").jscolor.fromString(result.decorationColor || "#007EF3FF");
document.querySelector('div#textShadowOptions').style.display = result.shadowActivated ? "flex" : "none";
document.querySelector('div#textDecorationOptions').style.display = result.decorationActivated ? "flex" : "none";
const tabs = await chrome.tabs.query({active: true, currentWindow: true});
if (tabs.length > 0) {
const activeTabURL = new URL(tabs[0].url);
document.querySelector("#add_url").value = activeTabURL.host;
}
updatePreview();
chrome.runtime.sendMessage({ request: "inject-css-all" });
chrome.runtime.sendMessage({ request: "update-action-icon" });
} catch (error) {
onError(error);
}
}
async function saveOptions(e) {
e.preventDefault();
const selectIndex = document.querySelector("#custom_select").selectedIndex;
try {
if(selectIndex === 0) {
await chrome.storage.local.set({
background_color: document.querySelector("#background_color").value || "#007EF3",
color: document.querySelector("#color").value || "white",
shadowActivated: document.querySelector("input#activate_textShadow").checked || false,
shadowColor: document.querySelector("#shadow-color").value || "none",
shadowBlur: document.querySelector("#shadow-blur").value || "0",
decorationActivated: document.querySelector("input#activate_textDecoration").checked || false,
decorationType: document.querySelector("#decoration_select").value || "none",
decorationColor: document.querySelector("#decoration-color").value || "none",
witness: true
});
} else {
const result = await chrome.storage.local.get();
const customs = result.customOptions;
const indexToUpdate = selectIndex - 1;
customs[indexToUpdate] = {
background: document.querySelector("#background_color").value || "#007EF3",
color: document.querySelector("#color").value || "white",
shadowActivated: document.querySelector("input#activate_textShadow").checked || false,
shadowColor: document.querySelector("#shadow-color").value || "none",
shadowBlur: document.querySelector("#shadow-blur").value || "0",
decorationActivated: document.querySelector("input#activate_textDecoration").checked || false,
decorationType: document.querySelector("#decoration_select").value || "none",
decorationColor: document.querySelector("#decoration-color").value || "none",
url: document.querySelector("#change_url").value
};
await chrome.storage.local.set({ customOptions: customs });
const select = document.querySelector("#custom_select");
select.options[select.selectedIndex].textContent = document.querySelector("#change_url").value;
select.options[select.selectedIndex].value = document.querySelector("#change_url").value;
}
chrome.runtime.sendMessage({ request: "inject-css-all" });
} catch (error) {
onError(error);
}
}
function updatePreview() {
const preview = document.querySelector("#preview");
preview.style.background = document.querySelector("#background_color").value;
preview.style.color = document.querySelector("#color").value;
if (document.querySelector("input#activate_textShadow").checked) {
preview.style.textShadow = `${document.querySelector("#shadow-color").value} 0px 0px ${document.querySelector("#shadow-blur").value}px`;
} else {
preview.style.textShadow = "";
}
if (document.querySelector("input#activate_textDecoration").checked) {
preview.style.textDecoration = `${document.querySelector("#decoration_select").value} ${document.querySelector("#decoration-color").value}`;
} else {
preview.style.textDecoration = "";
}
}
async function restoreOptions() {
try {
const result = await chrome.storage.local.get();
if (result.customOptions) {
result.customOptions.forEach(element => {
const option = document.createElement("option");
option.textContent = element.url;
option.value = element.url;
document.querySelector("#custom_select").appendChild(option);
});
}
const tabs = await chrome.tabs.query({active: true, currentWindow: true});
if (tabs.length > 0) {
const activeTabURL = new URL(tabs[0].url);
let isOnCustom = false;
if (result.customOptions) {
result.customOptions.forEach((element) => {
if (matchRuleShort(activeTabURL.host, element.url)) {
const select = document.querySelector("#custom_select");
select.value = element.url;
select.dispatchEvent(new Event('change'));
isOnCustom = true;
}
});
}
if (!isOnCustom) {
document.querySelector("#add_url").value = activeTabURL.host;
document.querySelector("#custom_select").dispatchEvent(new Event('change'));
}
}
if (!text_decoration) {
document.querySelector("#formdivtxtdec").style.display = "none";
}
} catch (error) {
onError(error);
}
}
function updateShadowColorDisplay() {
document.querySelector('div#textShadowOptions').style.display = document.querySelector("input#activate_textShadow").checked ? "flex" : "none";
}
function updateDecorationDisplay() {
document.querySelector('div#textDecorationOptions').style.display = document.querySelector("input#activate_textDecoration").checked ? "flex" : "none";
}
async function changeCustomDisplay(event) {
const selectIndex = document.querySelector("#custom_select").selectedIndex;
try {
const result = await chrome.storage.local.get();
if(selectIndex !== 0) {
const customUrl = document.querySelector("#custom_select").value;
document.querySelector("#change_url").value = customUrl;
document.querySelector("#change_url").placeholder = customUrl;
document.querySelector("#url_div").style.display = "block";
document.querySelector("#remove_custom").style.display = "block";
const customs = result.customOptions;
const currentCustom = customs[selectIndex - 1];
document.querySelector("#background_color").jscolor.fromString(currentCustom.background || "#007EF3");
document.querySelector("#color").jscolor.fromString(currentCustom.color || "white");
document.querySelector("#shadow-color").jscolor.fromString(currentCustom.shadowColor || "#ffffff");
document.querySelector("#shadow-blur").value = currentCustom.shadowBlur || "0px";
document.querySelector("input#activate_textShadow").checked = currentCustom.shadowActivated;
document.querySelector("input#activate_textDecoration").checked = currentCustom.decorationActivated;
document.querySelector("#decoration_select").value = currentCustom.decorationType;
document.querySelector("#decoration-color").jscolor.fromString(currentCustom.decorationColor || "#007EF3FF");
document.querySelector('div#textShadowOptions').style.display = currentCustom.shadowActivated ? "flex" : "none";
document.querySelector('div#textDecorationOptions').style.display = currentCustom.decorationActivated ? "flex" : "none";
} else {
document.querySelector("#url_div").style.display = "none";
document.querySelector("#remove_custom").style.display = "none";
document.querySelector("#background_color").jscolor.fromString(result.background_color || "#007EF3");
document.querySelector("#color").jscolor.fromString(result.color || "white");
document.querySelector("#shadow-color").jscolor.fromString(result.shadowColor || "#ffffff");
document.querySelector("#shadow-blur").value = result.shadowBlur || "0px";
document.querySelector("input#activate_textShadow").checked = result.shadowActivated;
document.querySelector("input#activate_textDecoration").checked = result.decorationActivated;
document.querySelector("#decoration_select").value = result.decorationType;
document.querySelector("#decoration-color").jscolor.fromString(result.decorationColor || "#007EF3FF");
document.querySelector('div#textShadowOptions').style.display = result.shadowActivated ? "flex" : "none";
document.querySelector('div#textDecorationOptions').style.display = result.decorationActivated ? "flex" : "none";
}
updatePreview();
} catch (error) {
onError(error);
}
}
function localizeHtmlPage() {
const objects = document.getElementsByTagName('html');
for (let j = 0; j < objects.length; j++) {
const obj = objects[j];
const valStrH = obj.innerHTML.toString();
const valNewH = valStrH.replace(/__MSG_(\w+)__/g, function(match, v1) {
return v1 ? chrome.i18n.getMessage(v1) : "";
});
if(valNewH !== valStrH) {
obj.innerHTML = valNewH;
}
}
}
function checkURLHost(event) {
const inp = event.target;
// FIXED REGEX: Using a literal prevents escape characters from being swallowed by string parsing
const regex = /^(([a-zA-Z*]|[a-zA-Z*][a-zA-Z0-9\-*]*[a-zA-Z0-9*])\.)*([A-Za-z*]|[A-Za-z*][A-Za-z0-9\-*]*[A-Za-z0-9*])$/;
if (regex.test(inp.value)) {
inp.setCustomValidity("");
} else {
inp.setCustomValidity("Please enter a valid hostname pattern");
}
}
localizeHtmlPage();
// Set event listeners
document.addEventListener("DOMContentLoaded", restoreOptions);
document.addEventListener("DOMContentLoaded", () => { let myPicker = new JSColor('#shadow-color', {'position': 'top'}) });
document.addEventListener("DOMContentLoaded", () => { setTimeout(updatePreview, 100) });
document.querySelector("#save_btn").addEventListener("click", saveOptions);
document.querySelector("#formdiv").addEventListener("keyup", updatePreview);
document.querySelector("#formdiv").addEventListener("input", updatePreview);
document.querySelector("#add_url").addEventListener("input", checkURLHost);
document.querySelector("#change_url").addEventListener("input", checkURLHost);
document.querySelector("#add_custom").addEventListener("click", addCustom);
document.querySelector("#remove_custom").addEventListener("click", removeCustom);
document.querySelector("input#activate_textShadow").addEventListener("change", updatePreview);
document.querySelector("select#custom_select").addEventListener("change", changeCustomDisplay);
document.querySelector("input#activate_textShadow").addEventListener("change", updateShadowColorDisplay);
document.querySelector("input#activate_textDecoration").addEventListener("change", updateDecorationDisplay);
document.addEventListener("paste", (e) => {
const t = e.target;
const isEditable = t instanceof HTMLInputElement || t instanceof HTMLTextAreaElement || (t && t.isContentEditable);
if (!isEditable) {
e.preventDefault();
const clipboardData = e.clipboardData || window.clipboardData;
if (!clipboardData) return;
const clipboardText = clipboardData.getData("text") || "";
try {
const json = JSON.parse(clipboardText);
if (json) {
if (json.color) {
document.querySelector("#color").jscolor.fromString("FFFFFFFF");
document.querySelector("#color").jscolor.fromString(json.color);
}
if (json.background_color) {
document.querySelector("#background_color").jscolor.fromString("FFFFFFFF");
document.querySelector("#background_color").jscolor.fromString(json.background_color);
}
if (json.shadowActivated !== undefined) {
document.querySelector("input#activate_textShadow").checked = json.shadowActivated;
document.querySelector('div#textShadowOptions').style.display = json.shadowActivated ? "flex" : "none";
}
if (json.shadowColor) {
document.querySelector("#shadow-color").jscolor.fromString("FFFFFFFF");
document.querySelector("#shadow-color").jscolor.fromString(json.shadowColor);
}
if (json.shadowBlur) {
document.querySelector("#shadow-blur").value = json.shadowBlur;
}
if (json.decorationActivated !== undefined) {
document.querySelector("input#activate_textDecoration").checked = json.decorationActivated;
document.querySelector('div#textDecorationOptions').style.display = json.decorationActivated ? "flex" : "none";
}
if (json.decorationType) {
document.querySelector("#decoration_select").value = json.decorationType;
}
if (json.decorationColor) {
document.querySelector("#decoration-color").jscolor.fromString("FFFFFFFF");
document.querySelector("#decoration-color").jscolor.fromString(json.decorationColor);
}
updatePreview();
}
} catch (err) {
// Silently fail if not valid JSON
}
}
});