1

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);
                }
            }
        }
    );
}
Ian
  • 5,704
  • 6
  • 40
  • 72

1 Answers1

7

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.

Community
  • 1
  • 1
Wladimir Palant
  • 56,865
  • 12
  • 98
  • 126
  • I am completely confused by `nsICookieManager`. All I want to do is retrieve and set cookies from another domain. Can I use `nsICookieManager` to change an existing cookie? The only possible option is `add`. Will that replace a cookie already in place? Sorry if I sound really dumb-I've never used cookies before, and the MDN isseverely lacking in examples. – Ian May 11 '13 at 13:53
  • @Ian: Yes, you call `add()` to overwrite a cookie. These methods are meant to be called by the HTTP protocol implementation, and in HTTP you overwrite cookies implicitly. – Wladimir Palant May 11 '13 at 14:10