简体   繁体   English

将javascript嵌入为base64

[英]Embed javascript as base64

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 which is served from google. 我正在研究一个小的GreaseMonkey脚本,我想在其中嵌入一个jQuery插件(Markitup),以便脚本完全自包含(images + js),除了从谷歌提供的jQuery。

I found the site http://www.greywyvern.com/code/php/binary2base64 which says that you can embed javascript with the href if you base64 encode the script, much like serving images as basse64 from CSS. 我发现网站http://www.greywyvern.com/code/php/binary2base64 ,如果你对base64编码脚本,你可以用href嵌入javascript,就像从CSS提供basse64图像一样。

<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. 所以我尝试了但是根本无法使用OS X上的Firefox 3.0.5。

I put together a small test page to isolate the problem but couldn't get it to work at that page either. 我把一个小的测试页面放在一起来隔离问题,但也无法让它在那个页面上工作。

<!DOCTYPE HTML>
<html>
   <head>
      <title>Title</title>
      <meta charset="utf-8">
   </head>
   <body>
      <script href="data:text/javascript;base64,YWxlcnQoJ2FzYWRhc2QnKTsK"></script> 
      <script>alert('a');</script>  
   </body>
</html>

The base64 string (YWxlcnQoJ2FzYWRhc2QnKTsK) says alert('asadasd'); base64字符串(YWxlcnQoJ2FzYWRhc2QnKTsK)表示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? 我做错了什么或为什么这不起作用,任何想法?

也许只是一个想法,但也许“href”应该是“src”而不是。

If you'd check script tag syntax you'd get 如果你检查脚本标记语法,你会得到

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

Using data URIs here doesn't work in IE 6 btw. 在这里使用数据URI在IE 6 btw中不起作用。

That's funny, I am working on precisely the same problem: making a Greasemonkey script to add markItUp to all textareas of a page. 这很有趣,我正在研究同样的问题:制作一个Greasemonkey脚本,将markItUp添加到页面的所有textareas中。

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. 无论如何,我不明白为什么要将它作为Base64插入。 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 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: 你可以直接把它放在GM脚本中(有些人用jQuery做),或者动态地将<script src="someURL"></script>到文档中并等待加载(有很多例子在网上)(不便之处:在持有文件的网站上创建流量),或者,就像我目前正在尝试的那样,使用GM的最新(0.8)功能:

// @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. 所需的JS文件将从本地副本自动加载到GM脚本中,该副本速度快且始终可用。 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. 我也遇到了set.js的问题,所以我直接在脚本中输入。
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. 我认为,我必须将miuStyle文本放在网页中,然后更改它,以便所有背景图像都指向托管在Photobucket上的相同图像或类似图像,并带有偏移量。 I haven't found a way to use local images (in CSS), alas, even with @resource. 我还没有找到一种方法来使用本地图像(在CSS中),唉,甚至使用@resource。

My response doesn't address your problem, alas (but John's remark seems valid), but it might put you to another, simpler solution. 我的回答没有解决你的问题,唉(但约翰的评论似乎有效),但它可能会让你找到另一个更简单的解决方案。

Base-64 makes the file larger, but it makes it more machine readable so it actually increases pagespeed. Base-64使文件更大,但它使机器更易读,因此它实际上增加了pagespeed。 I can definitely see why you'd want this, unless you want to look at 50 lines of gibberish in your file. 我绝对可以理解你为什么要这样,除非你想在文件中查看50行乱码。 FYI, I just read a study that base 64 actually loads slower on mobile so if your app is heavy mobile I'd stay away 仅供参考,我刚刚阅读了一项基于64实际上在移动设备上加载速度较慢的研究,因此,如果您的应用程序是重型移动设备,我会远离它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM