删除Cookie:如何获取其路径(和域)?

时间:2018-11-07 19:37:39

标签: javascript cookies

各种文章和SO answer s建议通过JS删除cookie的方式如下:

document.cookie = cookieName + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT;';

或者如果我们需要指定域/路径,例如:

document.cookie = cookieName + "=" +
    (path   ? `;path=${path}` : "") +
    (domain ? `;domain=${domain}` : "") +
    ";expires=Thu, 01 Jan 1970 00:00:01 GMT";

现在,在非根目录(如https://classic.tiddlywiki.com/upgrade/)中,情况更加复杂。如果打开它并在控制台中键入document.cookie,将得到一个值(未指定domainpathexpires):

TiddlyWikiClassicOptions=chkRegExpSearch:%22false%22 chkCaseSensitiveSearch:%22false%22 chkIncrementalSearch:%22true%22 chkAnimate:%22true%22 chkSaveBackups:%22true%22 chkAutoSave:%22false%22 chkGenerateAnRssFeed:%22false%22 chkSaveEmptyTemplate:%22false%22 chkOpenInNewWindow:%22true%22 chkToggleLinks:%22false%22 chkHttpReadOnly:%22true%22 chkForceMinorUpdate:%22false%22 chkConfirmDelete:%22true%22 chkInsertTabs:%22false%22 chkUsePreForStorage:%22true%22 chkDisplayInstrumentation:%22false%22 chkRemoveExtraMarkers:%22false%22 txtBackupFolder:%22%22 txtEditorFocus:%22text%22 txtMainTab:%22Timeline%22 txtMoreTab:%22moreTabAll%22 txtMaxEditRows:%2230%22 txtFileSystemCharSet:%22UTF-8%22 txtTheme:%22%22 txtUserName:%22YourName%22

并在控制台中编写

document.cookie = 'TiddlyWikiClassicOptions=; expires=Thu, 01 Jan 1970 00:00:01 GMT;';

不删除cookie(或更改其值)(document.cookie给出相同的含义)。而且,写作

document.cookie = 'TiddlyWikiClassicOptions=lalala';

不会更改现有的cookie,而是添加另一个具有相同名称的cookie:

TiddlyWikiClassicOptions=lalala; TiddlyWikiClassicOptions=chkRegExpSearch:%22false%22 chkCaseSensitiveSearch:%22false%22 chkIncrementalSearch:%22true%22 chkAnimate:%22true%22 chkSaveBackups:%22true%22 chkAutoSave:%22false%22 chkGenerateAnRssFeed:%22false%22 chkSaveEmptyTemplate:%22false%22 chkOpenInNewWindow:%22true%22 chkToggleLinks:%22false%22 chkHttpReadOnly:%22true%22 chkForceMinorUpdate:%22false%22 chkConfirmDelete:%22true%22 chkInsertTabs:%22false%22 chkUsePreForStorage:%22true%22 chkDisplayInstrumentation:%22false%22 chkRemoveExtraMarkers:%22false%22 txtBackupFolder:%22%22 txtEditorFocus:%22text%22 txtMainTab:%22Timeline%22 txtMoreTab:%22moreTabAll%22 txtMaxEditRows:%2230%22 txtFileSystemCharSet:%22UTF-8%22 txtTheme:%22%22 txtUserName:%22YourName%22

这就是我在干净的Chrome中获得的东西。我还尝试在安装了HTML5 Storage Manager All in One(Chrome扩展程序)的Vivaldi中显示相同的内容,但存在2个cookie的情况除外:而控制台给出的行与上述相同(在{{1}上}),扩展名显示

2 cookies with same names and same values

https://classic.tiddlywiki.com/不会发生这种情况(删除/编辑工作按预期进行)。

现在,我发现必须删除初始Cookie,

document.cookie

如果我添加了document.cookie = 'TiddlyWikiClassicOptions=; path=/; expires=Thu, 01 Jan 1970 00:00:01 GMT;'; ,我也必须这样做

lalala

删除第二个。

我想知道如何确定应该使用哪个路径? document.cookie = 'TiddlyWikiClassicOptions=; expires=Thu, 01 Jan 1970 00:00:01 GMT;'; 似乎对每个Cookie的“范围”保持沉默。还是这是设计引起的,我总是必须知道为哪个路径分配了哪个cookie?

PS我看过一篇帖子,使我认为域(it's in Russian)存在相同的问题,所以我想知道如何也将域与cookie关联。

2 个答案:

答案 0 :(得分:1)

如果您不知道Cookie的(子)域和/或路径,请尝试使所有组合过期。

fname = 'sales_{}.csv'.format(client)
with open(fname, 'w') as output:
    ...

答案 1 :(得分:0)

这是我对dana想法的实现(删除每个路径和域对的cookie):

function deleteCookieFromAllScopes(cookieName)
{
    var domainParts = window.location.hostname.split('.').reverse();
    var topLevelDomain = domainParts.shift();
    var domains = [];
    for(let domainPart of domainParts)
    {
        let prevDomain = domains.slice(-1)[0] || topLevelDomain;
        domains.push(domainPart + '.' + prevDomain);
    }

    var path = window.location.pathname;
    var paths = ['/'], pathLength = 1, nextSlashPosition;
    while( (nextSlashPosition = path.indexOf('/', pathLength)) != -1 )
    {
        pathLength = nextSlashPosition + 1;
        paths.push(path.substr(0, pathLength));
    }

    for(let path of paths)
        for(let domain of domains)
            document.cookie = `${cookieName}=; path=${path}; domain=${domain}; expires=Thu, 01 Jan 1970 00:00:01 GMT;`;
}

毫无疑问,它不再是毫无疑问的,但我认为它更具可读性。欢迎提出改进建议。

这是对通过file:方案打开的页面的cookie的特殊情况的修改:

function deleteCookieFromAllPaths(cookieName)
{
    var path = window.location.pathname;
    var paths = ['/'], pathLength = 1, nextSlashPosition;
    while( (nextSlashPosition = path.indexOf('/', pathLength)) != -1 )
    {
        pathLength = nextSlashPosition + 1;
        paths.push(path.substr(0, pathLength));
    }

    for(let path of paths)
        document.cookie = `${cookieName}=; path=${path}; expires=Thu, 01 Jan 1970 00:00:01 GMT;`;
}