-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpopup.js
107 lines (83 loc) · 3.17 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
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
/* global document chrome */
const selectedMirrorKey = 'selectedMirror';
const onOffKey = 'active';
const mirrorNames = chrome.extension.getBackgroundPage().getAllMirrorsNames()
let selectedMirrorName;
function setSelectedMirror(mirrorName) {
chrome.extension.getBackgroundPage().setSelectedMirror(mirrorName);
}
function setOnOffTitle(value) {
let message = !value
? chrome.i18n.getMessage("clickOnButtonMessage")
: chrome.i18n.getMessage("clickOffButtonMessage");
document.getElementsByClassName('switch')[0].title = message;
}
document.addEventListener('DOMContentLoaded', () => {
document.getElementById('infoMirror').innerText = chrome.i18n.getMessage("dropdownText");
document.getElementById('infoMirror').href = chrome.i18n.getMessage("wikipediaMirrorUrl");
chrome.storage.local.get([selectedMirrorKey, onOffKey], (data) => {
selectedMirrorName = data[selectedMirrorKey];
const onoffbutton = document.getElementById('onoffbutton');
const dropdown = document.getElementById('dropdown');
onoffbutton.checked=data[onOffKey]
setOnOffTitle(onoffbutton.checked)
if (dropdown) {
// $FlowFixMe
mirrorNames.forEach((mirrorName) => {
// <input type="radio" name="gender" value="male" checked> Male<br>
const mirrorOption = document.createElement('input');
mirrorOption.type = 'radio';
mirrorOption.name = 'wiki';
mirrorOption.value = mirrorName;
mirrorOption.disabled = !onoffbutton.checked
const mirrorLabel = document.createElement('label');
mirrorLabel.innerHTML = mirrorName;
dropdown.appendChild(mirrorOption);
dropdown.appendChild(mirrorLabel);
dropdown.appendChild(document.createElement('br'));
});
onoffbutton.addEventListener('change', () => {
// Notify background of the update
chrome.storage.local.set({"active": onoffbutton.checked}, ()=> {
chrome.runtime.sendMessage({data:"refresh"})
})
setOnOffTitle(onoffbutton.checked)
dropdown.querySelectorAll("input").forEach((input)=>{
input.disabled = !onoffbutton.checked
})
})
dropdown.addEventListener('change', () => {
document.getElementsByName('wiki').forEach((radio)=> {
if(radio.checked) {
selectedMirrorName = radio.value;
setSelectedMirror(selectedMirrorName);
// $FlowFixMe
const data = {};
data[selectedMirrorKey] = radio.value;
chrome.storage.local.set(data, () => {
if (chrome.runtime.error) {
console.log(chrome.runtime.error);
}
});
}
})
});
dropdown.querySelectorAll("input").forEach((input)=>{
input.disabled = !onoffbutton.checked
})
}
if (!selectedMirrorName) {
[selectedMirrorName] = mirrorNames;
const mirrorData = {};
mirrorData[selectedMirrorKey] = selectedMirrorName;
chrome.storage.local.set(mirrorData);
}
dropdown.childNodes.forEach((mirrorOption) => {
if (mirrorOption.value === selectedMirrorName) {
// eslint-disable-next-line no-param-reassign
mirrorOption.checked='checked';
}
});
setSelectedMirror(selectedMirrorName);
});
});