Javascript“导出”所有饼干?

发布于 2024-12-02 06:46:46 字数 892 浏览 2 评论 0原文

有一个很酷的 Firefox 扩展,它可以让您导出所有 cookie到 Netscape HTTP Cookies 文件 cookies.txt,然后您可以将其与 wget (等)一起使用,

这是一个示例 cookies.txt 文件happycog.com site:

# Netscape HTTP Cookie File
# http://www.netscape.com/newsref/std/cookie_spec.html
# This is a generated file!  Do not edit.

cognition.happycog.com  FALSE   /   FALSE   1345696044  exp_last_visit  998800044
cognition.happycog.com  FALSE   /   FALSE   1345696044  exp_last_activity   1314160044

如何使用Javascript构建相同样式的“cookies导出”?当然,它只能读取当前域的 cookie。那就好了。

其他详细信息:

我意识到 cookie 无法使用纯 JavaScript 导出到文件系统。我很高兴将它们导出到文本区域或 document.write。我只是想知道如何以相同的格式获取它们,这样我基本上可以将它们复制并粘贴到 cookies.txt 文件中。不过,这里的挑战是使用 javascript 来完成,而不是使用插件。

There is a cool Firefox extension which lets you export all cookies to a Netscape HTTP Cookies File, cookies.txt, which you can then use with wget (et.al.)

Here is an example cookies.txt file for the happycog.com site:

# Netscape HTTP Cookie File
# http://www.netscape.com/newsref/std/cookie_spec.html
# This is a generated file!  Do not edit.

cognition.happycog.com  FALSE   /   FALSE   1345696044  exp_last_visit  998800044
cognition.happycog.com  FALSE   /   FALSE   1345696044  exp_last_activity   1314160044

How can I build the same style "cookies export" with Javascript? Granted it would only be able to read cookies for the current domain. That would be just fine.

Additional Details:

I realize that cookies can't be exported to the file system with pure javascript. I'd be happy with them being exported to a textarea, or with document.write. I just want to know how I can get them in the same format where I can basically copy and paste them to a cookies.txt file. The challenge here is to do it with javascript, though, and not to use an addon.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

喜爱纠缠 2024-12-09 06:46:46
var cookieData = document.cookie.split(';').map(function(c) {
  var i = c.indexOf('=');
  return [c.substring(0, i), c.substring(i + 1)];
});

copy(JSON.stringify(JSON.stringify(cookieData)));

这会将您的 cookie 导出到键/值对数组中(例如 [ [key1, val1], [key2, val2], ...]),然后将其复制到剪贴板。它不会保留任何过期日期信息,因为无法通过 JavaScript 提取这些信息。


要重新导入 cookie,您需要运行以下代码:

var cookieData = JSON.parse(/*Paste cookie data string from clipboard*/);
cookieData.forEach(function (arr) {
  document.cookie = arr[0] + '=' + arr[1];
});
var cookieData = document.cookie.split(';').map(function(c) {
  var i = c.indexOf('=');
  return [c.substring(0, i), c.substring(i + 1)];
});

copy(JSON.stringify(JSON.stringify(cookieData)));

This will export your cookies into an array of key/value pairs (eg. [ [key1, val1], [key2, val2], ...]), and then copy it to your clipboard. It will not retain any expiration date info because that's impossible to extract via Javascript.


To import the cookies back, you'd run the following code:

var cookieData = JSON.parse(/*Paste cookie data string from clipboard*/);
cookieData.forEach(function (arr) {
  document.cookie = arr[0] + '=' + arr[1];
});
梦里的微风 2024-12-09 06:46:46

抱歉回复延迟了 - 必须睡觉了。我刚刚玩过这个,得出的结论是答案是

原因是 Javascript 无法访问除 cookie 名称和值之外的任何信息,您无法检索路径、到期时间等。您实际上想做什么?这是针对您正在开发的某个特定网站还是仅用于浏览时的一般用途?

Sorry about the delayed response - had to sleep. I have just been playing with this and concluded that the answer is NO.

The reason for this is that Javascript does not have access to any more information than the cookie's name and value, you can't retrieve the path, expiry time, etc etc. What do you actually want to do? Is this for one specific site you are developing or just for general use when browsing?

南渊 2024-12-09 06:46:46

该页面的所有 cookie 都存储在 document.cookie

All cookies for the page is stored in document.cookie

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文