Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome: JavaScript window.open to be Save-able

Imagine a FTP client written in HTML and JavaScript. This part works. But it would be nice if user can "copy the listing" into clipboard. Turns out that clipboard stuff is not so easy in JS (besides, listings can be huge). So better is to pop up a window with the generated listing, then user can chose to Copy'Paste, or Save the page to disk.

Currently I do:

    my_window = window.open("", "Copy List");
    my_window.document.write('<pre&gt\n'+string+'&lt/pre&gt');
    my_window.document.close();

Which works. I get a new tab, and the listing I have generated in "string" displays nicely.

But Chrome disables/greyes-out the "Save Page" option. It would be nice if user can save the page (html or txt). What magic is required to open a window/tab and let them save the content?

Since we use WebSockets (key1/key2) this only works in Chrome, no other browsers needed.

like image 363
lundman Avatar asked Mar 08 '12 02:03

lundman


1 Answers

Way after the fact but you can use a data URI for this:

window.open("data:text/plain;base64,"+btoa(theCode))

like image 79
Fabio Beltramini Avatar answered Sep 19 '22 03:09

Fabio Beltramini