October 4, 2004

Saving text from a web browser

When I write weblog entries, I often find myself thinking, "I'd really like to write some markup where the user can just click a button and save what I've written. Uploading files to my weblog is a pain."

Not any longer! Courtesy of the data: protocol, it is now fairly easy to write a fragment of HTML markup and use this script to quickly convert text in a webpage to a format Mozilla can save to your hard drive!

<form action="javascript:void()">
<fieldset><legend>saveAsFile.js</legend>
<textarea rows="10" cols="80" id="saveAs" readonly="readonly">
<!-- Use whatever id you want, and stick the contents you want the user to save in here. -->
</textarea><br>
<button onclick="saveAsFile('saveAs', 'application/octet-stream')" type="button">Save File</button>
</fieldset>
</form>

The second argument is optional; it simply specifies a content-type for the textarea's contents. If you omit it, the script assumes "application/octet-stream", which is commonly used for downloads.

The only downside to this script is it doesn't specify the filename through the data: protocol. That's not allowed in the protocol, so the user has to manually specify the filename. Hence why I included the filename in the legend element.

Here's the script, ready for you to save per the instructions.

saveAsFile.js

Do not click this button if you are running Mozilla Firefox version 0.10.0 (PR 1) or earlier! You must be running version 0.10.1 or later if you are using Mozilla Firefox. Failure to heed this warning will result in the deletion of files on your hard drive! Mozilla Application Suite is safe to use with any 1.x version. See bug 259708 for details.




Enjoy!

Posted by WeirdAl at October 4, 2004 10:31 PM
Comments

Its all about data:

http://segment7.net/projects/ruby/datafy/

(From Alex: Hey, I never said I was the first to think of it. Ian Hickson's data: URI kitchen is another good tool.)

Posted by: Eric Hodel at October 5, 2004 9:16 AM