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.
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));
This seems to do the trick:
javascript:location.href='http://web.archive.org/save/'+location.href
(Source)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With