0

I have tried modifying my code so it looks similar to this answer.

Hello, my code seems to be returning an empty array when trying to use chrome.cookies.getAll asynchronously. It could be that I'm using it incorrectly, or that the url isn't passing through.

chrome.history.onVisited.addListener(function(HistoryItem) {
    var cookies = {
        start: function(callback) {
            this.logCookies(HistoryItem.url,callback);
        },
        logCookies: function(url,callback) {
            chrome.cookies.getAll({
                'url': url
            }, function(cookie) {
                console.log(cookie) //log the cookie
            });
        }
    }
    cookies.start(function() {
        console.log('getting cookies...')
    });
});

I hope this isn't too vague... what I'm trying to do is get all cookies from the currently visited website and log them. (My hope is to delete them in the future, but for now I would like to log each cookie found). Instead, it returns an empty array with the length of 0, and getting cookies... does not show up in the console. I'm not sure what I did wrong, it seems to me that I did everything right. I'm new to asynchronous coding, so a little help would be appreciated. Thank you!

johnboy13
  • 73
  • 7
  • Instead of console.log(cookie) use `callback(cookie)`. But I recommend using [Mozilla's WebExtension polyfill](https://github.com/mozilla/webextension-polyfill). See also [How do I return the response from an asynchronous call?](//stackoverflow.com/q/14220321) – wOxxOm Feb 18 '19 at 05:05
  • @wOxxOm I have tried that, and still no luck. It returns an array with the length of 0 still. – johnboy13 Feb 18 '19 at 18:28
  • It probably means there were no cookies for that URL. – wOxxOm Feb 18 '19 at 18:29
  • @wOxxOm Thank you for saying that, it reminded me that I needed to add `http` and `https` permissions to my manifest. – johnboy13 Feb 18 '19 at 18:50

1 Answers1

0

I found the problem! Turns out I didn't have the right permissions set in my manifest and I tweaked the code to use Promises, and it works completely fine! I got it to remove each cookie, too.

chrome.history.onVisited.addListener(function(HistoryItem) {
    var cookies = {

        start: function() {
            return this.logCookies();
        },

        logCookies: function() {

            return new Promise(function(resolve,reject){

                function remove(cookies){
                    for (var i = 0; i < cookies.length; i++) {
                        chrome.cookies.remove({url: HistoryItem.url + cookies[i].path, name: cookies[i].name},function(){
                            console.log('cookie removed!')
                        });
                    }
                    resolve()
                }

                chrome.cookies.getAll({
                    'url': HistoryItem.url
                }, function(cookies) {
                    remove(cookies)
                });

            })
        }
    }

    cookies.start().then(function(){
        console.log('removing cookies...')
    });

});

johnboy13
  • 73
  • 7