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

发布于 12-18 13:18 字数 744 浏览 4 评论 0原文

我正在为 Greasemonkey 编写一个中等复杂的脚本。其中一部分是生成一大堆 HTML 和 CSS 并将其塞入页面中。我想将这些 HTML 和 CSS blob 作为单独的文件保留在我的源代码树中,因为:

  • Javascript 没有多行字符串,因此我要么得到一大行,要么得到大量串联,要么得到行延续。丑陋的。
  • 这些文件以不同的速率发展,因此在 Git 中将它们作为单独的文件在理论上会更好。
  • 当它不是一个文档嵌入到另一个文档中时,我的文本编辑器可以正确设置模式

不幸的是,Greasemonkey 脚本只是一个脚本,而不是一个包,所以我必须在某个时候内联 HTML 和 CSS。我正在尝试为这个工作流程找到一个好的构建系统。构建分发版需要将 HTML 和 CSS 复制到用户脚本中。

我的第一反应是使用 make C 预处理器和 #include,但这仅适用于行,因此执行以下操作:

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

不起作用。

我正在寻找的东西与 http://js-preprocessor.com/ 完全相同,但这并不当我运行它时,不要抛出“参数数量错误”错误。 :P

I'm writing a moderately complex script for Greasemonkey. Part of it is generating a big blob of HTML and CSS and cramming it into the page. I'd like to keep these HTML and CSS blobs as separate files in my source tree, since:

  • Javascript has no multiline strings, so either I get a huge line, or lots of concatenation, or line continuations. Ugly.
  • The files evolve at different rates, so having them as separate files in Git is notionally better
  • 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. 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.

My first instinct was to use make the C preprocessor and #include, but this only works on lines, so doing something like:

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. :P

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

匿名的好友2024-12-25 13:18:26

JavaScript,至少对于 Firefox (Greasemonkey) 来说确实有多行字符串。因此,您可以将代码存储在变量中,而不必使用串联或 \ 字符。

例如,这适用于 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);

在具有 jQuery 的页面上从控制台尝试。

除此之外:

  • 如果包含在“编译”时修复,请使用@require 和/或 @resource
    如果要就地更新已安装的脚本(而不是卸载然后重新安装),请务必重命名或“版本”任何 @require@resource 文件,以便 GM/ FF 将更新用户计算机上的副本。

    EG,版本:@require http://My_Site/MyJs.js
    到:@require http://My_Site/MyJs.js?vers=2

  • 如果包含内容在运行时被拉出、使用跨域AJAX,通过GM_xmlhttpRequest()加载

JavaScript, at least for Firefox (Greasemonkey) does have multi-line strings. So, you can store code in variables without having to futz around with concatenation or \ chars.

For example, this works in 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.

Other than that:

  • If the includes are fixed at "compile" time, use @require and/or @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.

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

  • If the includes are pulled at run time, use cross-domain AJAX, via GM_xmlhttpRequest() to load the code/data.

此岸叶落2024-12-25 13:18:26

嗯,这是一个很难的问题。我猜您认为 @require 或 @resource-thingies ,其中包含其他令人讨厌的文档(因为它仅在安装/更新脚本时才会下载)。

另一种选择是将文件保留在网络上并为其提供 URL,并在需要时获取它们。这不适用于 chrome(同源策略),但适用于 Greasemonkey/firefox。我可能会使用一些原始版本控制的东西(1 个带有相对 URL 和版本的文件)和 localStorage,这样文件就会被缓存。

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).

Another option would be to keep the files on the web and have URLs to them, and fetch them when needed. This won't work for chrome (same-origin policy), but works for 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.

谜兔2024-12-25 13:18:26

我不知道有这样的工具,但用脚本语言编写似乎并不难。例如,在 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, " ");
    }))
})

您可以将其放入 build.js 或其他内容中,并在 makefile 中调用它。

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

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.

七度光2024-12-25 13:18:26

我结束了自己的 Python 开发。这并不完全是微不足道的(~50 LOC),thg435,但它可以完成工作。 Brock Adams 的 CDATA 多行字符串使其变得更容易。

I ended rolling my own in Python. It's not exactly trivial (~50 LOC), thg435, but it does the job. Brock Adams' CDATA multiline string makes it easier.

少女七分熟2024-12-25 13:18:26

试试这个 - @Builder,它支持的功能包括直接来自 GitHub:https: //github.com/electricimp/Builder

小例子

@include "localFile.js"
@include once "github:jquery/jquery/build/[email protected]"

@set ABC 123

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

Try this one one – @Builder, which among other features supports includes directly from GitHub: https://github.com/electricimp/Builder

Little example

@include "localFile.js"
@include once "github:jquery/jquery/build/[email protected]"

@set ABC 123

@if ABC > 123
  //
@else
  //
@end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文