简体   繁体   English

在 Firefox 扩展中使用 JavaScript 删除 cookie

[英]Deleting cookies with JavaScript in Firefox extension

I have searched a lot for deleting all or a specific cookie with JavaScript.我已经搜索了很多使用 JavaScript 删除所有或特定 cookie 的方法。 There are lots of posts that say its not possible 100%, or you can not delete cookies with HttpOnly flag .有很多帖子说它不可能 100%,或者你不能删除带有HttpOnly 标志的cookie。 Then the question is how the Cookies Manager+ Firefox extension can delete cookies with JavaScript?那么问题来了, Cookies Manager+ Firefox 扩展如何使用 JavaScript 删除 cookie? Or how the Delete All Cookies From JavaScript Chrome extension lets programmers delete cookies by sending postMessage to his extension?或者, Delete All Cookies From JavaScript Chrome 扩展程序如何让程序员通过向其扩展程序发送postMessage来删除 cookie?

I am developing a Firefox extension and need to delete some cookies from a website.我正在开发 Firefox 扩展,需要从网站上删除一些 cookie。

How do I delete cookies in a Firefox extension?如何删除 Firefox 扩展中的 cookie?

As Alexander O'Mara mentioned in a comment, Chrome and Firefox JavaScript extensions run in a context that has higher privileges than JavaScript that is included on webpages.正如Alexander O'Mara在评论中提到的,Chrome 和 Firefox JavaScript 扩展在比网页中包含的 JavaScript 具有更高权限的上下文中运行。 At that higher privilege level they are permitted to make changes to cookies.在更高的权限级别,他们被允许对 cookie 进行更改。 Your confusion probably is that you are reading webpages that discuss what is possible for JavaScript that is running from a webpage.您的困惑可能是您正在阅读讨论从网页运行的 JavaScript 的可能性的网页。

While I have not tested it, the following code appears to do what you want:虽然我尚未对其进行测试,但以下代码似乎可以满足您的需求:

var {Cc, Ci} = require("chrome");

function DeleteAllCookiesForDomain( domain ) {
    var cookieManager = Cc["@mozilla.org/cookiemanager;1"].getService(Ci.nsICookieManager);
    var iter = cookieManager.enumerator;
    var cookie_count = 0;
    while (iter.hasMoreElements()) {
        var cookie = iter.getNext();
        if (cookie instanceof Ci.nsICookie) {
            if (domain.indexOf(cookie.host.toUpperCase()) != -1) {
                cookieManager.remove(cookie.host, cookie.name, cookie.path, false);
                cookie_count++;
            }
        }
    }
    return cookie_count;
};

The above code is slightly modified from code found in How do I delete cross site cookies with Firefox 22+ extension?上面的代码是从如何使用 Firefox 22+ 扩展程序删除跨站点 cookie 中的代码稍作修改的 . .

However, the above code is inefficient as it iterates over all cookies, not just those from the domain you are interested in deleting.但是,上面的代码效率低下,因为它遍历所有 cookie,而不仅仅是来自您有兴趣删除的域的 cookie。 The following uses the nsICookieManager2 interface to iterate only over those cookies for the domain which you are deleting:以下使用nsICookieManager2接口仅迭代您要删除的域的那些 cookie:

//For SDK
var {Cc, Ci} = require("chrome");
var cookieManager = Cc["@mozilla.org/cookiemanager;1"].getService(Ci.nsICookieManager2);
//*/
/*//For restartless/bootstrap/overlay
Components.utils.import("resource://gre/modules/Services.jsm");
var cookieManager = Services.cookies;
//*/

function DeleteAllCookiesForDomain( domain ) {
    var iter = cookieManager.getCookiesFromHost(domain);
    var cookie_count = 0;
    while (iter.hasMoreElements()) {
        var cookie = iter.getNext();
        if (cookie instanceof Ci.nsICookie) {
            cookieManager.remove(cookie.host, cookie.name, cookie.path, false);
            cookie_count++;
        }
    }
    return cookie_count;
};

Just use browsingData API I found it very friendly and easy to use只需使用browseData API我发现它非常友好且易于使用

In your background script在你的后台脚本中

var removalOptions = {hostnames: ['example.com', 'www.example.com']};
var dataTypeSet = {cookies: true}; // , indexedDB: true, localStorage:true
browser.browsingData.remove(removalOptions , dataTypeSet);

Note: I used "browsingData" and "storage" permissions in the mainfest.json file of the extension注意:我在扩展的 mainfest.json 文件中使用了“browsingData”和“storage”权限

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM