-
-
Notifications
You must be signed in to change notification settings - Fork 111
/
popup.js
186 lines (164 loc) · 5.5 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// Initialize variables
const defaultWhiteList = "defaultWhiteList";
const greyPrefix = "-Grey";
let hostUrl;
let activeTab;
let cookieStoreId;
let page = browser.extension.getBackgroundPage().exposedFunctions;
// Logs the error
function onError(error) {
console.error(`Error: ${error}`);
}
// Fills the popup page
function fillPopup(tabs) {
browser.storage.local.get("activeMode")
.then((items) => {
document.getElementById("activeModeSwitch").checked = items.activeMode;
return Promise.resolve();
}).catch(onError);
let notifyMessage = page.getNotifyMessage();
if (notifyMessage !== "") {
document.getElementById("notify").appendChild(document.createTextNode(notifyMessage));
}
// Fill the host site placeholder if it exists
activeTab = tabs[0];
if (!page.isAWebpage(activeTab.url)) {
return Promise.resolve();
}
hostUrl = page.getHostname(activeTab.url);
// Sets the checkbox depending on the if it exists in the set
cookieStoreId = tabs[0].cookieStoreId;
if (page.contextualIdentitiesEnabled) {
if(page.whiteList.hasHost(hostUrl, cookieStoreId)) {
document.getElementById("switchToWhiteList").checked = true;
} else if(page.whiteList.hasHost(hostUrl, cookieStoreId + greyPrefix)) {
document.getElementById("switchToGreyList").checked = true;
} else {
document.getElementById("switchToNoList").checked = true;
}
} else {
if(page.whiteList.hasHost(hostUrl, defaultWhiteList)) {
document.getElementById("switchToWhiteList").checked = true;
} else if(page.whiteList.hasHost(hostUrl, defaultWhiteList + greyPrefix)) {
document.getElementById("switchToGreyList").checked = true;
} else {
document.getElementById("switchToNoList").checked = true;
}
}
// hostUrl = page.extractMainDomain(hostUrl);
let hostPlaceholder = document.getElementById("hostwebsite");
// Append the favicon image of the host site to the beggining of the URL
if (activeTab.favIconUrl) {
let faviconImage = new Image();
faviconImage.src = activeTab.favIconUrl;
faviconImage.style.width = "1em";
faviconImage.style.height = "1em";
hostPlaceholder.appendChild(faviconImage);
}
// Sets the Host site placeholder
hostPlaceholder.appendChild(document.createTextNode(hostUrl));
if (page.contextualIdentitiesEnabled) {
let name = page.cache.getNameFromCookieID(cookieStoreId);
hostPlaceholder.appendChild(document.createTextNode(`\n(${name})`));
}
return Promise.resolve();
}
browser.tabs.query({
currentWindow: true,
active: true
})
.then(fillPopup)
.catch(onError);
function animateSuccess(element) {
element.classList.add("successAnimated");
setTimeout(() => {
element.classList.remove("successAnimated");
}, 1500);
}
function animateFailure(element) {
element.classList.add("failureAnimated");
setTimeout(() => {
element.classList.remove("failureAnimated");
}, 1500);
}
// Setting Click Handling
document.getElementById("settings").addEventListener("click", () => {
browser.runtime.openOptionsPage();
});
// Clear all history for a domain
document.getElementById("cookieCleanup").addEventListener("click", () => {
page.cleanupOperation(false, false);
animateSuccess(document.getElementById("cookieCleanup"));
});
document.getElementById("cookieCleanupIgnoreOpenTabs").addEventListener("click", () => {
page.cleanupOperation(true, false);
animateSuccess(document.getElementById("cookieCleanupIgnoreOpenTabs"));
});
// Clear all cookies for that domain
document.getElementById("clearCookiesForDomain").addEventListener("click", () => {
return browser.cookies.getAll({
domain: hostUrl,
storeId: cookieStoreId
})
.then((cookies) => {
if (cookies.length > 0) {
for (let i = 0; i < cookies.length; i++) {
let cookieDomain = page.prepareCookieDomain(cookies[i]) + cookies[i].path;
browser.cookies.remove({
url: cookieDomain,
name: cookies[i].name,
storeId: cookieStoreId
}).catch(onError);
}
page.statLog.incrementCounter(cookies.length);
animateSuccess(document.getElementById("clearCookiesForDomain"));
} else {
animateFailure(document.getElementById("clearCookiesForDomain"));
}
return Promise.resolve();
});
});
// Turns on or off active mode cookie cleaning
document.getElementById("activeModeSwitch").addEventListener("click", () => {
if (document.getElementById("activeModeSwitch").checked) {
browser.storage.local.set({activeMode: true});
page.enableActiveMode();
} else {
browser.storage.local.set({activeMode: false});
page.disableActiveMode();
}
});
// Radio button event Handling
document.getElementById("switchToNoList").addEventListener("click", () => {
console.log("Removed from list");
if (hostUrl !== undefined) {
if(page.contextualIdentitiesEnabled) {
page.whiteList.removeURLFromLists(hostUrl, cookieStoreId);
} else {
page.whiteList.removeURLFromLists(hostUrl, defaultWhiteList);
}
page.checkIfProtected(activeTab);
}
});
document.getElementById("switchToGreyList").addEventListener("click", () => {
console.log("Added to GreyList");
if (hostUrl !== undefined) {
if (page.contextualIdentitiesEnabled) {
page.whiteList.addURL(hostUrl, cookieStoreId + greyPrefix);
} else {
page.whiteList.addURL(hostUrl, defaultWhiteList + greyPrefix);
}
page.checkIfProtected(activeTab);
}
});
document.getElementById("switchToWhiteList").addEventListener("click", () => {
console.log("Added to WhiteList");
if (hostUrl !== undefined) {
if (page.contextualIdentitiesEnabled) {
page.whiteList.addURL(hostUrl, cookieStoreId);
} else {
page.whiteList.addURL(hostUrl, defaultWhiteList);
}
page.checkIfProtected(activeTab);
}
});