gpt4 book ai didi

javascript - 删除 cookie : how to get its path (and domain)?

转载 作者:行者123 更新时间:2023-11-30 20:02:48 25 4
gpt4 key购买 nike

各种文章和SO answer建议通过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";

现在,在非 root 位置(如 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 存储管理器的 Vivaldi 中尝试了多合一(Chrome 扩展),它显示相同,除了存在 2 个 cookie 的情况:而控制台给出与上面相同的行(在 document 上.cookie), 扩展显示

2 cookies with same names and same values

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

现在,我认为要删除初始 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;';

删除第二个。

我想知道,如何确定我应该使用哪条路径? document.cookie 似乎对每个 cookie 的“范围”保持沉默。或者这是设计引入的东西,我总是必须知道为哪个路径分配了哪个 cookie?

PS 我看到一篇帖子让我觉得域 ( it's in Russian ) 也存在同样的问题,所以我想知道如何让域也与 cookie 相关联。

最佳答案

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

var hostname = window.location.hostname;
var pathname = window.location.pathname;
var i = -1;
do {
var domain = hostname.substr(i+1);
var j = 0;
do {
var path = pathname.substr(0,j+1);
var cookie = 'TiddlyWikiClassicOptions=; path='+path+'; domain='+domain+'; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
console.log(cookie);
// stacksnippets throws an exception... uncomment to set cookie.
//document.cookie = cookie;
} while ((j = pathname.indexOf('/', j+1)) != -1)
} while ((i = hostname.indexOf('.', i+1)) < hostname.lastIndexOf('.'));

关于javascript - 删除 cookie : how to get its path (and domain)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53196593/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com