从 Firefox 扩展 (XUL) 读取网页 cookie

发布于 2024-08-06 16:33:02 字数 845 浏览 5 评论 0原文

我正在为 Firefox 浏览器创建一个扩展。我想读取 XUL 文件中使用 JavaScript 由 HTML 页面设置的 cookie。是否可以?

我尝试使用 document.cookie,但它不起作用:

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);
}

你能帮我吗?提前致谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

撩人痒 2024-08-13 16:33:02

这些代码片段可能会有所帮助

///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...

}
}

///如果您想通过域名读取 cookie,您可以使用此代码...

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);
}

}

希望这些会有所帮助:)

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 :)

把梦留给海 2024-08-13 16:33:02

您可能还想查看可与 cookie 配合使用的现有扩展,例如 FireCookie。

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

如若梦似彩虹 2024-08-13 16:33:02

至少有两种不同的方法可以做到这一点:

首先,如果您只是想与 Firefox cookie 存储进行交互,您可以使用 nsICookieManagernsICookieManager2 接口用于查询、添加和删除 cookie 。

其次,如果您更喜欢 document.cookie 方法,并且您想要处理浏览器中已加载的文档,您可以这样做,但要记住的重要一点是获取正确的文档以供使用。当您在 XUL 代码中简单地使用 document 时,您指的是与您正在运行的浏览器窗口关联的文档对象。多个页面可能会加载到此窗口中的不同选项卡中,每个页面都有自己的文档对象。因此,您首先需要找到您感兴趣的文档对象。例如,执行此操作的一种方法是注册以获取页面加载通知,然后在页面加载时检查页面以查看它们是否已加载。的兴趣。例如,您的 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);

通过这种方法,您可以使用您熟悉的机制仅与与该页面关联的那些 cookie 进行交互,而不必担心处理与完全不同页面关联的 cookie。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文