Created
July 7, 2015 08:27
-
-
Save atian25/63feefc9613a67f412c6 to your computer and use it in GitHub Desktop.
js 奇葩技巧之隐藏代码
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//http://www.cnblogs.com/52cik/p/js-hide-code.html | |
(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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment