-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpopup.js
69 lines (65 loc) · 1.86 KB
/
popup.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
function close_page() {
setTimeout(() => {
window.close();
}, 200);
}
function get_saved_data() {
chrome.storage.sync.get({ lang: "", fontsize: 75 }).then((result) => {
let lang = result.lang;
let fontsize = result.fontsize;
if (lang) {
document.getElementById("lang").value = lang;
}
if (fontsize) {
document.getElementById("fontsize").value = fontsize;
}
});
}
function some_error(target) {
if (target) {
target.style.background = "red";
target.title = "Error. Are you sure this is the Coursera tab?";
}
}
function ok_state(target) {
if (target) {
target.style.background = "blue";
target.title = "All translated, closing now...";
}
}
document.addEventListener(
"DOMContentLoaded",
function () {
get_saved_data();
let checkButton = document.getElementById("translateBtn");
checkButton.addEventListener(
"click",
function () {
let lang = document.getElementById("lang").value;
let fontsize = document.getElementById("fontsize").value;
chrome.storage.sync.set({ lang: lang, fontsize: fontsize }).then(() => {
console.log("Value is set to " + lang, fontsize);
});
chrome.tabs.query({ active: true, currentWindow: true }).then((tabs) => {
chrome.tabs
.sendMessage(tabs[0].id, { method: "translate" })
.then((response) => {
if (response?.method && response.method == "translated") {
ok_state(checkButton);
close_page();
} else {
console.log("Answer not from target page?");
some_error(checkButton);
}
})
.catch(() => {
console.log("Not connected target page");
some_error(checkButton);
});
});
},
false
);
},
false
);