0

I'm new to implementing chrome extensions and I want to be able to get a cookie value from a site, delete it, reload the site, and then get a different cookie value. The issue is that I can get the cookie value that I want, but when I remove the cookie and reload the page to get that value again it comes back as the same one instead of generating a new value. My thoughts are that the way I'm removing my cookie is wrong. If anyone could help me figure out where I'm going wrong, that would help a bunch, thanks!

manifest.json

{
    "manifest_version": 2,
    "name": "xxxx",
    "version": "1.0",
    "permissions": [
        "cookies",
        "tabs",
        "activeTab",
        "http://*/*",
        "https://*/*",
        "<all_urls>"
    ],
    "content_scripts": [
        {
            "matches": [
                "<all_urls>"
            ],
            "js": ["content.js"]
        }
    ],
    "background": {
        "scripts": ["background.js"]
    },
    "browser_action": {
        "default_icon": "cookie.png"
    }
}

background.js

function cookieInfo(){
    chrome.cookies.getAll({url: url, name: "specificCookie"}, (cookies) => {
        //console.log("getting cookie")
        console.log(cookies[0].value)
        //return cookies
    });
}

function removeCookies(){
    chrome.cookies.getAll({domain: url}, function(cookies) {
        console.log('Cookies Removed')
        clearCookies(cookies);
    });
}

function clearCookies(cookies) {
    // iterate on cookie to get cookie detail
    for (let i = 0; i < cookies.length; i++) {
        let url = "https" + (cookies[i].secure ? "s" : "") + "://" + cookies[i].domain + cookies[i].path;
        let cname = cookies[i].name;

        // delete cookie
        chrome.cookies.remove({
            url: url,
            name: cname
        });
    }
}

chrome.browserAction.onClicked.addListener(buttonClicked);

function buttonClicked(tab){  
    cookieInfo();
    removeCookies();

}

content.js

chrome.runtime.onMessage.addListener(gotMessage);

function gotMessage(req, sender, res){
    console.log(req);
}
Michael
  • 1,454
  • 3
  • 19
  • 45
  • Assuming the posted code is complete, the `url` variable is undefined in removeCookies and cookieInfo. Also `domain` is not a URL, it's just the domain part. It looks like you didn't actually debug your code. Debugging is essential in programming especially when learning new stuff. You can use the built-in devtools for that. Set a breakpoint inside the callbacks, run the code, inspect the variables, run it line by line in step mode, and so on. Note that the background script runs in a separate background page with its own devtools. – wOxxOm May 30 '20 at 09:14
  • @wOxxOm the url in my post is just a placeholder. This can be replaced with an actual url. When i run this i get a cookie back with say value: xxxxx. Using removeCookie method, it should remove all the cookies from that url. I reload the page and click the extension button again in hopes of getting a cookie with the value: xxxxd, but instead get: xxxxx – Michael May 30 '20 at 15:52
  • You need to present a working code, not pseudocode, in order to receive meaningful feedback. Otherwise we have to point out the obvious but irrelevant things thus wasting time. Another such guess would be that you incorrectly use asynchronous API, see [Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference](https://stackoverflow.com/q/23667086) and [How do I return the response from an asynchronous call?](https://stackoverflow.com/q/14220321) – wOxxOm May 30 '20 at 15:56

0 Answers0