views:

1366

answers:

4

Hello, I'm working on a small GreaseMonkey script where I would like to embed a jQuery plugin (Markitup) so that the script is fully self contained (images + js) except for jQuery who is served from google.

I found the site http://www.greywyvern.com/code/php/binary2base64 wich says that you can embed javascript with the href if you base64 encode the script, much like serving images as basse64 from CSS.

<script type="text/javascript" href="data:text/javascript;base64,dmFyIHNjT2JqMSA9IG5ldyBzY3Jv..."></script>

So i tried that but couldn't get it to work at all, using Firefox 3.0.5 on OS X.

I put together a small testpage to isolate the problem but couldn't get it to work at that page either.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd"&gt;
<html>
   <head>
      <title>Title</title>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
   </head>
   <body>
      <script type="text/javascript" charset="utf-8" href="data:text/javascript;base64,YWxlcnQoJ2FzYWRhc2QnKTsK"></script> 
      <script type="text/javascript" charset="utf-8">alert('a');</script>  
   </body>
</html>

The base64 string (YWxlcnQoJ2FzYWRhc2QnKTsK) says alert('asadasd'); so I'm supposed to get two alerts but the only one I see is the second one where the alert is located as text inside the script tag.

Am I doing something wrong or why isn't this working, any ideas?

+4  A: 

maybe just a thought but maybe the "href" should be "src" instead.

John Boker
Thanks, the example on the site is wrong and I never thought about checking that.
Daniel Johansson
+2  A: 

If you'd check script tag syntax you'd get

<script type="..." src="..."
                   ^^^
                   src NOT href

It doesn't work in IE 6 btw

romke
A: 

Did you try getting rid of the charset="utf-8"? His example does not have that.

Greg Dean
+1  A: 

That's funny, I am working on precisely the same problem: making a Greasemonkey script to add markItUp to all textareas of a page.

Now, I don't have an issue with the library script itself. I don't see why you want to insert it as Base64, anyway. As pointed out, it will be larger.
You can put it directly in the GM script if you want (some people do that with jQuery), or add dynamically a <script type="text/javascript" src="someURL"></script> to the document and wait for loading (there are numerous examples of that on the Net)(inconvenience: creates a traffic on the sites holding the files), or, like I am currently trying, using the latest (0.8) feature of GM:

// @require      jquery.js
// @require      markitup.js
// @resource     miuStyle style.css

The required JS files are automatically loaded into the GM script from a local copy, which is fast and always available. Don't use the packed versions, they don't work here. I also had issues with set.js so I just put in directly in the script.
It works well, but I don't have style nor icons yet.
I must put the miuStyle text in the Web page, I think, and change it so all background images refer to the same image hosted on Photobucket or similar, with offset. I haven't found a way to use local images (in CSS), alas, even with @resource.

My response doesn't address your problem, alas (but John's remark seems valid), but it might put you to another, simpler solution.

PhiLho
Daniel Johansson
Reminder: @require load the file only once, when the user script is installed. After that, it lives on the user's profile on its hard disk, and each use of the script makes no hit on the server.I suspected I could use base64 encode images in CSS but I haven't looked at the syntax yet.
PhiLho