6

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.

sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
Jason Nichols
  • 3,739
  • 3
  • 29
  • 49
  • maybe this will help you: javascript:(function(){cas=document.createElement('SCRIPT');cas.type='text/javascript';cas.src=(window.location.protocol)+'//linkbook.co/index.php/api/v1/GetBookmarklet?version=12.9.2&url='+encodeURIComponent(location.href)+'&userAgent='+(JSON.stringify(navigator.userAgent))+((document.doctype)?('&doctype='+(document.doctype.name)):'')+'&x='+(Math.random());document.getElementsByTagName('head')[0].appendChild(cas);})(); – Ionut Flavius Pogacian Nov 05 '13 at 07:33
  • maybe you should take a look at www.linkbook.co; signup, get bookmarklet ... add your data to archive; also, you could inspect the js code – Ionut Flavius Pogacian Nov 05 '13 at 07:34

2 Answers2

4

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));
DG.
  • 3,417
  • 2
  • 23
  • 28
  • when I tried the simple `window.open()` I wound up getting a page back from them that said I had to use POST... Curious why that worked for you and not for me. – Jason Nichols Nov 05 '13 at 15:19
1

This seems to do the trick:

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

(Source)

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
TimC
  • 31
  • 4