从Greasemonkey脚本中删除所有Google Cookie

时间:2011-12-16 11:13:28

标签: javascript cookies greasemonkey

我尝试了很多不同的脚本,但都没有。如何删除Google创建的Cookie或网站的所有Cookie?

1 个答案:

答案 0 :(得分:4)

最好不要使用Greasemonkey。它会很麻烦,可能会错过在页面加载后设置的Cookie,并且只能在您实际浏览Google时删除Google Cookie。

另外,您必须设置脚本的// @include语句,以捕获Google当前和未来的所有域名(google.com,accounts.google.com,mail.google.com,google-analytics.com等) )。如果谷歌服务"Secure cookies"那些也无法触及。

最好使用专为智能删除Cookie而构建的工具。我推荐Selective Cookie Delete 此外,谷歌和其他网站跟踪你的情况远远多于饼干。每周至少运行一次CCleaner是个好主意。


但是,如果您仍想使用Greasemonkey执行此操作,则以下代码将删除运行该脚本的域的许多Cookie:

警告: JavaScript和Greasemonkey甚至无法查看网页上的所有Cookie,也无法删除“安全”(仅限服务器)Cookie。)。

//--- 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);
}

eraseCookie()的位置:
(请注意,此 eraseCookie 通过尝试所有可能的路径和最可能的子域来获取更多cookie。)

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);
}