Search code examples
cookiessdkgoogle-chrome-extensionfirefox-addonfirefox-addon-sdk

Access cookies with a Firefox Addon content script?


I am trying to translate an Addon from Chrome that someone else created. It has a content script that has chrome.cookies.get in it. I can't find a suitable way to fix this for Firefox. Is there any way that I can access the cookies from a content script in the addon sdk?

Here's the original code:

function getCookies(domain, name, callback) {
    chrome.cookies.get({"url": domain, "name": name},
        function(cookie) {
            if (callback) {
                if (cookie) {
                        callback(cookie.value);
                } else {
                    callback(null);
                }
            }
        }
    );
}

Solution

  • A content script doesn't have the necessary privileges to use any advanced API - neither in Firefox nor in Chrome. It can get the cookies for the current page via document.cookie however. Same restrictions apply as for the web page itself - HTTPOnly cookies won't be visible.

    In the extension modules however you can use nsICookieManager2 interface to access cookies. See Access specific cookies by domain/name in Firefox extension for details. If you want that information from a content script you will have to send a message from the content script to the extension to make the extension retrieve it for you.