用于为FireFox创建cookie的API

时间:2012-01-20 12:52:37

标签: firefox firefox-addon

我想编写一个为firefox创建cookie的应用程序。 我想创建客户端cookie,以便firefox将在HTTP请求中发送cookie内容。 与win32 API InternetSetCookie()

类似

你能指导一下吗?

如果您可以向我指出一些代码段或帮助,我会尝试从中找出答案。

这个cookie需要转到SQLITE数据库,但是从旧问题来看,如果firefox正在运行,这个数据库会被锁定。此锁定在FF 3.5

中完成

只想确认FF9是否属于这种情况,还是有任何API?

此致

1 个答案:

答案 0 :(得分:2)

在Firefox上,您可以编写一个附加组件来实现这一目标。请查看以下加载项的源代码。它们提供了在Firefox运行时添加,删除,编辑cookie等功能。它们似乎都适用于Firefox 9.0.1(最新的稳定版)。

  1. Cookie Manager+
  2. Advanced Cookie Manager
  3. Add N Edit Cookie
  4. Edit Cookie
  5. 修改

    我发布了一些来自Evernote插件的 MozillaCookieManagerImpl.js 文件的cookie管理代码。我认为代码说明了一切。看看下面。它显示了如何访问cookie,设置,获取和删除它们等。

    访问Mozilla的Cookie管理界面

    Evernote.MozillaCookieManagerImpl = function MozillaCookieManagerImpl() {
    };
    Evernote.inherit(Evernote.MozillaCookieManagerImpl,
        Evernote.CookieManagerImpl, true);
    Evernote.MozillaCookieManagerImpl.isResponsibleFor = function(navigator) {
      var ua = navigator.userAgent.toLowerCase();
      return (ua.indexOf("firefox") >= 0 || ua.indexOf("thunderbird") >= 0 || ua
          .indexOf("shredder") >= 0);
    };
    
    
    Evernote.MozillaCookieManagerImpl.prototype.manager = null;
    Evernote.MozillaCookieManagerImpl.prototype._ios = null;
    Evernote.MozillaCookieManagerImpl.prototype._cookieSrv = null;
    Evernote.MozillaCookieManagerImpl.prototype._cookieManagerSrv = null;
    
    
    Evernote.MozillaCookieManagerImpl.prototype.getIOService = function() {
      if (this._ios == null) {
        this._ios = Components.classes["@mozilla.org/network/io-service;1"]
            .getService(Components.interfaces.nsIIOService);
      }
      return this._ios;
    };
    
    
    Evernote.MozillaCookieManagerImpl.prototype.getCookieService = function(
        force) {
      if (this._cookieSrv == null || force) {
        this._cookieSrv = Components.classes["@mozilla.org/cookieService;1"]
            .getService(Components.interfaces.nsICookieService);
      }
      return this._cookieSrv;
    };
    
    
    Evernote.MozillaCookieManagerImpl.prototype.getCookieManagerService = function(
        force) {
      if (this._cookieManagerSrv == null || force) {
        this._cookieManagerSrv = Components.classes["@mozilla.org/cookiemanager;1"]
            .getService(Components.interfaces.nsICookieManager);
      }
      return this._cookieManagerSrv;
    };
    

    获取Cookie

    Evernote.MozillaCookieManagerImpl.prototype.get = function(name, url) {
      var uri = this.getIOService().newURI(url, null, null);
      var cookieMgr = this.getCookieManagerService();
      if (cookieMgr) {
        for ( var e = cookieMgr.enumerator; e.hasMoreElements();) {
          var cookie = e.getNext().QueryInterface(Components.interfaces.nsICookie);
          if (cookie && cookie.host == uri.host && cookie.name == name) {
            return new Evernote.Cookie(cookie);
          }
        }
      }
      return null;
    };
    

    设置Cookie

    Evernote.MozillaCookieManagerImpl.prototype.set = function(cookie, url) {
      var uri = (typeof url == 'string') ? this.getIOService().newURI(url, null,
          null) : null;
      if (cookie instanceof Evernote.Cookie && typeof cookie.name == 'string'
          && cookie.name.length > 0) {
        this.getCookieService().setCookieString(uri, null,
            (cookie.name + "=" + cookie.value + ";"), null);
      }
    };
    

    删除Cookie

    Evernote.MozillaCookieManagerImpl.prototype.remove = function(name, url) {
      var cookieMgr = this.getCookieManagerService();
      var urlParts = url.split("://", 2);
      var domain = (urlParts.length == 2) ? urlParts[1] : urlParts[0];
      urlParts = domain.split("/", 2);
      var path = (urlParts.length == 2) ? urlParts[1] : null;
      cookieMgr.remove(domain, name, path, false);
    };