使用grunt-obfuscator混淆js文件:未捕获ReferenceError:require未定义 [英] Obfuscating js files with grunt-obfuscator: Uncaught ReferenceError: require is not defined

查看:167
本文介绍了使用grunt-obfuscator混淆js文件:未捕获ReferenceError:require未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在互联网上搜索如何破解我的JavaScript代码,并在使用uglify将所有文件缩小但未混淆后,我决定使用 grunt-obfuscator



在我的简单项目中进行此配置后:



Gruntfile.js

  module.exports = function(grunt){

grunt.loadNpmTasks('grunt-obfuscator');

grunt.initConfig({
connect:{
server:{
options:{
port:9000,
base:'app /'
}
}
},
观看:{
项目:{
文件:['app / ** / *。js', 'app / ** / *。html','app / ** / *。json','app / ** / * .css'],
选项:{
livereload:true



obfuscator {
files:[
'app / js / app.js',
'app / js /controllers.js'
],
条目:'app / js / app.js',
out:'app / js / obfuscated.js',
strings:true ,
root:__dirname
}
});

grunt.loadNpmTasks('grunt-contrib-connect');
grunt.loadNpmTasks('grunt-contrib-watch');


grunt.registerTask('default',['obfuscator','connect','watch']);

};

我的app.js

 (function(){

var app = angular.module('myapp',[
'ngRoute',
'myapp.controllers'
]);

app.config(['$ routeProvider',function($ routeProvider){

$ routeProvider
.when('/',{
templateUrl:'views / home.html'
})
.otherwise({
redirectTo:'/'
} );
}]);

})();

我的controller.js

 (function(){
angular.module('myapp.controllers',[])
.controller('AppController',['$范围',
函数($ scope){
$ scope.name =Test123;
}]);
})();

执行 grunt 后,我的 obfuscated.js 真的很难阅读,这真的很棒。所以我进入我的本地主机,现在正在加载 obfuscated.js 文件,但我得到这个错误:

 未捕获的ReferenceError:require未定义




  1. 我缺少什么?

我的obfuscated.js

 !function(a,b){function c(b,d){var e,f; if(\x2e!= b [ 0]&&\x2f!= b [0])返回a(b); if(d = d ||\x72\x6f\x6f\x74,e = c。解析(b),!e&& /\.json$/i.test(b))返回一个(\x2e\x2f+ c.basename(b)); if(f = c。缓存[e],!f)try {return a(b)} catch(g){throw Error(\x66\x61\x69\x6c\x65\x64\x20\x74\\ \\x6f\x20\x72\x65\x71\x75\x69\x72\x65\x20\x22 + b + \x22\x20\x66\x72\ (f.exports = {},f.call(f。)。出口,F,f.exports,c.relative( e))的),f.exports} c.cache = {},c.basename = A( \x70\x61\x74\x68)。基名,c.resolve =函数(B){风险如果(\ x2e!= b [0])返回a.resolve(b); for(d = [b,b +\x2e\x6a\x73,b + \x2f\x69\x6e\x64\x65\x78\x2e\x6a\x73\" ,b + \x2e\x6a\x73\x6f\x6e,b + \x2f\x69\x6e\x64\x65\x78\x2e\x6a\x73\x6f\x6e],E = 0; F = d [E],E ++)如果(c.cache [f])return f},c.register = function(a,b){c.cache [a] = b},c.relative = function(a){function b(b){var d ,e,f,g,h; if(\x2e!= b [0])return c(b); for(d = a.split(\x2f),e = b.split \x2f),d.pop()中,f = 0,G = e.length g取代; F; F + = 1)H = E [F], \x2e\x2e == H + d.pop():\x2e!= h& d.push(h); return c(d.join(\x2f),a)} return b.resolve = c.resolve, b.cache = c.cache,b},c.register(\x2e\x2f\x61\x70\x70\x2f\x6a\x73\x2f\x61\x70\\ \\x70\x2e\x6a\x73\" ,功能(一个,b,c){!function(){var a = angular.module(\x6d\x79\x61\x70\x70,[\x6e\x67\x52\x6f \x75\x74\x65\" ,\x6d\x79\x61\x70\x70\x2e\x63\x6f\x6e\x74\x72\x6f\ x6c\x6c\x65\x72\x73 ]); a.config([ \x24\x72\x6f\x75\x74\x65\x50\x72\x6f \x76\x69\x64\x65\x72\" ,功能的(a){a.when( \x2f,{templateUrl:\x76\x69\x65\x77\ x73\x2f\x68\x6f\x6d\x65\x2e\x68\x74\x6d\x6c })否则({redirectTo: \x2f})}]) ,c.extensions [\x2e\x73\x65\x72\x76\x65\x72\x2e\x63\x6f\x6e\x74\x72\x6f\ x6c\x6c\x65\x72\x2e\x6a\x73 ] = c.extensions [ \x2e\x6a\x73 ],c.extensions [ \x2e\x73 \x65\x72\x76\x65\x7 2\x2e\x6d\x6f\x64\x65\x6c\x2e\x6a\x73 ] = c.extensions [ \x2e\x6a\x73\" ],C。扩展[\x2e\x73\x65\x72\x76\x65\x72\x2e\x72\x6f\x75\x74\x65\x73\x2e\ x6a\x73 ] = c.extensions [ \x2e\x6a\x73 ]}()}),c.register( \x2e\x2f\x61\x70\x70\\ \\x2f\x6a\x73\x2f\x63\x6f\x6e\x74\x72\x6f\x6c\x6c\x65\x72\x73\x2e\x6a \x73\" ,函数(){!函数(){angular.module(\x6d\x79\x61\x70\x70\x2e\x63\x6f\x6e\x74\\ \\x72\x6f\x6c\x6c\x65\x72\x73\" ,[])。控制器(\x41\x70\x70\x43\x6f\x6e\x74 \x72\x6f\x6c\x6c\x65\x72\" ,[ \x24\x73\x63\x6f\x70\x65,函数(){a.name =\x54\x65\ x73\x74\x31\x32\x33 }])}()}),b.exports = C( \x2e\x2f\x61\x70\x70\x2f\ x6a\x73\x2f\x61\x70\x70\x2e\x6a\x73\" )}(需要,模块); 

更新
此问题没有答案,插件不是为angularjs创建的,仅针对Node.js.而创作者并没有最小的意图来制作角度js。 :(但它是一个很酷的工具

解决方案

这里有几个问题。



我是否需要混淆我所有的js文件才能运行?
没有



一个混淆文件可以完美处理您的所有依赖关系NON-obfuscated?

我缺少什么?
很难说,uglification可能非常棘手。
首先,我将验证依赖注入是否完成,或者您可以使用 ngAnnotate 为你做
依赖关系的定义如下:

  .service('myService',['$ rootScope',function($ rootScope){...}]); 

如果这样不能解决您的问题,那么您必须挖掘似乎发送问题的requireJS。

编辑您的代码并粘贴main.js我们看到你想要丑化的东西


I was searching over the internet about how to ofuscate my javascript code and after use uglify that convert all my files minified but not obfuscated I decided to use grunt-obfuscator

After make this configuration on my simple project:

Gruntfile.js

  module.exports = function (grunt) {

  grunt.loadNpmTasks('grunt-obfuscator');

  grunt.initConfig({
    connect: {
      server: {
        options: {
          port: 9000,
          base: 'app/'
        }
      }
    },
    watch: {
      project: {
        files: ['app/**/*.js', 'app/**/*.html', 'app/**/*.json', 'app/**/*.css'],
        options: {
          livereload: true
        }
      }
    },
    obfuscator: {
      files: [
        'app/js/app.js',
        'app/js/controllers.js'
      ],
      entry: 'app/js/app.js',
      out: 'app/js/obfuscated.js',
      strings: true,
      root: __dirname
    }
  });

  grunt.loadNpmTasks('grunt-contrib-connect');
  grunt.loadNpmTasks('grunt-contrib-watch');


  grunt.registerTask('default', ['obfuscator', 'connect', 'watch']);

};

My app.js

(function () {

    var app = angular.module('myapp', [
            'ngRoute',
            'myapp.controllers'
        ]);

    app.config(['$routeProvider', function ($routeProvider) {

        $routeProvider
            .when('/', {
                templateUrl: 'views/home.html'
            })
            .otherwise({
                redirectTo: '/'
            });
    }]);

})();

My controller.js

(function() {
    angular.module('myapp.controllers', [])
        .controller('AppController', ['$scope',
         function ($scope) {
            $scope.name = "Test123";
        }]);
})();

After execute grunt my obfuscated.js is really hard to read, wich is really great. So I'm enter to my localhost wich is loading now the obfuscated.js file but I'm getting this error:

Uncaught ReferenceError: require is not defined

  1. What am I missing?

My obfuscated.js

!function(a,b){function c(b,d){var e,f;if("\x2e"!=b[0]&&"\x2f"!=b[0])return a(b);if(d=d||"\x72\x6f\x6f\x74",e=c.resolve(b),!e&&/\.json$/i.test(b))return a("\x2e\x2f"+c.basename(b));if(f=c.cache[e],!f)try{return a(b)}catch(g){throw Error("\x66\x61\x69\x6c\x65\x64\x20\x74\x6f\x20\x72\x65\x71\x75\x69\x72\x65\x20\x22"+b+"\x22\x20\x66\x72\x6f\x6d\x20"+d+"\n"+g.message+"\n"+g.stack)}return f.exports||(f.exports={},f.call(f.exports,f,f.exports,c.relative(e))),f.exports}c.cache={},c.basename=a("\x70\x61\x74\x68").basename,c.resolve=function(b){var d,e,f;if("\x2e"!=b[0])return a.resolve(b);for(d=[b,b+"\x2e\x6a\x73",b+"\x2f\x69\x6e\x64\x65\x78\x2e\x6a\x73",b+"\x2e\x6a\x73\x6f\x6e",b+"\x2f\x69\x6e\x64\x65\x78\x2e\x6a\x73\x6f\x6e"],e=0;f=d[e];e++)if(c.cache[f])return f},c.register=function(a,b){c.cache[a]=b},c.relative=function(a){function b(b){var d,e,f,g,h;if("\x2e"!=b[0])return c(b);for(d=a.split("\x2f"),e=b.split("\x2f"),d.pop(),f=0,g=e.length;g>f;f+=1)h=e[f],"\x2e\x2e"==h?d.pop():"\x2e"!=h&&d.push(h);return c(d.join("\x2f"),a)}return b.resolve=c.resolve,b.cache=c.cache,b},c.register("\x2e\x2f\x61\x70\x70\x2f\x6a\x73\x2f\x61\x70\x70\x2e\x6a\x73",function(a,b,c){!function(){var a=angular.module("\x6d\x79\x61\x70\x70",["\x6e\x67\x52\x6f\x75\x74\x65","\x6d\x79\x61\x70\x70\x2e\x63\x6f\x6e\x74\x72\x6f\x6c\x6c\x65\x72\x73"]);a.config(["\x24\x72\x6f\x75\x74\x65\x50\x72\x6f\x76\x69\x64\x65\x72",function(a){a.when("\x2f",{templateUrl:"\x76\x69\x65\x77\x73\x2f\x68\x6f\x6d\x65\x2e\x68\x74\x6d\x6c"}).otherwise({redirectTo:"\x2f"})}]),c.extensions["\x2e\x73\x65\x72\x76\x65\x72\x2e\x63\x6f\x6e\x74\x72\x6f\x6c\x6c\x65\x72\x2e\x6a\x73"]=c.extensions["\x2e\x6a\x73"],c.extensions["\x2e\x73\x65\x72\x76\x65\x72\x2e\x6d\x6f\x64\x65\x6c\x2e\x6a\x73"]=c.extensions["\x2e\x6a\x73"],c.extensions["\x2e\x73\x65\x72\x76\x65\x72\x2e\x72\x6f\x75\x74\x65\x73\x2e\x6a\x73"]=c.extensions["\x2e\x6a\x73"]}()}),c.register("\x2e\x2f\x61\x70\x70\x2f\x6a\x73\x2f\x63\x6f\x6e\x74\x72\x6f\x6c\x6c\x65\x72\x73\x2e\x6a\x73",function(){!function(){angular.module("\x6d\x79\x61\x70\x70\x2e\x63\x6f\x6e\x74\x72\x6f\x6c\x6c\x65\x72\x73",[]).controller("\x41\x70\x70\x43\x6f\x6e\x74\x72\x6f\x6c\x6c\x65\x72",["\x24\x73\x63\x6f\x70\x65",function(a){a.name="\x54\x65\x73\x74\x31\x32\x33"}])}()}),b.exports=c("\x2e\x2f\x61\x70\x70\x2f\x6a\x73\x2f\x61\x70\x70\x2e\x6a\x73")}(require,module);

UPDATE There is no answer for this question since this plugin is not created for angularjs, only for Node.js. And the creator has not the minimal intention to make it for angular js. :( However it is a cool tool

解决方案

there are several questions here.

Do I need to obfuscate all my js files in order to get this run? no

An obfuscate file can work perfect with all your dependencies NON-obfuscated? yes

What am I missing? Hard to tell, uglification can be very tricky. First I'll verify if dependencies injections are well done or you can use ngAnnotate to do that for you dependency shall be defined as example bellow:

.service('myService',['$rootScope',function($rootScope){ ... }]);

If that does not solve your issue, you have to dig on requireJS that seems to send an issue

Edit your code and paste your main.js for us see what you are trying to uglify

这篇关于使用grunt-obfuscator混淆js文件:未捕获ReferenceError:require未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆