How to hide js code

php中世界最好的语言
Release: 2018-04-23 10:10:19
Original
5020 people have browsed it

这次给大家带来怎么隐藏js代码,隐藏js代码的注意事项有哪些,下面就是实战案例,一起来看一下。

好了,我们来看下代码吧。

(function(window) { var rep = { // 替换用的数据,使用了4个零宽字符,数据量减少了一半。 '00': '\u200b', '01': '\u200c', '10': '\u200d', '11': '\uFEFF' }; function hide(str) { str = str.replace(/[^\x00-\xff]/g, function(a) { // 转码 Latin-1 编码以外的字符。 return escape(a).replace('%', '\\'); }); str = str.replace(/[\s\S]/g, function(a) { // 处理二进制数据并且进行数据替换 a = a.charCodeAt().toString(2); a = a.length < 8 ? Array(9 - a.length).join('0') + a : a; return a.replace(/../g, function(a) { return rep[a]; }); }); return str; } var tpl = '("@code".replace(/.{4}/g,function(a){var rep={"\u200b":"00","\u200c":"01","\u200d":"10","\uFEFF":"11"};return String.fromCharCode(parseInt(a.replace(/./g, function(a) {return rep[a]}),2))}))'; window.hider = function(code, type) { var str = hide(code); // 生成零宽字符串 str = tpl.replace('@code', str); // 生成模版 if (type === 'eval') { str = 'eval' + str; } else { str = 'Function' + str + '()'; } return str; } })(window);
Copy after login

大家可以通过最简单的代码例子进行测试一下,看看效果如何,也可以进一步优化,或者写成插件什么的,就当练习吧。。
虽然这个东西没什么实际用处,不过有时候玩玩还是不错的,可出面试题吓唬新人哦。。

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:

使用JS思维技巧

JS调试使用详解

The above is the detailed content of How to hide js code. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!