-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
130 lines (125 loc) · 4.78 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
function updateCache(setting, newValue) {
// Send a message to update the cache in background.js
chrome.runtime.sendMessage({
action: "updateCache",
setting: setting,
value: newValue
});
}
function checkAllDuplicates() {
// Send a message to update the cache in background.js
chrome.runtime.sendMessage({
action: "checkAllDuplicates"
});
}
document.addEventListener("DOMContentLoaded", function () {
const extensionEnableCheckbox = document.getElementById("extensionEnableCheckbox");
const stripCheckbox = document.getElementById("stripCheckbox");
const anchorsCheckbox = document.getElementById("anchorsCheckbox");
const switchToOriginalTabCheckbox = document.getElementById("switchToOriginalTabCheckbox");
const ignoreWebsiteButton = document.getElementById("ignoreWebsiteButton");
const clearWebsitesButton = document.getElementById("clearWebsitesButton");
// Load the saved settings when the popup is opened
chrome.storage.sync.get(["extensionEnabled"], function (result) {
if (result.extensionEnabled !== undefined) {
extensionEnableCheckbox.checked = result.extensionEnabled;
}
});
chrome.storage.sync.get(["ignoreQueryStrings"], function (result) {
if (result.ignoreQueryStrings !== undefined) {
stripCheckbox.checked = result.ignoreQueryStrings;
}
});
chrome.storage.sync.get(["ignoreAnchorTags"], function (result) {
if (result.ignoreAnchorTags !== undefined) {
anchorsCheckbox.checked = result.ignoreAnchorTags;
}
});
chrome.storage.sync.get(["switchToOriginalTab"], function (result) {
if (result.switchToOriginalTab !== undefined) {
switchToOriginalTabCheckbox.checked = result.switchToOriginalTab;
}
});
chrome.storage.sync.get(["ignoreWebsite"], function (result) {
if (result.ignoreWebsite !== undefined) {
ignoreWebsiteButton.enabled = result.ignoreWebsite;
}
});
chrome.storage.sync.get(["ignoredWebsites"], function (result) {
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
const currentUrl = tabs[0].url;
const ignoredWebsites = result.ignoredWebsites || [];
const index = ignoredWebsites.indexOf(currentUrl);
if (index === -1) {
ignoreWebsiteButton.innerText = "Exclude Website";
} else {
ignoreWebsiteButton.innerText = "Include Website";
}
});
});
// Add `change` event handlers
extensionEnableCheckbox.addEventListener("change", function () {
const newValue = this.checked;
chrome.storage.sync.set({ "extensionEnabled": newValue }, function () {
updateCache("extensionEnabled", newValue);
console.log("[SETTING]: extensionEnabled ->", newValue);
if (newValue === true)
checkAllDuplicates();
});
});
stripCheckbox.addEventListener("change", function () {
const newValue = this.checked;
chrome.storage.sync.set({ "ignoreQueryStrings": newValue }, function () {
updateCache("ignoreQueryStrings", newValue);
console.log("[SETTING]: ignoreQueryStrings ->", newValue);
});
});
anchorsCheckbox.addEventListener("change", function () {
const newValue = this.checked;
chrome.storage.sync.set({ "ignoreAnchorTags": newValue }, function () {
updateCache("ignoreAnchorTags", newValue);
console.log("[SETTING]: ignoreAnchorTags ->", newValue);
});
});
switchToOriginalTabCheckbox.addEventListener("change", function () {
const newValue = this.checked;
chrome.storage.sync.set({ "switchToOriginalTab": newValue }, function () {
updateCache("switchToOriginalTab", newValue);
console.log("[SETTING]: switchToOriginalTab ->", newValue);
});
});
ignoreWebsiteButton.addEventListener("click", function () {
const button = this;
chrome.storage.sync.get(["ignoredWebsites"], function (result) {
let ignoredWebsites = result.ignoredWebsites || [];
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
const currentUrl = tabs[0].url;
const index = ignoredWebsites.indexOf(currentUrl);
if (index === -1) {
ignoredWebsites.push(currentUrl);
button.innerText = "Include Website";
} else {
ignoredWebsites.splice(index, 1);
button.innerText = "Exclude Website";
}
chrome.storage.sync.set({ "ignoredWebsites": ignoredWebsites }, function () {
updateCache("ignoreWebsite", ignoredWebsites);
console.log("ignoredWebsites ->\n", ignoredWebsites.join(",\n"));
});
});
});
});
clearWebsitesButton.addEventListener("click", function () {
chrome.storage.sync.set({ "ignoredWebsites": [] }, function () {
updateCache("ignoreWebsite", []);
console.log("ignoredWebsites ->\n", []);
});
ignoreWebsiteButton.innerText = "Exclude Website";
clearWebsitesButton.innerText = "Cleared!";
clearWebsitesButton.style.backgroundColor = "#ed3f54";
setTimeout(function () {
clearWebsitesButton.innerText = "Clear Excluded Websites";
clearWebsitesButton.style.backgroundColor = "";
}, 2000);
});
});