-
Notifications
You must be signed in to change notification settings - Fork 43
/
options.js
99 lines (92 loc) · 2.67 KB
/
options.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
import {
$,
syncStore,
getGlobalSettings,
setGlobalSetting,
delSiteSettings,
resetSiteSchemes,
} from './common.js';
function initSettings() {
const globalSettings = getGlobalSettings();
if (globalSettings['detect_animation']) {
$('detect_animation').value = globalSettings['detect_animation'];
}
}
async function onForget() {
await resetSiteSchemes();
loadSettingsDisplay((await syncStore()).export());
}
// Open all links in new tabs.
function onLinkClick() {
const links = document.getElementsByTagName("a");
for (let i = 0; i < links.length; i++) {
(function () {
const ln = links[i];
const location = ln.href;
ln.onclick = function () {
chrome.tabs.create({active: true, url: location});
};
})();
}
}
function onDetectAnim(evt) {
setGlobalSetting('detect_animation', evt.target.value);
}
function loadSettingsDisplay(store) {
function makeTag(tag, ...contents) {
const element = document.createElement(tag);
for (const child of contents) {
console.log(`child: ${child} - ${typeof child}`);
try {
element.appendChild(
typeof child === "string" ? document.createTextNode(child)
: child
);
} catch {
console.log(`Bad contents of ${tag}: ${JSON.stringify(contents)}`);
console.log(`Bad child type: ${JSON.stringify(child)}`);
}
}
return element;
}
function makeSiteDiv([url, filter, ...mods]) {
const deleteIcon = makeTag("img");
deleteIcon.src = chrome.runtime.getURL("delete.svg");
const deleteBtn = url ? makeTag("button", deleteIcon) : makeTag("span", "");
const row = makeTag('div',
deleteBtn,
makeTag('span', url || "DEFAULT"),
makeTag('span', filter),
makeTag('span', mods.join(', ')),
);
if (url) {
deleteBtn.className = "delete-button";
deleteBtn.onclick = () => {
row.parentElement.removeChild(row);
delSiteSettings(url);
}
}
return row;
}
const settingsDiv = $('settings');
const heading = makeTag("div",
makeTag("span", ""),
makeTag("span", "Website"),
makeTag("span", "Filter"),
makeTag("span", "Options"),
);
heading.id = "settings-heading";
settingsDiv.appendChild(heading);
for (const site of store) {
settingsDiv.appendChild(makeSiteDiv(site));
}
}
async function init() {
const store = await syncStore();
initSettings();
$('forget').addEventListener('click', onForget, false);
$('detect_animation').addEventListener('change', onDetectAnim, false);
loadSettingsDisplay(store.export());
}
window.addEventListener('load', init, false);
document.addEventListener('DOMContentLoaded', onLinkClick);