0

This is my first time making an extension and I'm currently trying to create a google chrome extension to delete cookies. The problem is that no errors are being reported but also no cookies are being deleted. Part of the code I have written is below.

const deleteAll = document.getElementById("deleteAll");
  deleteAll.addEventListener('click',deleteAllCookies);
  function deleteAllCookies() {
    var cookies = document.cookie.split(";");
    for (var i = 0; i < cookies.length; i++) {
      var cookie = cookies[i];
      var eqPos = cookie.indexOf("=");
      var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
      document.cookie = "username=;expires=Thu, 01 Jan 1970 00:00:00 GMT;path=/;";
    }

    location.reload();
  };

    "name": "Cookie Delete",
    "manifest_version": 3,
    "version": "1.0",
    "permissions": [
        "cookies",
        "tabs",
        "storage"
    ],
    "host_permissions": ["<all_urls>"],
    "action": {
        "default_popup": "cookie.html"
        
         
    },
    "icons": {
        "128":"logo.png"

    },
       "background":{
        "service_worker": "cookie.js"
      }
      

I have tried implementing other codes with none yielding any success. The scripts are mentioned in the html and the manifest.

nPKmmer
  • 3
  • 2
  • Is it running at all? Add a `console.log()` to confirm. – Barmar May 02 '23 at 16:20
  • @Barmar I've done what you suggested and yes it appears that the function is not running. I am not sure what went wrong since I have mentioned the script in my manifest and html file. I've added my manifest above – nPKmmer May 02 '23 at 17:12
  • Sorry, I don't know enough about writing extensions to diagnose this. – Barmar May 02 '23 at 17:14
  • 1
    Did you generate this via ChatGPT? There's no such thing as `background.service_worker` and you don't need it anyway. Remove it and add `` at the end of your cookie.html. Also, if you want to change `document.cookie` you need to put that part of the code in the content script ([examples](/a/67227376)) or use `chrome.cookies` API instead (look for examples yourself). – wOxxOm May 02 '23 at 17:31
  • @wOxxOm Thank you for your input, I do have the script in the HTML file and I have taken the background service worker out. I will look more into the content scripts. – nPKmmer May 02 '23 at 17:54

1 Answers1

0

There are a handful of errors with your code.

  1. you are defining a function, but never calling it.
  2. the premise of your code is that you are overwriting the value of every cookie with an expiration date that is in the past. The issue here is that you not using any of the variables you are creating. You are just repeatedly setting the cookie username to an expired value, for every cookie.

In order to make it work, you would need to change this to something like

document.cookie = `${name}=;expires=Thu, 01 Jan 1970 00:00:00 GMT;path=/;`;

that sets the overwrites the value of each cookie with a blank value.

  1. This will only overwrite cookies that are not HTTPOnly. Since you are wanting a browser extension, you could use the cookies api. This is not available in content scripts, but is available in other extension surfaces.
const [tab] = await chrome.tabs.query({ active: true });
if (tab) {
  const domain = new URL(tab.url).hostname;
  const cookies = await chrome.cookies.getAll({ domain });

  cookies.forEach(cookie => {
    chrome.cookies.remove({
      name: cookie.name,
      url: tab.url,
      storeId: cookie.storeId,
    }, function(removedCookie) {
        console.log('Removed cookie:', removedCookie);
    });
  })
}

Note that in order for this to work, you would need to add activeTab, tabs, and cookies to the permissions field of your manifest, as well as request permission on <all_urls> for host_permissions.

Patrick
  • 13,872
  • 5
  • 35
  • 53