Skip to content

Commit

Permalink
feat #303: change individual cookie removal to browsingData api
Browse files Browse the repository at this point in the history
  • Loading branch information
rafeerahman committed Aug 20, 2024
1 parent 606e08d commit 3debe8a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/_locales
Submodule _locales updated 1 files
+20 −10 en/messages.json
20 changes: 8 additions & 12 deletions src/js/background/assignManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -571,18 +571,14 @@ window.assignManager = {
return true;
},

async _resetCookiesForSite(pageUrl, cookieStoreId) {
const url = new URL(pageUrl);
// Remove 'www.' from the domain value
const domain = url.hostname.replace(/^www\./, "");
const cookies = await browser.cookies.getAll({domain: domain, storeId: cookieStoreId});
for (const cookie of cookies) {
const domain = cookie.domain.startsWith(".") ? cookie.domain.slice(1) : cookie.domain;
const cookieUrl = `${cookie.secure ? "https" : "http"}://${domain}${cookie.path}`;
await browser.cookies.remove({ url: cookieUrl, name: cookie.name, storeId: cookie.storeId });
}

return true; // Success
async _resetCookiesForSite(hostname, cookieStoreId) {
const hostNameTruncated = hostname.replace(/^www\./, ''); // Remove "www." from the hostname
await browser.browsingData.removeCookies({
cookieStoreId: cookieStoreId,
hostnames: [hostname, hostNameTruncated] // This does not remove cookies from associated domains. To remove all cookies, we have a container storage removal option.
});

return true;
},

async _setOrRemoveAssignment(tabId, pageUrl, userContextId, remove) {
Expand Down
20 changes: 11 additions & 9 deletions src/js/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ const Logic = {

setTimeout(() => {
notificationCard.classList.remove("is-shown");
}, 1000);
}, 2000);
});
},

Expand Down Expand Up @@ -984,12 +984,15 @@ Logic.registerPanel(P_CONTAINER_INFO, {
Utils.addEnterHandler(deleteData, async () => {
const userContextId = Utils.userContextId(identity.cookieStoreId)

await browser.runtime.sendMessage({
const result = await browser.runtime.sendMessage({
method: "deleteContainerDataOnly",
message: { userContextId }
});

Logic.notify({messageId: "storageWasClearedConfirmation", placeholders: [identity.name]});

if (result.done === true) {
Logic.notify({messageId: "storageWasClearedConfirmation", placeholders: [identity.name]});
}

this.prepare();
});

Expand Down Expand Up @@ -1477,7 +1480,7 @@ Logic.registerPanel(P_CONTAINER_ASSIGNMENTS, {
/* As we don't have the full or correct path the best we can assume is the path is HTTPS and then replace with a broken icon later if it doesn't load.
This is pending a better solution for favicons from web extensions */
const assumedUrl = `https://${site.hostname}/favicon.ico`;
const resetSiteCookiesInfo = browser.i18n.getMessage("resetSiteCookiesTooltipInfo");
const resetSiteCookiesInfo = browser.i18n.getMessage("clearSiteCookiesTooltipInfo");
const deleteSiteInfo = browser.i18n.getMessage("deleteSiteTooltipInfo");
trElement.innerHTML = Utils.escaped`
<td>
Expand All @@ -1498,13 +1501,12 @@ Logic.registerPanel(P_CONTAINER_ASSIGNMENTS, {
});
const resetButton = trElement.querySelector(".reset-button");
Utils.addEnterHandler(resetButton, async () => {
const pageUrl = `https://${site.hostname}`;
const cookieStoreId = Logic.currentCookieStoreId();
const result = await Utils.resetCookiesForSite(pageUrl, cookieStoreId);
const result = await Utils.resetCookiesForSite(site.hostname, cookieStoreId);
if (result === true) {
Logic.notify({messageId: "cookieResetSuccess", placeholders: []});
Logic.notify({messageId: "cookiesClearedSuccess", placeholders: [site.hostname]});
} else {
Logic.notify({messageId: "cookiesCouldNotBeReset", placeholders: []});
Logic.notify({messageId: "cookiesCouldNotBeCleared", placeholders: [site.hostname]});
}
});
trElement.classList.add("menu-item", "hover-highlight", "keyboard-nav");
Expand Down

0 comments on commit 3debe8a

Please sign in to comment.