我正在开发一个 Firefox 插件,它允许用户(所有用户都属于特定组;该插件的受众范围非常有限)从状态栏查看其身份验证 cookie 的状态。 我们都必须进行身份验证才能访问与工作相关的网站,但当 cookie 过期时我们不会收到任何警告,因此这会导致工作流程中令人烦恼的、有时甚至是严重的中断。 最终,这个附加组件将允许我们从状态栏提交我们的凭据,而不必进行任何重新加载或重定向,但现在,我只想看到它显示状态。
我一直在查看 Mozilla 开发人员页面的 nsICookie、nsICookie2、nsICookieManager 等,但并没有很清楚地了解它们如何适合 JavaScript 或 XUL 或其他任何内容。
理想情况下,我只是想要一种让 JavaScript 能够走出文档并获取我指定的域的 cookie 字符串的方法。 如果我能做到这一点,就可以将代码移植到其他浏览器(特别是 Safari 和 Chrome)。 但如果这必须是特定于浏览器的,那么我至少想知道检查 cookie 是否存在于 Firefox 中的方法,而不需要任何设置或删除的花哨。
简而言之,我想要一种方式来表达:
if (cookieExists("sample.com", CookieName)) {
alert("You're signed in!");
} else {
alert('Go sign in, you fool!');
}
执行此操作最简单/最便携的方法是什么(当然是浏览器端)?
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 work flow. 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.
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.
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)?
发布评论
评论(3)
可以从 Firefox 扩展访问所有 cookie,并使用 nsICookieManager 和 nsICookie 接口。 从扩展中的 javascript 代码,您可以访问 cookie 管理器,并且
现在可以迭代所有存储的 cookie
,当引用 cookie 对象时,您可以检查其
在 nsICookie 接口。 此代码是 Firefox 特定的,可以作为浏览器扩展或 签名脚本运行。 希望我的解释有一点帮助。
下面我提供了一些有关在扩展中使用 JS XPCOM 接口的链接:
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
and than you can iterate through all stored cookies
now, when having reference to cookie object you can check its properties
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.
Below I present some links on using JS XPCOM interfaces in extensions:
您可以使用 jquery 插件进行 cookie 处理
http://www .stilbuero.de/2006/09/17/cookie-plugin-for-jquery/
或简单地通过 javascript :
http://www.quirksmode.org/js/cookies.html
you can use jquery plugin for cookie handling
http://www.stilbuero.de/2006/09/17/cookie-plugin-for-jquery/
or simply through javascript :
http://www.quirksmode.org/js/cookies.html
这是在 javascript 中使用 cookie 的一个很好的教程。 使用该教程中的函数,您可能可以执行以下操作:
这是 cookie 函数:
Here's a nice tutorial for working with cookies in javascript. Using the functions from that tutorial, you could probably do something like this:
Here are the cookie functions: