Skip to content

Commit 2a56392

Browse files
committed
Updated to manifest V3
1 parent 9973ebb commit 2a56392

File tree

9 files changed

+220
-142
lines changed

9 files changed

+220
-142
lines changed

_locales/en/messages.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,4 +420,4 @@
420420
"editThis_SameSite_Strict": {
421421
"message": "Strict"
422422
}
423-
}
423+
}

js/background.js

Lines changed: 115 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,92 @@
1+
importScripts('utils.js');
2+
3+
// Initialize global variables
4+
var data = {
5+
lastVersionRun: null,
6+
readOnly: [],
7+
filters: [],
8+
nCookiesProtected: 0,
9+
nCookiesFlagged: 0,
10+
nCookiesShortened: 0
11+
};
12+
13+
// Initialize global preferences
14+
var preferences = {
15+
showContextMenu: true,
16+
showChristmasIcon: false,
17+
useMaxCookieAge: false,
18+
maxCookieAgeType: 0
19+
};
20+
121
var showContextMenu = undefined;
222

3-
updateCallback = function () {
23+
// Load preferences and data from Chrome storage
24+
chrome.storage.local.get(['lastVersionRun', 'readOnly', 'filters', 'preferences'], function (items) {
25+
data.lastVersionRun = items.lastVersionRun || null;
26+
data.readOnly = items.readOnly || [];
27+
data.filters = items.filters || [];
28+
preferences = items.preferences || preferences;
29+
30+
showContextMenu = preferences.showContextMenu;
31+
32+
var currentVersion = chrome.runtime.getManifest().version;
33+
var oldVersion = data.lastVersionRun;
34+
35+
data.lastVersionRun = currentVersion;
36+
chrome.storage.local.set({ lastVersionRun: currentVersion });
37+
38+
if (oldVersion !== currentVersion) {
39+
if (oldVersion === null || oldVersion === undefined) {
40+
// Is firstrun
41+
chrome.tabs.create({ url: 'http://www.editthiscookie.com/start/' });
42+
} else {
43+
chrome.notifications.onClicked.addListener(function (notificationId) {
44+
chrome.tabs.create({
45+
url: 'http://www.editthiscookie.com/changelog/'
46+
});
47+
chrome.notifications.clear(notificationId, function () {});
48+
});
49+
var opt = {
50+
type: "basic",
51+
title: "EditThisCookie",
52+
message: _getMessage("updated"),
53+
iconUrl: "/img/icon_128x128.png",
54+
isClickable: true
55+
};
56+
chrome.notifications.create("", opt, function () {});
57+
}
58+
}
59+
60+
updateCallback();
61+
});
62+
63+
function updateCallback() {
464
if (showContextMenu !== preferences.showContextMenu) {
565
showContextMenu = preferences.showContextMenu;
666
setContextMenu(showContextMenu);
767
}
868
setChristmasIcon();
9-
};
10-
11-
setChristmasIcon();
12-
setInterval(setChristmasIcon, 60 * 60 * 1000); //Every hour
13-
14-
//Every time the browser restarts the first time the user goes to the options he ends up in the default page (support)
15-
localStorage.setItem("option_panel", "null");
69+
}
1670

17-
var currentVersion = chrome.runtime.getManifest().version;
18-
var oldVersion = data.lastVersionRun;
71+
function setChristmasIcon() {
72+
if (isChristmasPeriod() && preferences.showChristmasIcon) {
73+
chrome.action.setIcon({ path: "/img/cookie_xmas_19x19.png" });
74+
} else {
75+
chrome.action.setIcon({ path: "/img/icon_19x19.png" });
76+
}
77+
}
1978

20-
data.lastVersionRun = currentVersion;
79+
setChristmasIcon();
80+
setInterval(setChristmasIcon, 60 * 60 * 1000);
2181

22-
if (oldVersion !== currentVersion) {
23-
if (oldVersion === undefined) { //Is firstrun
24-
chrome.tabs.create({ url: 'http://www.editthiscookie.com/start/' });
82+
// Every time the browser restarts, the first time the user goes to the options he ends up in the default page (support)
83+
chrome.storage.local.set({ option_panel: "null" }, function () {
84+
if (chrome.runtime.lastError) {
85+
console.error("Error setting option_panel: ", chrome.runtime.lastError);
2586
} else {
26-
chrome.notifications.onClicked.addListener(function (notificationId) {
27-
chrome.tabs.create({
28-
url: 'http://www.editthiscookie.com/changelog/'
29-
});
30-
chrome.notifications.clear(notificationId, function (wasCleared) { });
31-
});
32-
var opt = {
33-
type: "basic",
34-
title: "EditThisCookie",
35-
message: _getMessage("updated"),
36-
iconUrl: "/img/icon_128x128.png",
37-
isClickable: true
38-
};
39-
chrome.notifications.create("", opt, function (notificationId) {
40-
});
87+
console.log("option_panel set to null");
4188
}
42-
}
89+
});
4390

4491
setContextMenu(preferences.showContextMenu);
4592

@@ -48,24 +95,18 @@ chrome.cookies.onChanged.addListener(function (changeInfo) {
4895
var cookie = changeInfo.cookie;
4996
var cause = changeInfo.cause;
5097

51-
var name = cookie.name;
52-
var domain = cookie.domain;
53-
var value = cookie.value;
54-
55-
if (cause === "expired" || cause === "evicted")
56-
return;
98+
if (cause === "expired" || cause === "evicted") return;
5799

58100
for (var i = 0; i < data.readOnly.length; i++) {
59101
var currentRORule = data.readOnly[i];
60102
if (compareCookies(cookie, currentRORule)) {
61103
if (removed) {
62104
chrome.cookies.get({
63-
'url': "http" + ((currentRORule.secure) ? "s" : "") + "://" + currentRORule.domain + currentRORule.path,
64-
'name': currentRORule.name,
65-
'storeId': currentRORule.storeId
105+
url: "http" + ((currentRORule.secure) ? "s" : "") + "://" + currentRORule.domain + currentRORule.path,
106+
name: currentRORule.name,
107+
storeId: currentRORule.storeId
66108
}, function (currentCookie) {
67-
if (compareCookies(currentCookie, currentRORule))
68-
return;
109+
if (compareCookies(currentCookie, currentRORule)) return;
69110
var newCookie = cookieForCreationFromFullCookie(currentRORule);
70111
chrome.cookies.set(newCookie);
71112
++data.nCookiesProtected;
@@ -75,55 +116,54 @@ chrome.cookies.onChanged.addListener(function (changeInfo) {
75116
}
76117
}
77118

78-
//Check if a blocked cookie was added
79119
if (!removed) {
80120
for (var i = 0; i < data.filters.length; i++) {
81121
var currentFilter = data.filters[i];
82-
if (filterMatchesCookie(currentFilter, name, domain, value)) {
83-
chrome.tabs.query(
84-
{ active: true },
85-
function (tabs) {
86-
var url = tabs[0].url;
87-
var toRemove = {};
88-
toRemove.url = url;
89-
toRemove.url = "http" + ((cookie.secure) ? "s" : "") + "://" + cookie.domain + cookie.path;
90-
toRemove.name = name;
91-
chrome.cookies.remove(toRemove);
92-
++data.nCookiesFlagged;
93-
});
122+
if (filterMatchesCookie(currentFilter, cookie.name, cookie.domain, cookie.value)) {
123+
chrome.tabs.query({ active: true }, function (tabs) {
124+
var toRemove = {
125+
url: "http" + (cookie.secure ? "s" : "") + "://" + cookie.domain + cookie.path,
126+
name: cookie.name
127+
};
128+
chrome.cookies.remove(toRemove);
129+
++data.nCookiesFlagged;
130+
});
94131
}
95132
}
96133
}
97134

98-
if (!removed && preferences.useMaxCookieAge && preferences.maxCookieAgeType > 0) { //Check expiration, if too far in the future shorten on user's preference
99-
var maxAllowedExpiration = Math.round((new Date).getTime() / 1000) + (preferences.maxCookieAge * preferences.maxCookieAgeType);
135+
if (!removed && preferences.useMaxCookieAge && preferences.maxCookieAgeType > 0) {
136+
var maxAllowedExpiration = Math.round(Date.now() / 1000) + (preferences.maxCookieAge * preferences.maxCookieAgeType);
100137
if (cookie.expirationDate !== undefined && cookie.expirationDate > maxAllowedExpiration + 60) {
101138
var newCookie = cookieForCreationFromFullCookie(cookie);
102-
if (!cookie.session)
103-
newCookie.expirationDate = maxAllowedExpiration;
139+
if (!cookie.session) newCookie.expirationDate = maxAllowedExpiration;
104140
chrome.cookies.set(newCookie);
105141
++data.nCookiesShortened;
106142
}
107143
}
108144
});
109145

110146
function setContextMenu(show) {
111-
chrome.contextMenus.removeAll();
112-
if (show) {
113-
chrome.contextMenus.create({
114-
"title": "EditThisCookie",
115-
"contexts": ["page"],
116-
"onclick": function (info, tab) {
117-
showPopup(info, tab);
118-
}
119-
});
120-
}
121-
}
147+
chrome.contextMenus.removeAll(function() {
148+
if (chrome.runtime.lastError) {
149+
console.error("Error removing context menus: ", chrome.runtime.lastError);
150+
} else {
151+
console.log("Context menus removed");
152+
}
122153

123-
function setChristmasIcon() {
124-
if (isChristmasPeriod() && preferences.showChristmasIcon) {
125-
chrome.browserAction.setIcon({ "path": "/img/cookie_xmas_19x19.png" });
126-
} else {
127-
chrome.browserAction.setIcon({ "path": "/img/icon_19x19.png" });
128-
}
154+
if (show) {
155+
chrome.contextMenus.create({
156+
id: "editThisCookie", // Unique identifier for the context menu item
157+
title: "EditThisCookie",
158+
contexts: ["page"]
159+
}, function() {
160+
if (chrome.runtime.lastError) {
161+
console.error("Error creating context menu: ", chrome.runtime.lastError);
162+
} else {
163+
console.log("Context menu created");
164+
}
165+
});
166+
}
167+
});
129168
}
169+

js/options_main_page.js

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,38 @@
1-
var panel = JSON.parse(localStorage.getItem("option_panel"));
2-
var arguments = getUrlVars();
3-
var element;
4-
5-
if (panel === "null" || panel === null || panel === undefined) {
6-
element = "support";
7-
} else {
8-
element = panel;
1+
// Function to get URL variables
2+
function getUrlVars() {
3+
var vars = {};
4+
window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m, key, value) {
5+
vars[key] = value;
6+
});
7+
return vars;
98
}
109

11-
if (arguments.page !== undefined) {
12-
element = arguments.page;
10+
// Function to redirect based on the panel state
11+
function redirectBasedOnPanel(panel) {
12+
var arguments = getUrlVars();
13+
var element;
14+
15+
if (panel === "null" || panel === null || panel === undefined) {
16+
element = "support";
17+
} else {
18+
element = panel;
19+
}
20+
21+
if (arguments.page !== undefined) {
22+
element = arguments.page;
23+
}
24+
25+
location.href = "/options_pages/" + element + ".html";
1326
}
1427

15-
location.href = "/options_pages/" + element + ".html";
28+
// Get the 'option_panel' value from chrome.storage
29+
chrome.storage.local.get("option_panel", function(result) {
30+
if (chrome.runtime.lastError) {
31+
console.error("Error getting option_panel: ", chrome.runtime.lastError);
32+
// In case of error, default to 'support'
33+
redirectBasedOnPanel("support");
34+
} else {
35+
// Call the redirect function with the retrieved panel value
36+
redirectBasedOnPanel(result.option_panel);
37+
}
38+
});

js/popup.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -217,14 +217,16 @@ function createAccordionList(cks, callback, callbackArguments) {
217217
let createAccordionCallback = callback;
218218
let createAccordionCallbackArguments = callbackArguments;
219219

220-
try {
221-
$("#cookiesList").accordion("destroy");
222-
} catch (e) {
223-
console.warn(e.message)
220+
// Check if the accordion is initialized before attempting to destroy it
221+
if ($("#cookiesList").hasClass("ui-accordion")) {
222+
try {
223+
$("#cookiesList").accordion("destroy");
224+
} catch (e) {
225+
console.warn(e.message);
226+
}
224227
}
225228

226-
if (cks === null)
227-
cks = cookieList;
229+
if (cks === null) cks = cookieList;
228230
for (var i = 0; i < cks.length; i++) {
229231
currentC = cks[i];
230232

@@ -305,6 +307,7 @@ function createAccordionList(cks, callback, callbackArguments) {
305307
});
306308
}
307309

310+
308311
function importCookies() {
309312
var nCookiesImportedThisTime = 0;
310313
var text = $(".value", "#pasteCookie").val();

js/utils.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,24 +137,25 @@ function showPopup(info, tab) {
137137
var tabID = encodeURI(tab.id);
138138
var tabIncognito = encodeURI(tab.incognito);
139139

140-
var urlToOpen = chrome.extension.getURL("popup.html") + "?url=" + tabUrl + "&id=" + tabID + "&incognito=" + tabIncognito;
140+
var urlToOpen = chrome.runtime.getURL("popup.html") + "?url=" + tabUrl + "&id=" + tabID + "&incognito=" + tabIncognito;
141141

142142
chrome.tabs.query({ 'windowId': chrome.windows.WINDOW_ID_CURRENT }, function (tabList) {
143143
for (var x = 0; x < tabList.length; x++) {
144144
var cTab = tabList[x];
145145
if (cTab.url.indexOf(urlToOpen) === 0) {
146146
chrome.tabs.update(cTab.id, {
147-
'selected': true
147+
'active': true // Updated from 'selected' to 'active'
148148
});
149-
return
149+
return;
150150
}
151151
}
152152
chrome.tabs.create({
153153
'url': urlToOpen
154-
})
155-
})
154+
});
155+
});
156156
}
157157

158+
158159
function copyToClipboard(text) {
159160
if (text === undefined)
160161
return;

0 commit comments

Comments
 (0)