-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
170 lines (150 loc) · 7.29 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
var counter = 0;
window.browser = (function () {
return window.msBrowser ||
window.browser ||
window.chrome;
})();
function handleTickInit(tick) {
//wait 1 sec before updating
//setTimeout(function () { }, 1000);
}
function updateCursorCounter(value) {
var tick = document.getElementsByClassName("tick");
for (let i = 0; i < tick.length; i++) {
tick[i].setAttribute("data-value", value);
}
counter = value;
}
async function askContentScriptForUserCount() {
try {
const [tab] = await browser.tabs.query({ active: true, lastFocusedWindow: true });
const response = await browser.tabs.sendMessage(tab.id, { type: "fetch-user-count" });
updateCursorCounter(response);
}
catch (e) {
// Handle the case where the content script was not injected
console.log("Error: Content script not injected.");
updateCursorCounter(0);
}
}
// Call the askContentScriptForUserCount function at regular intervals
askContentScriptForUserCount();
setInterval(askContentScriptForUserCount, 1000);
function isLocalNetworkURL(url) {
//remove the protocol
url = url.replace(/.*?:\/\//g, "");
//remove everything after the first slash or colon
url = url.replace(/\/.*|:.*/g, "");
// Regular expression to match IP addresses in the local network (private IP ranges)
const localNetworkRegex = /^(^127\.)|(^10\.)|(^172\.1[6-9]\.)|(^172\.2[0-9]\.)|(^172\.3[0-1]\.)|(^192\.168\.)/;
return localNetworkRegex.test(url);
}
getBlacklistedStatus();
async function getBlacklistedStatus() {
const [tab] = await browser.tabs.query({ active: true, lastFocusedWindow: true });
const URL = tab.url;
var blacklist = await browser.storage.local.get(["cursors.blacklist"]);
console.log("blacklist: " + blacklist["cursors.blacklist"]);
if (blacklist["cursors.blacklist"].includes(URL)) {
console.log("blacklist contains url, setting to blacklisted");
document.getElementById("switchDefaultState").id = "switchBlacklisted";
} else if (URL.startsWith("http://") || URL.startsWith("https://") && !isLocalNetworkURL(URL)) {
console.log("blacklist does not contain url, but url is valid, setting to whitelisted");
document.getElementById("switchDefaultState").id = "switchWhitelisted";
} else {
console.log("blacklist does not contain url, but url is not valid, setting to disabled");
document.getElementById("switchDefaultState").id = "switchBlacklisted";
}
}
async function blackwhitelistWebsite() {
// First, get the active tab to ensure the content script is injected
// Second, look if the url is already in the blacklist by checking the local storage
// Third, if it is in the blacklist, remove it from the blacklist
// Fourth, if it is not in the blacklist, add it to the blacklist
const [tab] = await browser.tabs.query({ active: true, lastFocusedWindow: true });
const URL = tab.url;
var blacklist = await browser.storage.local.get(["cursors.blacklist"]);
var changed = false;
if (blacklist["cursors.blacklist"].includes(URL)) {
// Remove the url from the blacklist
var index = blacklist["cursors.blacklist"].indexOf(URL);
if (index > -1) {
blacklist["cursors.blacklist"].splice(index, 1);
}
await browser.storage.local.set({ "cursors.blacklist": blacklist["cursors.blacklist"] });
document.getElementById("switchBlacklisted").id = "switchWhitelisted";
changed = true;
} else if ((!URL.startsWith("http://") && !URL.startsWith("https://")) || isLocalNetworkURL(URL)) {
// Do nothing
} else {
// Add the url to the blacklist
blacklist["cursors.blacklist"].push(URL);
await browser.storage.local.set({ "cursors.blacklist": blacklist["cursors.blacklist"] });
document.getElementById("switchWhitelisted").id = "switchBlacklisted";
changed = true;
}
if (changed) {
document.getElementById("refreshButton").style.display = "flex";
document.getElementsByClassName("switchContainer")[0].style.justifyContent = "right";
}
}
var blacklistToggleButton = document.getElementById("switchButton");
blacklistToggleButton.addEventListener("click", blackwhitelistWebsite);
var refreshButton = document.getElementById("refreshButton");
refreshButton.addEventListener("click", async function () {
//refrsh the page
const [tab] = await browser.tabs.query({ active: true, lastFocusedWindow: true });
browser.tabs.reload(tab.id);
window.close();
});
var customizationButtons = document.getElementsByClassName("customizationElement");
for (let i = 0; i < customizationButtons.length; i++) {
image = customizationButtons[i].children[0];
imageSource = image.src; //browser-extension://mnaknebffpoaobpghjjpmlpdpjmnbfnc/customization/cursors/0.png
imageSource = imageSource.substring(imageSource.lastIndexOf("/") + 1, imageSource.lastIndexOf("."));
customizationButtons[i].addEventListener("click", applyCustomization.bind(null, imageSource));
}
function applyCustomization(id) {
console.log("applyCustomization: " + id);
//Write the skinId to the local storage
browser.storage.local.set({ "cursors.customization.skinId": id }, function () {
if (browser.runtime.lastError) {
// Catch the error here
console.log("Error: " + browser.runtime.lastError);
}
document.getElementById("refreshButton").style.display = "flex";
document.getElementsByClassName("switchContainer")[0].style.justifyContent = "right";
console.log("Set skinId to " + id);
});
}
function getActiveSkin() {
browser.storage.local.get("cursors.customization.skinId", function (result) {
if (result["cursors.customization.skinId"]) {
var skinId = result["cursors.customization.skinId"];
if (skinId) {
console.log("getActiveSkin: skinId: " + skinId);
var customizationButtons = document.getElementsByClassName("customizationElement");
for (let i = 0; i < customizationButtons.length; i++) {
image = customizationButtons[i].children[0];
imageSource = image.src; //browser-extension://mnaknebffpoaobpghjjpmlpdpjmnbfnc/customization/cursors/0.png
imageSource = imageSource.substring(imageSource.lastIndexOf("/") + 1, imageSource.lastIndexOf("."));
if (imageSource == skinId) {
customizationButtons[i].classList.add("active");
}
}
}
} else {
console.log("getActiveSkin: skinId does not exist therefore the default skin is active");
var customizationButtons = document.getElementsByClassName("customizationElement");
for (let i = 0; i < customizationButtons.length; i++) {
image = customizationButtons[i].children[0];
imageSource = image.src; //browser-extension://mnaknebffpoaobpghjjpmlpdpjmnbfnc/customization/cursors/0.png
imageSource = imageSource.substring(imageSource.lastIndexOf("/") + 1, imageSource.lastIndexOf("."));
if (imageSource == 0) {
customizationButtons[i].classList.add("active");
}
}
}
});
}
getActiveSkin();