Reading Web Page Files from the Firefox Extension (XUL)

I am creating a Firefox browser extension. I would like to read the cookie that was set by the HTML page using JavaScript in the XUL file. Is it possible?

I tried using document.cookie, but it does not 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.

+3
source share
4 answers

These code snippets can 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 this helps :)

+14
source
+5

, cookie, FireCookie.

+1

:

-, cookie Firefox, nsICookieManager nsICookieManager2 , cookie.

-, document.cookie, , , , , . document XUL-, , , . , . , . , , , , , . , XUL- :

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 only interact with cookies that are associated with this page, using mechanisms that you are familiar with, and not worry about how to handle cookies associated with completely different pages.

+1
source

Source: https://habr.com/ru/post/1719346/


All Articles