Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Posting to Wayback Machine Via Bookmarklet

I am trying to use the "Save Page Feature" to make a bookmarklet that allows a user to push a page to the Internet Archive with a single click.

From what I've gathered, if I post to

http://web.archive.org/save/fullURI

It'll save the page at fullURI (i.e. fullURI=http://www.google.com with all the slashes)

So I wrote the following bookmarklet (white space added for clarity and javascript: removed to force syntax highlighting)

(function(){
    var u='http:\/\/web.archive.org\/save\/'+encodeURI(window.location.href); 
    var w = window.open('', ''); 
    w.document.write('<script> 
        var u = \'' + u +'\'; 
        var x = new XMLHttpRequest(); 
        x.open(\'POST\',u,true); 
        x.send();
    <\/script>')})();

Fiddle here.

So everything swims along, opens a new page, tries a post, and boom, CORS error (oddly from the parent page, not the new window).

Funnily enough I also found that if I open a new window, and paste the URI in, it works, but if I do a window.open(URI) it says please try a POST.

So at this point I'm open to ideas. Is there a good, cross-browser bookmarklet solution to this? Did I overlook something simple trying to reinvent the wheel?

FWIW I'm on Chrome 30 when I try pasting the URI.

like image 822
Jason Nichols Avatar asked Nov 04 '13 22:11

Jason Nichols


2 Answers

You are certainly going to get a CORS problem if you try to use XMLHttpRequest across domains, and that is exactly what you are doing in your new window, because it isn't the same domain as web.archive.org. The closest solution to what you code is trying to do is to write an HTML form to the new window with method=post and submit it, rather than use XMLHttpRequest.

I think, however, that you got confused and started down the wrong path for the wrong reason. This works fine.

window.open("http://web.archive.org/save/" + document.location.href);

As does this

window.open("http://web.archive.org/save/" + encodeURI(document.location.href));

But, somewhat surprisingly, this does not

window.open("http://web.archive.org/save/" + encodeURIComponent(document.location.href));
like image 196
DG. Avatar answered Sep 28 '22 11:09

DG.


This seems to do the trick:

javascript:location.href='http://web.archive.org/save/'+location.href

(Source)

like image 31
TimC Avatar answered Sep 28 '22 11:09

TimC