用于设置和读取Cookie的书签 [英] Bookmarklet for set and read cookies

查看:71
本文介绍了用于设置和读取Cookie的书签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要(为了练习)通过网站X中的小书签设置cookie,并与网站Y中的另一个小书签一起阅读他.

I need (for practice) to set a cookie via bookmarklet in website X, and read him with another bookmarklet from website Y.

例如,在Google中设置一个名为用户"且值为"Guy"的cookie,然后从YouTube上读取它.

For example, set a cookie named "user" with value of "Guy" in Google, and read this from YouTube.

我设法设置了cookie,但是想不出如何从b网站读取它的想法.

I managed to set the cookie, but can't think of any idea how to read him from website b.

谢谢!

推荐答案

我一直在寻找一种与朋友共享特定网站的Cookie的方法(通过书签在我的浏览器中阅读它们,而我的朋友也在他的浏览器中设置它们)通过小书签).并不是您想要的,但搜索使我来到了这里.这是我的方法:

I was looking for a way to share cookies of a specific website with a friend (reading them in my browser via bookmarklet and my friend setting them on his browser also via bookmarklet). Not quite what you asked for, but searching brought me here. This is my approach:

首先,有一个用于导出Cookie的小书签.它将删除不必要的空格并将您的数据编码为base64字符串以进行安全传输:

First there is a bookmarklet for exporting cookies. It will remove unnecessary white-spaces and encode your data in a base64 string for safe transport:

javascript:(
function(){
    prompt("GET cookies encoded in base64", btoa(document.cookie.replace(/\s/ig, "")));
    }
)
();

然后还有第二个小书签,用于导入字符串中编码的所有cookie.您还可以在此处设置可选的生存期(由于 https://www.quirksmode.org/js/cookies.html ):

Then there is a second bookmarklet for importing all cookies encoded in the string. You can also set an optional lifetime here (thanks to https://www.quirksmode.org/js/cookies.html):

javascript:(
function(){
    var inputstring = prompt("SET cookies decoded from base64");
    var inputclean = atob(inputstring).replace(/\s/ig, "");
    if (confirm("These cookies will be imported:\n\n" + inputclean.replace(/;/ig, "; "))) {
    var days = prompt("Cookie lifetime in full days", "365");
    var cookiearray = inputclean.split(";");
    cookiearray.forEach(function(entry) {
        var expires = "";
        var split = entry.split("=");
        if (days) {
            var date = new Date();
            date.setTime(date.getTime() + (days*24*60*60*1000));
            expires = "; expires=" + date.toUTCString();
        }
        document.cookie = split[0] + "=" + (split[1] || "")  + expires + "; path=/";
    });
    }
    }
)
();

别忘了您必须在特定网站或选项卡上运行它们.它不会导出浏览器存储的cookie的全部集合.

Do not forget you have to run those on a specific website or tab. It does NOT export the entire collection of the cookies your browser is storing.

这篇关于用于设置和读取Cookie的书签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆