访问cookie,希望在javascript中 [英] accessing cookies, hopefully in javascript

查看:121
本文介绍了访问cookie,希望在javascript中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个Firefox插件,允许用户(他们都是特定组的一部分;这个插件在受众范围内非常有限)来从状态栏查看他们的身份验证cookie的状态。我们都必须进行身份验证以访问与工作相关的网站,但是当Cookie过期时,我们不会收到警告,因此这会导致工作流程中出现恼人的,有时会突然中断的情况。最后,此添加将允许我们从状态栏提交我们的凭据,而不必去做任何重新加载或重定向,但现在,我只想看到它显示状态。

I am working on a Firefox add-on that will allow users (all of whom are part of a specific group; this add-on is very limited in audience scope) to see the status of their authentication cookie from the status bar. We all have to authenticate to access work-related sites, but we get no warning when the cookie expires, so this causes annoying and sometimes drastic interrupts in workflow. Eventually, this add on will allow us to submit our credentials from the status bar without having to go to do any reloads or redirects, but for now, I just want to see it show the status.

我一直在寻找在nsICookie,nsICookie2,nsICookieManager等的Mozilla开发者页面,它不清楚如何任何它适合javascript或XUL或任何其他。

I have been looking at the Mozilla developer pages at nsICookie, nsICookie2, nsICookieManager, etc, and it doesn't make very clear sense how any of it fits into javascript or XUL or anything else.

理想情况下,我只是想让javascript去文档外部并获取我指定的域的cookie字符串。如果我能做到这一点,它将允许代码可能被移植到其他浏览器(Safari和Chrome,特别是)。但是如果这必须是浏览器特定的,那么我至少想知道用于检查Firefox中是否存在cookie而没有设置或删除的响铃和哨声的方法。

Ideally, I'd just like a way for the javascript to go outside of the document and get the cookie string for a domain I specify. If I could do that, it would allow the code to possibly be ported over to other browsers (Safari and Chrome, in particular). But if this must be browser specific, then I would at least like to know the method for checking if the cookie exists in Firefox without any bells and whistles of setting or removing.

简单地说,我想要一种方式:

Simply put, I want a way to say:

 if cookieExists("sample.com", CookieName) {
      alert('You're signed in!');
 else {
      alert('Go sign in, you fool!');
      }

这是最简单/最便携的方式,当然)?

What is the easiest/most-portable way of doing this (browser-side, of course)?

谢谢!

推荐答案


我一直在查看nsICookie,nsICookie2,nsICookieManager等的Mozilla开发者页面,并没有清楚地知道它是如何适合javascript或XUL或其他任何东西。

I have been looking at the Mozilla developer pages at nsICookie, nsICookie2, nsICookieManager, etc, and it doesn't make very clear sense how any of it fits into javascript or XUL or anything else.

可以访问Firefox扩展中的所有Cookie,并使用nsICookieManager和nsICookie接口。从扩展程序中的JavaScript代码,您可以通过

access to all cookies from Firefox extension is possible and uses the nsICookieManager and nsICookie interfaces. From javascript code in your extension, you access the cookie manager with

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

,您可以遍历所有存储的cookie

and than you can iterate through all stored cookies

var enum = cookieManager.enumerator;
while (enum.hasMoreElements()){
   var cookie = enum.getNext();
   if (cookie instanceof Components.interfaces.nsICookie){
      // commands
   }
}

现在,当引用cookie对象时,您可以检查其属性

now, when having reference to cookie object you can check its properties

cookie.host
cookie.name
cookie.value
...

nsICookie 界面中定义。此代码是Firefox特有的,可以作为浏览器扩展或签名的脚本。希望我的解释帮助了一下。

defined in nsICookie interface. This code is Firefox specific and can be run as a browser extension or signed script. Hope my explanation helped a bit.

下面我展示一些关于在扩展中使用JS XPCOM接口的链接:

Below I present some links on using JS XPCOM interfaces in extensions:


  1. JS XPCOM

  2. 使用Cookie

  1. JS XPCOM
  2. Using cookies

这篇关于访问cookie,希望在javascript中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆