1

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

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

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

Thanks!

GuyChabra
  • 105
  • 1
  • 3
  • 12

3 Answers3

1

You need two bookmarklets, a getter and a setter.

You go to site X and use the getter bookmarklet to read the cookie and let the user copy it to his clipboard.

Then you go to site Y and use the setter. The setter will prompt the user for the bookmarklet and the user will then paste it into the prompt. The code will then set the cookie accordingly.

You can of course combine these two bookmarklets into a single getter/setter. The prompt will contain the current cookie for the page. The user can then choose to either copy the cookie and cancel (using it as a getter) or choose to to alter the cookie and click "OK" (using it as a setter).

DG.
  • 3,417
  • 2
  • 23
  • 28
  • I maneged to do the setter bookmarklet by using Document.cookie, I want to user for ex an iframe in order to get the cookie i set in website A from website B. – GuyChabra Jun 11 '13 at 20:23
  • If you do not control and own one of the websites, what you describe is impossible. – DG. Jun 12 '13 at 02:49
1

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:

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, "")));
    }
)
();

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=/";
    });
    }
    }
)
();

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.

Amnesius
  • 11
  • 3
0

According to this StackOverflow, how to get cookies from a different domain with php and javascript you can't get cookies from another domain UNLESS you have access to it, as it would be a huge security flaw.

Community
  • 1
  • 1
pandavenger
  • 1,017
  • 7
  • 20