-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.js
107 lines (93 loc) · 3.73 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
100
101
102
103
104
105
106
107
var tcDefaults = {
enabled: true, // default enabled
defaultSpeed: 1.0, // default:
domainRules: "",
currentSpeed: 1.0,
urlRules: "",
playlistRules: ""
};
function trimProtocolsAndBlanks(array) {
return array.map(ruleset => {
ruleset = ruleset.replace(/(^\w+:|^)\/\//, '')
if (ruleset.startsWith('www.')) { ruleset = ruleset.split('www.')[1]; }
return ruleset
}).filter(ruleset => (ruleset != "|1" && ruleset != ""));
}
function filterDuplicates(rules) {
let res = {};
for (let i = 0; i < rules.length; i++) {
res[rules[i].split("|")[0]] = rules[i].split("|")[1];
}
return Object.keys(res).map(key => key + "|" + res[key]);
}
// Saves options to chrome.storage
function save_options() {
var enabled = document.getElementById("enabled").checked;
var defaultSpeed = document.getElementById("defaultSpeed").value;
var domainRules = document.getElementById("domainRules").value.split("\n");
var urlRules = document.getElementById("urlRules").value.split("\n");
var playlistRules = document.getElementById("playlistRules").value.split("\n");
if (domainRules.length > 0) {
domainRules = trimProtocolsAndBlanks(domainRules).map(ruleset => [ruleset.split('/')[0].split("|")[0],ruleset.split('|')[1] ? ruleset.split('|')[1] : defaultSpeed].join("|"));
}
if (urlRules.length > 0) {
urlRules = trimProtocolsAndBlanks(urlRules).map(ruleset => [ruleset.split("|")[0],ruleset.split("|")[1] ? ruleset.split("|")[1] : defaultSpeed].join("|"));
}
if (playlistRules.length > 0) {
playlistRules = trimProtocolsAndBlanks(playlistRules).map(ruleset => {
let temp = ruleset.split("&list=");
if (temp.length > 1) { temp = "&list=" + [temp[1].split("&")[0].split("|")[0], temp[1].split("|")[1] ? temp[1].split("|")[1] : defaultSpeed].join("|"); }
else { temp = undefined; }
return temp;
}).filter(ruleset => ruleset != undefined);
}
domainRules = filterDuplicates(domainRules).join("\n");
urlRules = filterDuplicates(urlRules).join("\n");
playlistRules = filterDuplicates(playlistRules).join("\n");
document.getElementById("domainRules").value = domainRules;
document.getElementById("urlRules").value = urlRules;
document.getElementById("playlistRules").value = playlistRules;
updateTextBoxText(domainRules, urlRules, playlistRules);
chrome.storage.sync.set(
{
enabled: enabled,
defaultSpeed: defaultSpeed,
domainRules: domainRules,
urlRules: urlRules,
playlistRules: playlistRules
},
updateStatus("Options saved")
);
}
function updateTextBoxText(domainRules, urlRules, playlistRules) {
document.getElementById("domainRules").value = domainRules + "\n";
document.getElementById("urlRules").value = urlRules + "\n";
document.getElementById("playlistRules").value = playlistRules + "\n";
}
function updateStatus(message) {
var status = document.getElementById("status");
status.style.visibility = "visible";
status.textContent = message;
setTimeout(function () {
status.style.visibility = "hidden";
}, 2000);
}
// Restores options from chrome.storage
function restore_options() {
chrome.storage.sync.get(tcDefaults, function (storage) {
document.getElementById("enabled").checked = storage.enabled;
document.getElementById("defaultSpeed").value = storage.defaultSpeed;
updateTextBoxText(storage.domainRules, storage.urlRules, storage.playlistRules);
});
}
function restore_defaults() {
chrome.storage.sync.set(tcDefaults, function () {
restore_options();
// Update status to let user know options were saved.
updateStatus("Default options restored")
});
}
document.addEventListener("DOMContentLoaded", function () {
restore_options();
document.getElementById("save").addEventListener("click", save_options);
});