-
Notifications
You must be signed in to change notification settings - Fork 10
/
options.js
140 lines (119 loc) · 3.7 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
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
async function getPage() {}
function populateVoiceList() {
if (typeof speechSynthesis === "undefined") {
return;
}
var voices = speechSynthesis.getVoices();
for (var i = 0; i < voices.length; i++) {
var option = document.createElement("option");
option.textContent = voices[i].name + " (" + voices[i].lang + ")";
if (voices[i].default) {
option.textContent += " -- DEFAULT";
}
option.setAttribute("data-lang", voices[i].lang);
option.setAttribute("data-name", voices[i].name);
document.getElementById("voiceSelect").appendChild(option);
}
}
populateVoiceList();
if (
typeof speechSynthesis !== "undefined" &&
speechSynthesis.onvoiceschanged !== undefined
) {
speechSynthesis.onvoiceschanged = populateVoiceList;
}
function saveOptions(e) {
e.preventDefault();
browser.storage.sync.set({
voiceSelect: document.querySelector("#voiceSelect").value,
isOn: document.querySelector("#isOn").checked
});
browser.runtime.reload();
window.close();
}
function url_domain(data) {
var a = document.createElement("a");
a.href = data;
return a.hostname;
}
function addToBlacklist(e) {
async function store(result) {
if (result.blacklist == null){
blacklist = [""];
}
else{
blacklist = JSON.parse(result.blacklist);
}
current_url = await browser.tabs
.query({ currentWindow: true, active: true })
.then((tabs) => {
return tabs[0].url;
});
hostname = url_domain(current_url);
if (!blacklist.includes(hostname)) {
blacklist.push(hostname);
document.querySelector("#blacklist").innerHTML = 'de-blacklist';
}
else {
index = blacklist.indexOf(hostname);
if (index > -1){
blacklist.splice(index,1);
}
document.querySelector("#blacklist").innerHTML = 'blacklist';
}
blacklist = JSON.stringify(blacklist);
e.preventDefault();
browser.storage.sync.set({
blacklist: blacklist,
});
browser.runtime.reload();
window.close();
}
function onError(error) {
browser.storage.sync.set({
blacklist: JSON.stringify([""]),
});
}
let blacklist = browser.storage.sync.get("blacklist");
blacklist.then(store, onError);
}
function restoreOptions() {
function setCurrentChoice(result) {
document.querySelector("#voiceSelect").value =
result.voiceSelect || "en-US";
}
function setIsOn(result) {
document.querySelector("#isOn").checked = result.isOn || false;
}
async function setBlacklist(result) {
blacklist = JSON.parse(result.blacklist);
current_url = await browser.tabs
.query({ currentWindow: true, active: true })
.then((tabs) => {
return tabs[0].url;
});
hostname = url_domain(current_url);
if (blacklist.includes(hostname)){
document.querySelector("#blacklist").innerHTML = 'deblacklist';
}
else{
document.querySelector("#blacklist").innerHTML = 'blacklist';
}
}
function onError(error) {}
let getting = browser.storage.sync.get("voiceSelect");
getting.then(setCurrentChoice, onError);
let isOn = browser.storage.sync.get("isOn");
isOn.then(setIsOn, onError);
let blacklist = browser.storage.sync.get("blacklist");
blacklist.then(setBlacklist, onError);
}
var isChrome =
/Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor);
if (!isChrome) {
document.querySelector("#chromeText").remove();
}
document.querySelector("#voiceSelect").addEventListener("change", saveOptions);
document.querySelector("#isOn").addEventListener("change", saveOptions);
document.querySelector("#blacklist").addEventListener("click", addToBlacklist);
document.addEventListener("DOMContentLoaded", restoreOptions);