我有一些代码具有以下格式:
function myfunc1 () { ... jquery.bind('click', myfunc2) ... }
function myfunc2 () { ... }
...
Run Code Online (Sandbox Code Playgroud)
是的,功能是全局的,但是因为我在谷歌浏览器扩展内容脚本中编写,所以它是沙箱.
现在,我正在尝试缩小和混淆代码.我已经尝试过YUI Compressor和Google Closure编译器.问题是,我无法弄清楚如何缩小/混淆全局函数名称.使用YUI时,如果它们被外部调用,它不会缩小全局变量.在高级模式下关闭,似乎它可以重命名全局变量,但是我遇到了死代码删除的问题.大多数函数似乎都已死,因为它们依赖于DOM交互和事件处理,并且不会直接调用.
那么关于如何缩小这些全局变量的任何想法?我是否需要编写脚本来进行一些正则表达式替换?如果能更好地适应缩小模式(例如,添加到闭包或诸如此类的东西),我也愿意重构我的代码
我正在使用nodejs 0.4.7来发出请求,这是我的代码:
var post_data = JSON.stringify({
'compilation_level' : 'ADVANCED_OPTIMIZATIONS',
'output_format': 'json',
'warning_level' : 'QUIET',
'js_code' : code
});
var post_options = {
host: 'closure-compiler.appspot.com',
port: '80',
path: 'compile',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': post_data.length
}
};
var post_req = http.request(post_options, function(res) {
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('Response: ' + chunk);
});
});
post_req.write(post_data);
post_req.end();
Run Code Online (Sandbox Code Playgroud)
我得到的回应是
Response: <HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>302 Moved</TITLE></HEAD><BODY>
<H1>302 Moved</H1>
The document has moved
<A HREF="http://www.google.com/">here</A>.
</BODY></HTML>
Run Code Online (Sandbox Code Playgroud)
为什么会这样?我究竟做错了什么 ?在教程中它说我很乐意向http://closure-compiler.appspot.com/compile发出POST请求...
我花了半个小时找到这个问题的答案.将它和答案一起发布在这里,希望能节省半个小时.
编译此代码时
"use strict";
/**
* These are the flower and the instrument used in the code below.
*/
var flower, instrument;
Run Code Online (Sandbox Code Playgroud)
Closure Compiler给了我们这个警告
JSC_MULTIPLE_VAR_DEF: declaration of multiple variables with shared type information at line 6 character 0
var flower, instrument;
^
Run Code Online (Sandbox Code Playgroud)
这个含糊的警告意味着什么?
我正在研究Google的样式表重命名功能,我不知道如何重写我的jquery选择器.我没有发现该文件非常明确.
如果我有一些看起来像这样的代码:
<div class="MyClass"></div>
<div id="MyID"></div>
$('.MyClass').someFunc();
$('#MyID').someFunc();
Run Code Online (Sandbox Code Playgroud)
如何编写HTML和javascript以便CSS重命名可以工作?
谢谢你的建议.
javascript jquery google-closure-library google-closure-compiler google-closure-stylesheet
当我在http://closure-compiler.appspot.com上的闭包编译器中测试以下代码时:
// ==ClosureCompiler==
// @output_file_name default.js
// @compilation_level ADVANCED_OPTIMIZATIONS
// @formatting pretty_print
// ==/ClosureCompiler==
// These get renamed
window.foo = {};
window.bar = {};
// These don't
window.uid = {};
window.test = {};
Run Code Online (Sandbox Code Playgroud)
输出是:
window.a = {};
window.b = {};
window.uid = {};
window.test = {};
Run Code Online (Sandbox Code Playgroud)
为什么重命名:
window.foo = {};
window.bar = {};
Run Code Online (Sandbox Code Playgroud)
但不是:
window.uid = {};
window.test = {};
Run Code Online (Sandbox Code Playgroud)
某些词似乎是一个问题?
使用谷歌闭包编译器我收到以下警告消息:
WARNING - If this if/for/while really shouldnt have a body, use {}.
Run Code Online (Sandbox Code Playgroud)
这是文档说的内容:
此警告意味着您在if,for或while语句后面紧跟一个分号.例如:
Run Code Online (Sandbox Code Playgroud)// Produces JSC_SUSPICIOUS_SEMICOLON warning: if (true); else alert('no');
这仅仅是代码约定的事情还是{}比;某些原因更好?
我使用闭包编译器将不同的js文件编译成一个短文件.不可否认,这对于已编译的文件来说效果不是很好,但从来没有这么简单.我从维基页面获得了命令行选项,但它仍然显示警告.任何人都知道我可能做错了什么?
java -jar googleClosureCompiler.jar --jscomp_off=suspiciousCode --js "bootstrap.min.js"
Run Code Online (Sandbox Code Playgroud) javascript java command-line-arguments google-closure-compiler
我正在玩闭包编译器并输入以下代码:
var obj = (function() {
function H(a) {
this.a = a
}
var h = new H(1);
h.b=1
return h
})();
Run Code Online (Sandbox Code Playgroud)
我想看看它是否会将其转换为:
var obj = (function() {
function H(a) {
this.a = a;
this.b = 1
}
var h = new H(1);
return h;
})();
Run Code Online (Sandbox Code Playgroud)
但相反,我得到了这个错误
JSC_NOT_A_CONSTRUCTOR: cannot instantiate non-constructor at line 6 character 8
var h = new H(1);
我究竟做错了什么?
我在Closure Compiler中遇到问题。这是我的功能:
Editor.Elements.Map = function(type, view, x, y) {
var element = type[0].toUpperCase() + type.slice(1);
if(typeof Editor.Elements[element] === 'function') {
return new Editor.Elements[element](view).create(x, y);
}
return null;
}
Run Code Online (Sandbox Code Playgroud)
这将调用这样的类:
/**
* @constructor
* @extends {Editor.Elements.Element}
* @param view (object) {Editor.View}
*/
Editor.Elements.Circle = function(view) {
Editor.Elements.Element.apply(this, arguments);
if (!(view instanceof Editor.View)) {
throw new Error('An Instance of Editor.View is required.');
}
var me = this, _me = {};
...
}
Run Code Online (Sandbox Code Playgroud)
我收到以下警告:
JSC_UNSAFE_NAMESPACE: incomplete alias created for namespace Editor.Elements
Run Code Online (Sandbox Code Playgroud)
指的是这两行:
if(typeof …Run Code Online (Sandbox Code Playgroud) 我有一组简单的实际工作语句(我已经运行了代码,它完成了我对它的期望),如下所示:
result_xml = result.jqxhr.responseXML;
a = jQuery("data[name='calendar']", result_xml).text();
new_calendar = jQuery.parseHTML(a);
jQuery("div.calendar").html(jQuery("div.calendar table.calendar-table", new_calendar));
Run Code Online (Sandbox Code Playgroud)
result来自AJAX的回复,你应该认识到这个jqxhr领域.
我期待a成为一个字符串,因为我得到了一块数据text().数据是我保存在XML文档中的HTML字符串.所以我可以这样投它:
a = /** @type {string} */ jQuery("data[name='calendar']", result_xml).text();
Run Code Online (Sandbox Code Playgroud)
现在我将该字符串转换为HTML DOM,认为它将是一个元素:
new_calendar = /** @type {Element} */ jQuery.parseHTML(a);
Run Code Online (Sandbox Code Playgroud)
这就是我得到这个奇怪错误的地方:
警告 - 无效的强制转换 - 必须是以下的子类型或超类型
:(Array.<(Element | null)> | null)
to:(Element | null)
那么parseHTML()会返回一个元素数组(或null)?
然后我试图使用函数的输出,parseHTML()在html()那里,我很难理解发生了什么.
警告 - 无效的强制转换 - 必须是找到的子类型或超类型:(数组.<(元素| null)> | null)
必需:(文档|元素|对象.| jQuery | null | undefined)
坦率地说,我不明白为什么谷歌编译器会使一个函数的输出不能成为另一个函数的输入,即使它在现实世界中运行得很好.
将数组Element被视为 …
javascript ×8
jquery ×2
casting ×1
java ×1
minify ×1
namespaces ×1
node.js ×1
obfuscation ×1
optimization ×1
yui ×1