访问 cookie,希望是在 JavaScript 中

发布于 2024-07-21 03:30:48 字数 740 浏览 6 评论 0 原文

我正在开发一个 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)?

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

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

发布评论

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

评论(3

空城仅有旧梦在 2024-07-28 03:30:48

我一直在查看 Mozilla 开发者页面的 nsICookie、nsICookie2、nsICookieManager 等,但并不清楚它们如何适合 javascript 或 XUL 或其他任何内容。

可以从 Firefox 扩展访问所有 cookie,并使用 nsICookieManager 和 nsICookie 接口。 从扩展中的 javascript 代码,您可以访问 cookie 管理器,并且

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

现在可以迭代所有存储的 cookie

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

,当引用 cookie 对象时,您可以检查其

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

nsICookie 接口。 此代码是 Firefox 特定的,可以作为浏览器扩展或 签名脚本运行。 希望我的解释有一点帮助。

下面我提供了一些有关在扩展中使用 JS XPCOM 接口的链接:

  1. JS XPCOM
  2. 使用 cookie

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.

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

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

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

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

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:

  1. JS XPCOM
  2. Using cookies
长亭外,古道边 2024-07-28 03:30:48

您可以使用 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

浅紫色的梦幻 2024-07-28 03:30:48

这是在 javascript 中使用 cookie 的一个很好的教程。 使用该教程中的函数,您可能可以执行以下操作:

if readCookie(yourCookieName != "") {
      alert("You're signed in!");
 else {
      alert("Go sign in, you fool!");
}

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

Here's a nice tutorial for working with cookies in javascript. Using the functions from that tutorial, you could probably do something like this:

if readCookie(yourCookieName != "") {
      alert("You're signed in!");
 else {
      alert("Go sign in, you fool!");
}

Here are the cookie functions:

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