Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: update and support for chrome manifest version 3 #373

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
191 changes: 116 additions & 75 deletions js/background.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,92 @@
importScripts('utils.js');

// Initialize global variables
var data = {
lastVersionRun: null,
readOnly: [],
filters: [],
nCookiesProtected: 0,
nCookiesFlagged: 0,
nCookiesShortened: 0
};

// Initialize global preferences
var preferences = {
showContextMenu: true,
showChristmasIcon: false,
useMaxCookieAge: false,
maxCookieAgeType: 0
};

var showContextMenu = undefined;

updateCallback = function () {
// Load preferences and data from Chrome storage
chrome.storage.local.get(['lastVersionRun', 'readOnly', 'filters', 'preferences'], function (items) {
data.lastVersionRun = items.lastVersionRun || null;
data.readOnly = items.readOnly || [];
data.filters = items.filters || [];
preferences = items.preferences || preferences;

showContextMenu = preferences.showContextMenu;

var currentVersion = chrome.runtime.getManifest().version;
var oldVersion = data.lastVersionRun;

data.lastVersionRun = currentVersion;
chrome.storage.local.set({ lastVersionRun: currentVersion });

if (oldVersion !== currentVersion) {
if (oldVersion === null || oldVersion === undefined) {
// Is firstrun
chrome.tabs.create({ url: 'http://www.editthiscookie.com/start/' });
} else {
chrome.notifications.onClicked.addListener(function (notificationId) {
chrome.tabs.create({
url: 'http://www.editthiscookie.com/changelog/'
});
chrome.notifications.clear(notificationId, function () {});
});
var opt = {
type: "basic",
title: "EditThisCookie",
message: _getMessage("updated"),
iconUrl: "/img/icon_128x128.png",
isClickable: true
};
chrome.notifications.create("", opt, function () {});
}
}

updateCallback();
});

function updateCallback() {
if (showContextMenu !== preferences.showContextMenu) {
showContextMenu = preferences.showContextMenu;
setContextMenu(showContextMenu);
}
setChristmasIcon();
};

setChristmasIcon();
setInterval(setChristmasIcon, 60 * 60 * 1000); //Every hour

//Every time the browser restarts the first time the user goes to the options he ends up in the default page (support)
localStorage.setItem("option_panel", "null");
}

var currentVersion = chrome.runtime.getManifest().version;
var oldVersion = data.lastVersionRun;
function setChristmasIcon() {
if (isChristmasPeriod() && preferences.showChristmasIcon) {
chrome.action.setIcon({ path: "/img/cookie_xmas_19x19.png" });
} else {
chrome.action.setIcon({ path: "/img/icon_19x19.png" });
}
}

data.lastVersionRun = currentVersion;
setChristmasIcon();
setInterval(setChristmasIcon, 60 * 60 * 1000);

if (oldVersion !== currentVersion) {
if (oldVersion === undefined) { //Is firstrun
chrome.tabs.create({ url: 'http://www.editthiscookie.com/start/' });
// Every time the browser restarts, the first time the user goes to the options he ends up in the default page (support)
chrome.storage.local.set({ option_panel: "null" }, function () {
if (chrome.runtime.lastError) {
console.error("Error setting option_panel: ", chrome.runtime.lastError);
} else {
chrome.notifications.onClicked.addListener(function (notificationId) {
chrome.tabs.create({
url: 'http://www.editthiscookie.com/changelog/'
});
chrome.notifications.clear(notificationId, function (wasCleared) { });
});
var opt = {
type: "basic",
title: "EditThisCookie",
message: _getMessage("updated"),
iconUrl: "/img/icon_128x128.png",
isClickable: true
};
chrome.notifications.create("", opt, function (notificationId) {
});
console.log("option_panel set to null");
}
}
});

setContextMenu(preferences.showContextMenu);

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

var name = cookie.name;
var domain = cookie.domain;
var value = cookie.value;

if (cause === "expired" || cause === "evicted")
return;
if (cause === "expired" || cause === "evicted") return;

for (var i = 0; i < data.readOnly.length; i++) {
var currentRORule = data.readOnly[i];
if (compareCookies(cookie, currentRORule)) {
if (removed) {
chrome.cookies.get({
'url': "http" + ((currentRORule.secure) ? "s" : "") + "://" + currentRORule.domain + currentRORule.path,
'name': currentRORule.name,
'storeId': currentRORule.storeId
url: "http" + ((currentRORule.secure) ? "s" : "") + "://" + currentRORule.domain + currentRORule.path,
name: currentRORule.name,
storeId: currentRORule.storeId
}, function (currentCookie) {
if (compareCookies(currentCookie, currentRORule))
return;
if (compareCookies(currentCookie, currentRORule)) return;
var newCookie = cookieForCreationFromFullCookie(currentRORule);
chrome.cookies.set(newCookie);
++data.nCookiesProtected;
Expand All @@ -75,55 +116,55 @@ chrome.cookies.onChanged.addListener(function (changeInfo) {
}
}

//Check if a blocked cookie was added
if (!removed) {
for (var i = 0; i < data.filters.length; i++) {
var currentFilter = data.filters[i];
if (filterMatchesCookie(currentFilter, name, domain, value)) {
chrome.tabs.query(
{ active: true },
function (tabs) {
var url = tabs[0].url;
var toRemove = {};
toRemove.url = url;
toRemove.url = "http" + ((cookie.secure) ? "s" : "") + "://" + cookie.domain + cookie.path;
toRemove.name = name;
chrome.cookies.remove(toRemove);
++data.nCookiesFlagged;
});
if (filterMatchesCookie(currentFilter, cookie.name, cookie.domain, cookie.value)) {
chrome.tabs.query({ active: true }, function (tabs) {
var toRemove = {
url: "http" + (cookie.secure ? "s" : "") + "://" + cookie.domain + cookie.path,
name: cookie.name
};
chrome.cookies.remove(toRemove);
++data.nCookiesFlagged;
});
}
}
}

if (!removed && preferences.useMaxCookieAge && preferences.maxCookieAgeType > 0) { //Check expiration, if too far in the future shorten on user's preference
var maxAllowedExpiration = Math.round((new Date).getTime() / 1000) + (preferences.maxCookieAge * preferences.maxCookieAgeType);
if (!removed && preferences.useMaxCookieAge && preferences.maxCookieAgeType > 0) {
var maxAllowedExpiration = Math.round(Date.now() / 1000) + (preferences.maxCookieAge * preferences.maxCookieAgeType);
if (cookie.expirationDate !== undefined && cookie.expirationDate > maxAllowedExpiration + 60) {
var newCookie = cookieForCreationFromFullCookie(cookie);
if (!cookie.session)
newCookie.expirationDate = maxAllowedExpiration;
if (!cookie.session) newCookie.expirationDate = maxAllowedExpiration;
chrome.cookies.set(newCookie);
++data.nCookiesShortened;
}
}
});

function setContextMenu(show) {
chrome.contextMenus.removeAll();
if (show) {
chrome.contextMenus.create({
"title": "EditThisCookie",
"contexts": ["page"],
"onclick": function (info, tab) {
showPopup(info, tab);
}
});
}
}
chrome.contextMenus.removeAll(function() {
if (chrome.runtime.lastError) {
console.error("Error removing context menus: ", chrome.runtime.lastError);
} else {
console.log("Context menus removed");
}

function setChristmasIcon() {
if (isChristmasPeriod() && preferences.showChristmasIcon) {
chrome.browserAction.setIcon({ "path": "/img/cookie_xmas_19x19.png" });
} else {
chrome.browserAction.setIcon({ "path": "/img/icon_19x19.png" });
}
if (show) {
chrome.contextMenus.create({
id: "editThisCookie", // Unique identifier for the context menu item
title: "EditThisCookie",
contexts: ["page"]
}, function() {
if (chrome.runtime.lastError) {
console.error("Error creating context menu: ", chrome.runtime.lastError);
} else {
console.log("Context menu created");
}
});
}
});
}


45 changes: 34 additions & 11 deletions js/options_main_page.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,38 @@
var panel = JSON.parse(localStorage.getItem("option_panel"));
var arguments = getUrlVars();
var element;

if (panel === "null" || panel === null || panel === undefined) {
element = "support";
} else {
element = panel;
// Function to get URL variables
function getUrlVars() {
var vars = {};
window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m, key, value) {
vars[key] = value;
});
return vars;
}

if (arguments.page !== undefined) {
element = arguments.page;
// Function to redirect based on the panel state
function redirectBasedOnPanel(panel) {
var arguments = getUrlVars();
var element;

if (panel === "null" || panel === null || panel === undefined) {
element = "support";
} else {
element = panel;
}

if (arguments.page !== undefined) {
element = arguments.page;
}

location.href = "/options_pages/" + element + ".html";
}

location.href = "/options_pages/" + element + ".html";
// Get the 'option_panel' value from chrome.storage
chrome.storage.local.get("option_panel", function(result) {
if (chrome.runtime.lastError) {
console.error("Error getting option_panel: ", chrome.runtime.lastError);
// In case of error, default to 'support'
redirectBasedOnPanel("support");
} else {
// Call the redirect function with the retrieved panel value
redirectBasedOnPanel(result.option_panel);
}
});
15 changes: 9 additions & 6 deletions js/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,14 +217,16 @@ function createAccordionList(cks, callback, callbackArguments) {
let createAccordionCallback = callback;
let createAccordionCallbackArguments = callbackArguments;

try {
$("#cookiesList").accordion("destroy");
} catch (e) {
console.warn(e.message)
// Check if the accordion is initialized before attempting to destroy it
if ($("#cookiesList").hasClass("ui-accordion")) {
try {
$("#cookiesList").accordion("destroy");
} catch (e) {
console.warn(e.message);
}
}

if (cks === null)
cks = cookieList;
if (cks === null) cks = cookieList;
for (var i = 0; i < cks.length; i++) {
currentC = cks[i];

Expand Down Expand Up @@ -305,6 +307,7 @@ function createAccordionList(cks, callback, callbackArguments) {
});
}


function importCookies() {
var nCookiesImportedThisTime = 0;
var text = $(".value", "#pasteCookie").val();
Expand Down
11 changes: 6 additions & 5 deletions js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,24 +137,25 @@ function showPopup(info, tab) {
var tabID = encodeURI(tab.id);
var tabIncognito = encodeURI(tab.incognito);

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

chrome.tabs.query({ 'windowId': chrome.windows.WINDOW_ID_CURRENT }, function (tabList) {
for (var x = 0; x < tabList.length; x++) {
var cTab = tabList[x];
if (cTab.url.indexOf(urlToOpen) === 0) {
chrome.tabs.update(cTab.id, {
'selected': true
'active': true // Updated from 'selected' to 'active'
});
return
return;
}
}
chrome.tabs.create({
'url': urlToOpen
})
})
});
});
}


function copyToClipboard(text) {
if (text === undefined)
return;
Expand Down
Loading