-
Notifications
You must be signed in to change notification settings - Fork 10
/
pronounce.js
76 lines (68 loc) · 1.9 KB
/
pronounce.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
function onError(error) {}
function onGot(item) {
speechSynthesis.getVoices();
var voice = "Alex (en-US)";
if (item.voiceSelect) {
voice = item.voiceSelect.split(" ")[0];
}
document.ondblclick = async function () {
var sel =
(document.selection && document.selection.createRange().text) ||
(window.getSelection && window.getSelection().toString());
const regex = /[^\s]+/gm;
const found = sel.match(regex);
let voices = speechSynthesis.getVoices();
if (found && found.length == 1) {
let utterance = new SpeechSynthesisUtterance(sel);
for (i = 0; i < voices.length; i++) {
if (voices[i].name === voice) {
utterance.voice = voices[i];
lang = voices[i].lang;
}
}
speechSynthesis.speak(utterance);
addToList(sel,voice,lang);
}
};
}
function addToList(sel,voice) {
async function store(result) {
if (result.list == null) {
list = [];
} else {
list = JSON.parse(result.list);
}
if (!list.includes(sel)) {
list.push([sel,voice,lang]);
list = JSON.stringify(list);
browser.storage.sync.set({
list: list,
});
}
}
let list = browser.storage.sync.get("list");
list.then(store, onError);
}
function url_domain(data) {
var a = document.createElement("a");
a.href = data;
return a.hostname;
}
async function onGotBlacklist(result) {
blacklist = JSON.parse(result.blacklist);
current_url = window.location.href;
hostname = url_domain(current_url);
console.log(current_url);
if (!blacklist.includes(hostname)) {
let isOn = browser.storage.sync.get("isOn");
isOn.then(onGotIsOn, onError);
}
}
async function onGotIsOn(isOn) {
if (isOn.isOn == true) {
let getting = browser.storage.sync.get("voiceSelect");
getting.then(onGot, onError);
}
}
let blacklist = browser.storage.sync.get("blacklist");
blacklist.then(onGotBlacklist, onError);