Greasemonkey/Javascript 预处理器和构建系统
我正在为 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 技术交流群。

发布评论
评论(5)
嗯,这是一个很难的问题。我猜您认为 @require 或 @resource-thingies ,其中包含其他令人讨厌的文档(因为它仅在安装/更新脚本时才会下载)。
另一种选择是将文件保留在网络上并为其提供 URL,并在需要时获取它们。这不适用于 chrome(同源策略),但适用于 Greasemonkey/firefox。我可能会使用一些原始版本控制的东西(1 个带有相对 URL 和版本的文件)和 localStorage,这样文件就会被缓存。
我不知道有这样的工具,但用脚本语言编写似乎并不难。例如,在 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 中调用它。
试试这个 - @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
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
JavaScript,至少对于 Firefox (Greasemonkey) 来说确实有多行字符串。因此,您可以将代码存储在变量中,而不必使用串联或
\
字符。例如,这适用于 Firefox:
在具有 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:
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.