最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript to "export" all cookies? - Stack Overflow

programmeradmin4浏览0评论

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 site:

# Netscape HTTP Cookie File
# .html
# This is a generated file!  Do not edit.

cognition.happycog  FALSE   /   FALSE   1345696044  exp_last_visit  998800044
cognition.happycog  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.

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.

Share Improve this question edited Sep 1, 2011 at 1:58 cwd asked Aug 31, 2011 at 21:31 cwdcwd 54.8k54 gold badges169 silver badges199 bronze badges 2
  • You can't directly access the file system from javascript, so if it's a file export you want using JS alone, it can't be done... – DaveRandom Commented Aug 31, 2011 at 21:35
  • I realize that. That's why I put "export" in quotations. 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. I'll add this to the question. – cwd Commented Sep 1, 2011 at 1:56
Add a comment  | 

3 Answers 3

Reset to default 13
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];
});

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?

All cookies for the page is stored in document.cookie

发布评论

评论列表(0)

  1. 暂无评论