如何在我的firefox插件下设置cookie?

时间:2010-07-30 10:06:59

标签: javascript firefox firefox-addon

如何在我的firefox插件下设置cookie?

function setCookie(name, value, expires, path, domain, secure) {
    document.cookie = name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}
setCookie("foo", "bar");

这个简单的js不是在firefox插件中设置cookie,而是在网页中运行良好。

1 个答案:

答案 0 :(得分:4)

从附加组件中,您可以使用cookie管理器XPCOM:

    var ios = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService);
    var cookieUri = ios.newURI("http://www.yourplacewhereyouwanttosetthecookie.com/", null, null);
    var cookieSvc = Components.classes["@mozilla.org/cookieService;1"].getService(Components.interfaces.nsICookieService);

    cookieSvc.setCookieString(cookieUri, null, "your_key=your_value;", null);

您可以在此处找到更多详细信息:

https://developer.mozilla.org/en/Code_snippets/Cookies#Setting_a_cookie