UNPKG

947 BJavaScriptView Raw
1/**
2 * 对字符串进行 Unicode 编码
3 *
4 * @param {string} str 源字符串
5 * @return {string} 返回编码后的内容
6 */
7function encodeUnicode(str) {
8 return String(str).replace(/[^\x09-\x7f\ufeff]/g, function (all) {
9 return '\\u' + (0x10000 + all.charCodeAt()).toString(16).substring(1);
10 });
11}
12
13/**
14 * 零宽字符编码处理器
15 *
16 * @see http://ucren.com/blog/archives/549
17 */
18module.exports = function (content) {
19 if (!content) {
20 return content;
21 }
22 var t = parseInt('10000000', 2);
23 content = encodeUnicode(content).replace(/[^]/g, function (all) {
24 return (t + all.charCodeAt()).toString(2).substring(1).replace(/[^]/g, function (n) {
25 return {
26 0: '\u200c',
27 1: '\u200d'
28 }[n];
29 });
30 });
31 return '(function(){}).constructor("' + content + '".replace(/./g,function(a){return{"\u200c":0,"\u200d":1}[a]}).replace(/.{7}/g,function(a){return String.fromCharCode(parseInt(a,2))}))();';
32};
\No newline at end of file