1

I am developing a Mozilla Firefox extension using the Add-on SDK. On every tab change event, I want to read cookies of specified host which is open in another tab. I reached up to tab change, but am trying to figure out the way to get the cookies of specified host in recent activated tab.

var tabs = require("sdk/tabs");
tabs.on('activate', function(tab) {
    // want to get cookies here. 
});
Makyen
  • 31,849
  • 12
  • 86
  • 121
Ashish Patel
  • 921
  • 1
  • 10
  • 26
  • Related/near duplicate: [Access specific cookies by domain/name in Firefox extension](http://stackoverflow.com/q/9477682/3773011) – Makyen Sep 09 '16 at 07:16
  • In reading your question, it is unclear to me from which tab you are attempting to get the domain for which you want the cookies. Your code looks like you want to get the domain for the tab that just activated. Your text implies that you want to get the domain from a different tab. – Makyen Sep 09 '16 at 07:22
  • yes the domain i specified is in different tab – Ashish Patel Sep 09 '16 at 07:28
  • Then you are going to need to give us more information. Other than that it is in a different tab, you have provided no information as to how we are supposed to know which domain/tab. Thus, even if we were iterating through all the other tabs, we have no knowledge of how we might determine which tab/domain it is you desire to be used. For instance, I have more than 300 different tabs open in 20, or so windows within this profile. How are we to know which tab/domain to use? – Makyen Sep 09 '16 at 07:37

1 Answers1

2

Well, from within a tabs.on('activate') event handler, you have the tab. The tab object has a property url from which you can obtain the host. Once you have the host, you can get the cookies for that host. You have not stated what you want to do with them. So, here is just a way to enumerate them.

In order to use some methods of Services.cookies (nsICookieManager2) you will also need to require Chrome Authority.

var domainToUse = 'google.com';
var { Services }  = require("resource://gre/modules/Services.jsm");
var { Cc, Cu, Ci} = require("chrome");
let cookieEnumerator = Services.cookies.getCookiesFromHost(domainToUse);
while (cookieEnumerator.hasMoreElements()) {
  let cookie = cookieEnumerator.getNext().QueryInterface(Ci.nsICookie2); 
  console.log(cookie.host + ";" + cookie.name + "=" + cookie.value + "\n");
}

Update for newer versions of Firefox:
Note: At least by Firefox 50.0a2 (currently Firefox Developer Edition), it is necessary to use a slightly different call to getCookiesFromHost() to obtain the cookieEnumerator. Without the change, the call to getCookiesFromHost() will display a warning message in the Browser Console directing you to visit the nsICookieManager2 MDN documentation page which has no updated information on the warning, or any documentation on the change. I had to look in the source code to determine what was required. What appears to be desired is to pass in the current content document. However, from a background script that did not appear reasonable. The other way it is used is to just pass in an empty Object, {}. Thus, that line is changed to:

let cookieEnumerator = Services.cookies.getCookiesFromHost(domainToUse,{});

It is supposed to be for passing in "the originAttributes of cookies that would be be retrieved."

The above code is slightly modified from my answer to "How to set custom cookies using Firefox Add-on SDK (using Services from Firefox Add-on SDK)".

Community
  • 1
  • 1
Makyen
  • 31,849
  • 12
  • 86
  • 121
  • that probably won't work with private browsing windows, identity containers or first party isolation (the latter two are in-development features) – the8472 Sep 10 '16 at 00:37
  • @the8472, When in private browsing windows the above code (note changes required for newer Firefox versions) will return the cookies which were set during normal browsing. It will not get cookies which may have been transmitted for the private browsing window. – Makyen Sep 10 '16 at 01:32
  • @AshishPatel, I updated the code in this answer due to a, as of yet undocumented, change in the Firefox source code that causes any older code which calls `getCookiesFromHost()` to spit out a warning in the Browser Console telling you to update your code (and see the [nsICookieManager2 documentation](https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsICookieManager2), which has not yet been updated). Please see the section in this answer "Update for newer versions of Firefox" – Makyen Sep 10 '16 at 01:59
  • you don't need the document, just the originattributes. `._contentPrincipal.originAttributes` – the8472 Sep 10 '16 at 02:28
  • Hi . i have used this above solution it working fine with the development mode. but now i am publishing my addon while uploading files it shows bellow warning . Usage of low-level or non-SDK interface Warning: Your add-on uses an interface which bypasses the high-level protections of the add-on SDK. This interface should be avoided, and its use may significantly complicate your review process. – Ashish Patel Sep 19 '16 at 13:29
  • @AshishPatel, You will get that warning anytime you use `require("chrome")`, or potentially other lower level interfaces, within an Add-on SDK add-on. The warning reflects the ideal situation that all Add-on SDK extensions be able to accomplish everything with the high level SDK APIs. That is not possible in this case. Basically, you should just ignore that warning. – Makyen Sep 19 '16 at 15:08
  • @Makyen so is there any chances of get rejected because of this?? as of now i am in queue for review process. – Ashish Patel Sep 20 '16 at 06:04
  • @AshishPatel, No, it will be fine (assuming there is not something else that is a security issue). Using the lower level APIs & Chrome Authority is necessary for accessing cookies. The review process is looking mostly for security and privacy issues. Obtaining access to the low level APIs & Chrome Authority opens up more possibilities for what you might do (i.e. you have more power). Thus, they have to pay a bit more attention to your add-on in the review. Fortunately, review queue times are *much* shorter than they were not all that long ago. Thus, there should not be much impact (delay). – Makyen Sep 20 '16 at 07:23
  • @AshishPatel, The worst case for a review is that you will receive an email saying that the current version on your add-on has not passed the review. If so, the review will contain a list of specific issues that the reviewer had with the ad-on. You will then need to address those issues by either changing the add-on (usually), or (sometimes) explaining why it is not an issue, or why it is required. If you have questions once you get such an email, you can ask for clarification. – Makyen Sep 20 '16 at 07:30
  • 2019 and MDN document is not updated yet! thanks for your response. it fixed the issue.I wonder why they are keep adding parameter to existing method and functions which is not optional and keep breaking stuff. – AaA Jan 21 '19 at 04:26