[Greasemonkey] Convert selected text into a data url.

Jon C jcmmg68hc11 at hotmail.com
Tue Jun 14 22:05:35 EDT 2005


>From: Arvid Jakobsson <arvid.jakobsson at gmail.com>
>Reply-To: greasemonkey at mozdev.org
>To: greasemonkey at mozdev.org
>Subject: [Greasemonkey] Convert selected text into a data url.
>Date: Wed, 15 Jun 2005 00:26:34 +0200
>
>This is my first post at this list, and I know that asking questions
>is not the best way to start ;).
>
>I tried to write a script that allowed the user to converted selected
>text into a data url and the redirect the user to this data url by
>clicking on an item in the User Script Commands submenu.
>This is what I came up with:
>
>// ==UserScript==
>// @name        Selection -> Data URL
>// @namespace
>// @description Adds a item to the context menu which turns currently
>selected text into a Data URL
>// @include     *
>// ==/UserScript==
>
>(function() {
>	GM_registerMenuCommand("Turn Selected text into data url", function () {
>		var selected_text = window.getSelection().toString();
>
>		link = "data:text/html;charset=utf-8," + encodeURI(selected_text);
>//should I use encodeURI() or escape()?
>		location.href = link;
>	});
>})();
>
>the only problem is that when you select two or more lines, the
>linebreak won't show up in selected_text. This causes some javascript
>to stop working, if they use linebreaks instead of semicolon to
>distinguish between statements. I've tried searching google, but
>couldn't find anything.
>I would be grateful for any help.
>_______________________________________________
>Greasemonkey mailing list
>Greasemonkey at mozdev.org
>http://mozdev.org/mailman/listinfo/greasemonkey
Why not use the built-in Mozilla functions for base64 encoding/decoding.

btoa()       for encoding
atob()       for decoding

link = "data:text/html;charset=utf-8;base64," + btoa(selected_text);

or something like that.  I haven't tested this but I have used it for the 
GM_getValue(), GM_setValue() and it works great.

Incidentally, since I'm not sure how to reply to that other thread ( 
[Greasemonkey] Character encoding issues with GM_setValue / Mark Pilgrim ) 
w/o an email to reply to, I'll just state it here:  The btoa/atob functions 
should allow pretty much any data to be stored and retrieved with 
GM_setValue/GM_getValue.  Of course, length of the data might be limited 
however.

_________________________________________________________________
Express yourself instantly with MSN Messenger! Download today - it's FREE! 
http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/



More information about the Greasemonkey mailing list