Greasemonkey通过通配符阻止cookie

时间:2015-01-21 23:57:12

标签: cookies greasemonkey tampermonkey

我有一个名为Cookie##的Cookie,其中##是一个随机数。有没有办法使用Greasemonkey来阻止任何以" Cookie"开头的cookie?被封锁?

我找到this回答,但这只会阻止某个域的特定Cookie。我如何通过通配符将其扩展为阻止。

2 个答案:

答案 0 :(得分:1)

找出所有的Cookie都以" Cookie"然后一个接一个地做同样的答案。

document.cookie.split('; ')
    .map(function (x) { return x.split('=', 1)[0]; })
    .filter(function (x) { return x.substring(0, 6) === 'Cookie'; })
    .forEach(function (name){
        // set the domain
        var domain = ".jsfiddle.net";

        // get a date in the past
        var expireDate = new Date(-1).toUTCString();

        // clear the cookie and force it to expire
        document.cookie = name + "=; domain=" + domain + "; path=/; expires=" + expireDate;

    });

答案 1 :(得分:1)

您无法 阻止 Cookie,但您可以在设置完成后将其删除(¡but not always!)。

获取“Cookie ###”Cookie的列表,并使用正则表达式遍历它们:

var mtch;
var targCookRgx = /[; ]*(Cookie\d+)=/gi;
//-- Doc.cookie value will be changing in while()
var oldCookie   = document.cookie;

while ( (mtch = targCookRgx.exec (oldCookie) ) != null) {
    eraseCookie (mtch[1]);
}

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) {
        console.log ("cookieName to delete: ", 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;";
            //console.log ("--> Cookie str: ", diagStr);
            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);
}