Reading web-page cookies from a Firefox extension (XUL)

ぃ、小莉子 提交于 2020-01-01 06:53:03

问题


I'm creating an extension for the Firefox browser. I would like to read a cookie which was set by an HTML page using JavaScript in the XUL file. Is it possible?

I tried using document.cookie, but it doesn't work:

function readCookie(name) {
  var ca = document.cookie.split(';');
  var nameEQ = name + "=";
  for(var i=0; i < ca.length; i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1, c.length); //delete spaces
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
  return "";
}

function createCookie(name, value, days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
    }
  else var expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function eraseCookie(name) {
  createCookie(name, "", -1);
}

Could you help me? Thanks in advance.


回答1:


These code snippets may help

///By this method you can iterate throug each cookie by its name or value... 

function ReadCookie()   
{
var cookieMgr = Components.classes["@mozilla.org/cookiemanager;1"]
         .getService(Components.interfaces.nsICookieManager);

for (var e = cookieMgr.enumerator; e.hasMoreElements();) {
 var cookie = e.getNext().QueryInterface(Components.interfaces.nsICookie); 
  dump(cookie.host + ";" + cookie.name + "=" + cookie.value + "\n");

 ///Your code here to check your cookie or your implementation...
 ///You can use cookie name or value to select your cookie...

}
}

///If you want to read cookies by domain name you can use this code...

function GetCookie(){
try
{
alert("Getting Cookies");
    var ios = Components.classes["@mozilla.org/network/io-service;1"]
            .getService(Components.interfaces.nsIIOService);
var uri = ios.newURI("http://www.yoursite.com/", null, null);

var cookieSvc =
   Components.classes["@mozilla.org/cookieService;1"]
             .getService(Components.interfaces.nsICookieService);
var cookie = cookieSvc.getCookieString(uri, null);

///Your logic here...
}
catch (errorInfo)
{
    alert(errorInfo);
}

}

Hope these will help :)




回答2:


You probably want to use the nsICookieService interface: https://developer.mozilla.org/en/Code_snippets/Cookies

(Found via the helpful search on Mozilla's Add-on Developer Hub: https://addons.mozilla.org/en-US/developers/search?q=cookie)




回答3:


You also might want to look at existing extensions that work with cookies, such as FireCookie.




回答4:


There are at least two distinct ways of doing this:

Firstly, if you simply want to interact with the Firefox cookie store, you can use the nsICookieManager and nsICookieManager2 interfaces to query, add and remove cookies.

Secondly, if you're more comfortable with the document.cookie approach, and you want to deal with documents which are already loaded in the browser, you can do this, but the important thing to remember is to get the right document to work with. When you simply use document in XUL code, you are referring to the document object associated with the browser window in which you are running. Several pages might be loaded in different tabs within this window, each of which will have its own document object. As a result, you first need to find the document object that you are interested in. One way of doing this, for example, is to register to be notified of pages loads, and then to examine pages as they load to see whether they are of interest. For example, your XUL code might look like this:

function pageLoad (event) {
  if (event.originalTarget instanceof HTMLDocument) {
    var doc = event.originalTarget;

    if (doc.URL.match(/^https?:\/\/[^\/]*\.example\.com\//)) {
      ⋮
      doc.cookie = 'cookie_name' + "=" + value + ";expires=" +
                   (new Date(2050, 10, 23)).toGMTString();
      ⋮
    }
  }
}

var appcontent = document.getElementById("appcontent");  // locate browser

if (appcontent)
  appcontent.addEventListener("load", function (e) { pageLoad(e); }, true);

With this approach you can interact with just those cookies associated with that page using the mechanisms with which you are familiar, and without worrying about dealing with cookies associated with completely different pages.



来源:https://stackoverflow.com/questions/1523137/reading-web-page-cookies-from-a-firefox-extension-xul

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!