javascript - 如何 minfy/混淆全局函数名称?

发布于 2024-11-02 06:52:07 字数 506 浏览 10 评论 0原文

我有一些具有以下格式的代码:

function myfunc1 () { ... jquery.bind('click', myfunc2) ... }
function myfunc2 () { ... }
...

是的,这些函数是全局的,但没关系,因为我是在 google chrome 扩展内容脚本中编写的,所以它是沙箱的。

现在,我正在尝试缩小和混淆代码。我尝试过 YUI Compressor 和 Google Closure 编译器。问题是,我不知道如何缩小/混淆全局函数名称。对于 YUI,它不会缩小全局变量,以防它们被外部调用。在高级模式下使用 Closure 时,它​​似乎可以重命名全局变量,但是我在删除死代码时遇到了问题。大多数函数似乎已经死了,因为它们依赖于 DOM 交互和事件处理并且不直接调用。

那么关于如何缩小这些全局变量有什么想法吗?我是否需要编写一个脚本来进行一些正则表达式替换?如果这能更好地适应缩小模式,我也愿意重构我的代码(例如,添加到闭包或诸如此类的东西)

I have some code that has the following format:

function myfunc1 () { ... jquery.bind('click', myfunc2) ... }
function myfunc2 () { ... }
...

Yes, the functions are global, but it's ok since I'm writing within a google chrome extension content script, so it's sandboxed.

Now, I'm trying to minify and obfuscate the code. I've tried YUI Compressor and the Google Closure compiler. The problem is, I can't figure out how to minify/obfuscate the global function names. With YUI it doesn't minify the global variables in case they're called externally. With Closure in advanced mode, it seems like it can rename the global variables, however I'm having problem with the dead code removal. Most functions appear to be dead since they depend on DOM interaction and event handling and aren't called directly.

So any idea on how to minify these global variables? Do I need to just write a script to do some regex replacement? I'm also open to refactoring my code if that would fit the minification pattern better (for example, adding to a closure or whatnot)

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

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

发布评论

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

评论(4

蓝戈者 2024-11-09 06:52:08

您可以在代码中对常用的 jQuery 函数进行本地引用,然后将其缩小?

例如:

$(function() {
   var jQueryAnimate = $.animate,
       jQueryAddClass = $.addClass;

   $('.foo').jQueryAddClass('.bar');
});

You could make local references to commonly used jQuery functions in your code, which would then be minified?

For example:

$(function() {
   var jQueryAnimate = $.animate,
       jQueryAddClass = $.addClass;

   $('.foo').jQueryAddClass('.bar');
});
终陌 2024-11-09 06:52:07

缩小器不会修改公共/全局名称,因为对于许多脚本来说,这会破坏公共 API 的可用性和可预测性。

由于您不需要维护公共 API,因此通过将它们包装在闭包函数中来使它们成为“私有”可能就足够了:

(function () {

    function myfunc1 () { ... jquery.bind('click', myfunc2) ... }
    function myfunc2 () { ... 
    ...

})();

但是,即使这样,也不能保证,因为这在很大程度上取决于压缩器作者的判断。

Minifiers won't munge public/global names since, for many scripts, that will destroy the availability and predictability of the public API.

Since you don't need to maintain a public API, making them "private" by wrapping them in a closure function may be enough:

(function () {

    function myfunc1 () { ... jquery.bind('click', myfunc2) ... }
    function myfunc2 () { ... 
    ...

})();

But, even then, no guarantees as it's very much at the discretion of the minifier's authors.

虫児飞 2024-11-09 06:52:07

请参阅语义设计 JavaScript 混淆器。让您可以完全控制哪些符号被混淆,哪些符号未被混淆,这样您就可以管理此类情况。无需更改您的工作代码。

我为他们工作。

See the Semantic Designs JavaScript Obfuscator. Gives you complete control over what symbols are obfuscated, and which are not, precisely so you can manage situations like this. No need to change your working code.

I work for them.

反目相谮 2024-11-09 06:52:07

请参阅闭包编译器的在线文档。

换句话说:

  1. “导出”您想要保留的函数
  2. 使用您不想重命名的函数创建一个“externs”文件
  3. 在高级模式下运行闭包编译器

Please see the on-line docs for the Closure Compiler.

In other words:

  1. "Export" the functions you want to keep
  2. Make an "externs" file with functions you don't want renamed
  3. Run Closure Compiler in Advanced Mode
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文