greasemonkey可以从给定域中删除Cookie吗? [英] Can greasemonkey delete cookies from a given domain?

查看:330
本文介绍了greasemonkey可以从给定域中删除Cookie吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

greasemonkey可以从指定的网域中删除Cookie吗?如果是,如何?

解决方案

Greasemonkey可以删除的主要限制。其他工具可能更适合你想要的,见下文。但是,如果满足所有这些条件:




  • 您要删除的Cookie位于当前页面的域中。

  • 他们不是安全Cookie

  • 您可以遍历可能的路径,包括 / ,空白路径等。


  • 跟踪你真的是一个cookie。许多网站使用各种其他技术,包括LSO,本地存储等。



THEN 以下代码将删除它们:

  // ---循环通过Cookie并删除它们。 
var cookieList = document.cookie.split(/; \s * /);

for(var J = cookieList.length - 1; J> = 0; --J){
var cookieName = cookieList [J] .replace(/ \s * \w +)=。+ $ /,$ 1);

eraseCookie(CookieName);
}

其中 eraseCookie()是:

(请注意,通过尝试所有可能的路径和最有可能的子域名,此 eraseCookie 会获得更多的Cookie。)

 函数eraseCookie(cookieName){
// ---一次性INITS:
// ---设置可能的域。省略一些罕见的边缘情况。
var domain = document.domain;
var domain2 = document.domain.replace(/^www\./,);
var domain3 = document.domain.replace(/^(\w+\.)+?(\w+\.\w+)$/,$ 2);;

// ---获取当前页面的可能路径:
var pathNodes = location.pathname.split(/\").map(function(pathWord){
return'/'+ pathWord;
});
var cookPaths = [] .concat(pathNodes.map(function(pathNode){
if(this.pathStr){
this.pathStr + = pathNode;
}
else {
this.pathStr =; path =;
return(this.pathStr + pathNode);
}
return(this.pathStr);
}));

(eraseCookie = function(cookieName){
// ---对于每个路径,尝试删除cookie
cookPaths.forEach(function(pathStr){
// ---要删除cookie,将其过期日期设置为过期值
var diagStr = cookieName +=+ pathStr +; expires = Thu,1970年1月1日00:00 :01 GMT;;
document.cookie = diagStr;

document.cookie = cookieName +=+ pathStr +; domain =+ domain +; expires = 1970年1月1日00:00:01 GMT;;
document.cookie = cookieName +=+ pathStr +; domain =+ domain2 +; expires = Thu,01-Jan-1970 00 :00:01 GMT;;
document.cookie = cookieName +=+ pathStr +; domain =+ domain3 +; expires = Thu,1970年1月1日00:00:01 GMT; ;
});
})(cookieName);
}

可选函数,用于信息或调试:

  function listCookies(){
var cookieList = document.cookie.split(/; \s * /);

for(var J = 0,numCookies = cookieList.length; J console.log(Cookie,J,:, [J]);
}
}







您的GM脚本还可以使用iFrame技巧删除第三方域上的Cookie,但GM通常不是处理Cookie的最佳方式。 / p>

不要被任何其他声明欺骗,Greasemonkey和javascript根本不能删除cookie,除非满足在此答案顶部列出的所有条件。请注意,javascript和Greasemonkey甚至无法查看 页面上的所有Cookie。



Greasemonkey不是最好的工具



这里有一些更强大的解决方案:


  1. 使用选择性Cookie删除。它保留你想要的饼干,并删除其余的。它在一个非常方便的按钮的推动或Firefox自动关闭时执行。支持白名单和黑名单。

  2. 使用 BetterPrivacy

  3. 执行 CCleaner

  4. 对于强大的,自定义的,完全自动的Cookie删除,没有严重的限制Greasemonkey ,您可以编写自己的浏览器扩充功能。


Can greasemonkey delete cookies from a given domain? If so, how?

There are major limitations on what Greasemonkey can delete. Other tools may be better for what you want, see below. But, if all of these conditions are met:

THEN, the following code will delete them:

//--- Loop through cookies and delete them.
var cookieList  = document.cookie.split (/;\s*/);

for (var J = cookieList.length - 1;   J >= 0;  --J) {
    var cookieName = cookieList[J].replace (/\s*(\w+)=.+$/, "$1");

    eraseCookie (cookieName);
}

Where eraseCookie() is:
(Note that this eraseCookie gets many more cookies by attempting all possible paths and the most likely sub-domains.)

function eraseCookie (cookieName) {
    //--- ONE-TIME INITS:
    //--- Set possible domains. Omits some rare edge cases.?.
    var domain      = document.domain;
    var domain2     = document.domain.replace (/^www\./, "");
    var domain3     = document.domain.replace (/^(\w+\.)+?(\w+\.\w+)$/, "$2");;

    //--- Get possible paths for the current page:
    var pathNodes   = location.pathname.split ("/").map ( function (pathWord) {
        return '/' + pathWord;
    } );
    var cookPaths   = [""].concat (pathNodes.map ( function (pathNode) {
        if (this.pathStr) {
            this.pathStr += pathNode;
        }
        else {
            this.pathStr = "; path=";
            return (this.pathStr + pathNode);
        }
        return (this.pathStr);
    } ) );

    ( eraseCookie = function (cookieName) {
        //--- For each path, attempt to delete the cookie.
        cookPaths.forEach ( function (pathStr) {
            //--- To delete a cookie, set its expiration date to a past value.
            var diagStr     = cookieName + "=" + pathStr + "; expires=Thu, 01-Jan-1970 00:00:01 GMT;";
            document.cookie = diagStr;

            document.cookie = cookieName + "=" + pathStr + "; domain=" + domain  + "; expires=Thu, 01-Jan-1970 00:00:01 GMT;";
            document.cookie = cookieName + "=" + pathStr + "; domain=" + domain2 + "; expires=Thu, 01-Jan-1970 00:00:01 GMT;";
            document.cookie = cookieName + "=" + pathStr + "; domain=" + domain3 + "; expires=Thu, 01-Jan-1970 00:00:01 GMT;";
        } );
    } ) (cookieName);
}

Optional function, for information or debug:

function listCookies () {
    var cookieList  = document.cookie.split (/;\s*/);

    for (var J = 0, numCookies = cookieList.length;   J < numCookies;  ++J) {
        console.log ("Cookie ", J, ": ", cookieList[J]);
    }
}



Your GM script can also use iFrame tricks to delete cookies on third-party domains, but GM is not the best way to handle cookies, in general.

Don't be fooled by any other claims, Greasemonkey and javascript simply cannot delete a cookie unless all of the conditions, listed at the top of this answer, are met. Note that javascript and Greasemonkey cannot even see all the cookies on a page.

Greasemonkey is not the best tool for this, although it may be adequate for select situations.

Here are some far more powerful solutions:

  1. Use Selective Cookie Delete. It keeps the cookies you want and deletes the rest. It does this at the push of a very handy button or automatically when Firefox closes. Both white-lists and black-lists are supported.
  2. Use BetterPrivacy for sneakier LSO's.
  3. Run CCleaner at least once a week, to exorcise a broad spectrum of tracking and cruft.
  4. For powerful, custom, fully-automated cookie removal that does not have the severe limitations that Greasemonkey has, and that runs more often than Selective Cookie Delete, you can write your own browser extension.

这篇关于greasemonkey可以从给定域中删除Cookie吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆