-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpopup.js
More file actions
57 lines (52 loc) · 1.81 KB
/
popup.js
File metadata and controls
57 lines (52 loc) · 1.81 KB
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
(async() => {
async function getCurrentTab() {
let queryOptions = { active: true, lastFocusedWindow: true };
let [tab] = await chrome.tabs.query(queryOptions);
return tab;
}
const tab = await getCurrentTab();
const domain = new URL(tab.url).hostname;
document.getElementById("domain").textContent = domain;
let domains = await chrome.storage.local.get(["blocked"])
domains = domains.blocked ? domains.blocked : []
let isBlocked = domains.includes(domain)
const button = document.getElementById("toggle");
button.textContent = isBlocked ? 'OFF' : 'ON';
const footer = document.getElementById("footer");
if(isBlocked) {
button.classList.add("is-blocked");
}
button.addEventListener("click", async() => {
if(isBlocked) {
button.textContent = 'ON';
button.classList.remove("is-blocked");
button.title = '在当前域名下关闭';
domains = domains.filter(i => i !== domain);
} else {
button.textContent = 'OFF';
button.classList.add("is-blocked");
button.title = '在当前域名下启用';
domains = [...domains, domain];
}
if(!footer.querySelector("#refresh-btn")) {
const refreshBtn = document.createElement("button");
refreshBtn.id = "refresh-btn";
refreshBtn.textContent = "重新加载";
refreshBtn.classList.add("is-text");
refreshBtn.addEventListener("click", () => {
chrome.tabs.reload();
});
footer.appendChild(refreshBtn);
}
await chrome.storage.local.set({"blocked": domains});
isBlocked = !isBlocked;
});
document.getElementById("go-options").addEventListener("click", (e) => {
e.preventDefault();
if (chrome.runtime.openOptionsPage) {
chrome.runtime.openOptionsPage();
} else {
window.open(chrome.runtime.getURL('options.html'));
}
});
})();