如何在 JavaScript 模块中创建可混淆的变量和函数?

发布于 2024-12-19 03:46:31 字数 943 浏览 1 评论 0原文

我有一组 js 文件,在编译时连接成一个唯一的文件,然后在其上运行 YUI 压缩器(全部在 maven 下)。

不幸的是,文件的内容并没有被混淆。就好像所有声明的变量和方法都被认为是公共的(因此不可混淆)。

例如,a.js 使用 b.js 中声明的方法。拼接文件的内容为:

(function() {

    // b.js' content...

    // a.js' content...
    (function() {

        // Some var and methods of a declared here

        $(document).ready(function(){

            // Calls some a.js and b.js methods
            ...

        });

    })();

})();

b.js 的函数和变量在 b.js 文件中定义如下: a.js

var B1 = "Some text";
var B2 = "More text";
...

function MyPrivateMethod1() { ... };
function MyPrivateMethod2() { ... };

的方法 已被混淆,但 b.js 的未混淆。我可以在串联文件中看到 B1B2MyPrivateMethod1MyPrivateMethod1...,但找不到a.js 的变量和方法(名称已被混淆)。

如何混淆连接文件中 b.js 的 var 和方法? (REM:我需要将 b.js 保留为单独的文件)。

I have a set of js files I concatenate into a unique file at compile time, before running YUI compressor on it (all under maven).

Unfortunately, the content of a file is not obfuscated. It is as if all declared variables and methods were considered public (and therefore not obfuscatable).

For example a.js uses methods declared in b.js. The content of the concatenated file is:

(function() {

    // b.js' content...

    // a.js' content...
    (function() {

        // Some var and methods of a declared here

        $(document).ready(function(){

            // Calls some a.js and b.js methods
            ...

        });

    })();

})();

The functions and variables of b.js are defined as following in the b.js file:

var B1 = "Some text";
var B2 = "More text";
...

function MyPrivateMethod1() { ... };
function MyPrivateMethod2() { ... };

The methods of a.js are obfuscated, but not those of b.js. I can see B1, B2, MyPrivateMethod1, MyPrivateMethod1... in the concatenated file, but I cannot find the variables and methods of a.js (the names have been obfuscated).

How can I obfuscate the var and methods of b.js in the concatenated file? (REM: I need to keep b.js as a separate file).

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

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

发布评论

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

评论(1

断舍离 2024-12-26 03:46:31

我找到了一个解决方案,但我不确定它是最好的。我将 b.js 的代码移至函数中:

var dummy = function() {

    var B1 = "Some text";
    var B2 = "More text";
    ...

    var MyPrivateMethod1 = function() { ... };
    var MyPrivateMethod1 = function() { ... };
    ...

    function MyPublicMethod1() { ... };
    function MyPublicMethod2() { ... };
    ...

}

I found a solution, but I am not sure it is the best one. I moved b.js's code in a function:

var dummy = function() {

    var B1 = "Some text";
    var B2 = "More text";
    ...

    var MyPrivateMethod1 = function() { ... };
    var MyPrivateMethod1 = function() { ... };
    ...

    function MyPublicMethod1() { ... };
    function MyPublicMethod2() { ... };
    ...

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