Access cookies, hopefully in JavaScript

I’m 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) to see the status of their validation cookie in the status bar, We all need to be authenticated in order to access the sites, work-related, but we do not receive a warning when the cookie expires, so this leads to annoying and sometimes abrupt interruptions in work. In the end, this addition will allow us to send our credentials from the status bar without having to go to any reboots or redirects, but for now I just want it to show the status.

I looked at Mozilla's developer pages on nsICookie, nsICookie2, nsICookieManager, etc., and it’s not very clear how any of them fit into JavaScript or XUL or something else.

Ideally, I just want JavaScript to go beyond the bounds of the document and get a cookie string for the domain that I specify. If I could do this, it would allow porting the code to other browsers (in particular, Safari and Chrome). But if this should be browser specific, then I would like to at least learn how to check if a cookie exists in Firefox without any bells and whistles to configure or delete.

Simply put, I want 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 to do this (on the browser side, of course)?

+3
3

Mozilla nsICookie, nsICookie2, nsICookieManager .., , javascript XUL - .

Firefox 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 script. , .

JS XPCOM :

+5

Here's a good tutorial for working with cookies in javascript . Using the functions of this 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);
}
+1
source

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


All Articles