简体   繁体   English

Greasemonkey / Javascript预处理器和构建系统

[英]Greasemonkey/Javascript preprocessor and build system

I'm writing a moderately complex script for Greasemonkey. 我正在为Greasemonkey编写一个中等复杂的脚本。 Part of it is generating a big blob of HTML and CSS and cramming it into the page. 其中一部分是生成一大堆HTML和CSS并将其填入页面。 I'd like to keep these HTML and CSS blobs as separate files in my source tree, since: 我想将这些HTML和CSS blob保存为源树中的单独文件,因为:

  • Javascript has no multiline strings, so either I get a huge line, or lots of concatenation, or line continuations. Javascript没有多行字符串,所以我得到一个巨大的行,或许多串联,或行延续。 Ugly. 丑陋。
  • The files evolve at different rates, so having them as separate files in Git is notionally better 文件以不同的速率发展,因此将它们作为Git中的单独文件在理论上更好
  • My text editor can set the mode properly when it's not one document embedded in another 我的文本编辑器可以在不是一个文档嵌入另一个文档时正确设置模式

Among many other things. 还有很多其他的事情。

Unfortunately, Greasemonkey scripts are only ever a single script, not a bundle, so I have to inline the HTML and CSS at some point. 不幸的是,Greasemonkey脚本只是一个脚本,而不是一个包,所以我必须在某些时候内联HTML和CSS。 I'm trying to find a good build system for this workflow. 我正在努力为这个工作流程找到一个好的构建系统。 Building for distribution would involve copying from the HTML and CSS into the user script. 构建分发涉及从HTML和CSS复制到用户脚本。

My first instinct was to use make the C preprocessor and #include , but this only works on lines, so doing something like: 我的第一直觉是使用make C预处理器和#include ,但这只适用于行,所以做类似的事情:

var panel = document.createElement('div');
panel.innerHTML = '#include "panel.html"';

Doesn't work. 不行。

What I'm looking for is something exactly like http://js-preprocessor.com/ , but that doesn't throw a "wrong number of arguments" error when I run it. 我正在寻找的东西与http://js-preprocessor.com/完全相同,但是当我运行它时,它不会引发“错误的参数数量”错误。 :P :P

JavaScript, at least for Firefox (Greasemonkey) does have multi-line strings. JavaScript,至少对于Firefox(Greasemonkey) 确实有多行字符串。 So, you can store code in variables without having to futz around with concatenation or \\ chars. 因此,您可以将代码存储在变量中,而无需使用连接或\\ chars。

For example, this works in Firefox: 例如,这适用于Firefox:

var myPageCodeString = (<><![CDATA[

    <style type="text/css">
        .UpperLeft {
            position:           absolute;
            left:               0;
            top:                0;
            background:         orange;
        }
    </style>
    <script type="text/javascript">
        console.log ("Look at me, Ma! I was data, now I'm JS code!");
    </script>

    <div class="UpperLeft">
        <p>Look at me, Ma!</p>
        <p>I was data, now I'm HTML code!</p>
    </div>

]]></>).toString ();


$("body").append (myPageCodeString);

Try it from the console on a page that has jQuery. 在具有jQuery的页面上从控制台尝试它。

Other than that: 除此之外:

  • If the includes are fixed at "compile" time , use @require and/or @resource . 如果包含在“编译”时固定 ,请使用@require和/或@resource
    If an installed script will be updated in place (versus uninstall then reinstall), be sure to rename or "version" any @require or @resource files, so that GM/FF will update the copies on the user's machine. 如果安装的脚本将在适当的位置更新(与卸载然后重新安装),请务必重命名或“版本”任何@require@resource文件,以便GM / FF将更新用户计算机上的副本。

    EG, version: @require http://My_Site/MyJs.js EG,版本: @require http://My_Site/MyJs.js
    to: @require http://My_Site/MyJs.js?vers=2 , etc. to: @require http://My_Site/MyJs.js?vers=2 ,等等

  • If the includes are pulled at run time , use cross-domain AJAX, via GM_xmlhttpRequest() to load the code/data. 如果在运行时提取包含 ,则使用跨域AJAX,通过GM_xmlhttpRequest()加载代码/数据。

Hmm, that's a hard question. 嗯,这是一个很难的问题。 I guess you considered the @require or @resource-thingies , which includes other documents as annoying (since it only gets downloaded when installing/updating the script). 我猜您考虑过@require或@ resource-thingies ,其中包含其他文件很烦人(因为它只在安装/更新脚本时才下载)。

Another option would be to keep the files on the web and have URLs to them, and fetch them when needed. 另一个选择是将文件保留在Web上并为其提供URL,并在需要时获取它们。 This won't work for chrome (same-origin policy), but works for greasemonkey/firefox. 这不适用于chrome(同源策略),但适用于greasemonkey / firefox。 I'd probably use some raw versioning thing (1 file with relative URLs and versions) and localStorage too then, so the files are cached. 我可能会使用一些原始的版本控制(带有相对URL和版本的文件)和localStorage,因此文件被缓存。

I'm not aware of such a tool, but it doesn't seem to be hard to write in a scripting language. 我不知道这样的工具,但用脚本语言编写似乎并不难。 For example, in node.js 例如,在node.js中

var fs = require('fs');

fs.readFile(process.argv[2], "utf-8", function(err, data) {
    console.log(data.toString().replace(/include\s+([\w.]+)/g, function($0, $1) {
        return fs.readFileSync($1).toString().replace(/\n/g, " ");
    }))
})

You can put this in build.js or whatever and call it in your makefile. 您可以将它放在build.js或其他任何内容中,并在makefile中调用它。

Try this one one – @Builder , which among other features supports includes directly from GitHub: https://github.com/electricimp/Builder 尝试这一个 - @Builder ,其中包括其他功能支持直接来自GitHub: https//github.com/electricimp/Builder

Little example 小例子

@include "localFile.js"
@include once "github:jquery/jquery/build/release.js@2.2.3"

@set ABC 123

@if ABC > 123
  //
@else
  //
@end

I ended rolling my own in Python. 我在Python中结束了自己的创作。 It's not exactly trivial (~50 LOC), thg435, but it does the job. 这不是一件容易的事(~50 LOC),而是435,但它确实起到了作用。 Brock Adams' CDATA multiline string makes it easier. Brock Adams的CDATA多线字符串使其更容易。

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

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