将 ES6 javascript 函数代码转换为非 es6 代码
Converting ES6 javascript function code to non-es6 code
尝试在自己的脚本中使用复杂的解密逻辑,但代码是我不知道的 ES6。
const parseDecsig = data => {
try {
if (data.startsWith('var script')) {
// they inject the script via script tag
const obj = {}
const document = { createElement: () => obj, head: { appendChild: () => {} } }
eval(data)
data = obj.innerHTML
}
const fnnameresult = /yt\.akamaized\.net.*encodeURIComponent\((\w+)/.exec(data)
const fnname = fnnameresult[1]
const _argnamefnbodyresult = new RegExp(fnname + '=function\((.+?)\){(.+?)}').exec(data)
const [_, argname, fnbody] = _argnamefnbodyresult
const helpernameresult = /;(.+?)\..+?\(/.exec(fnbody)
const helpername = helpernameresult[1]
const helperresult = new RegExp('var ' + helpername + '={[\s\S]+?};').exec(data)
const helper = helperresult[0]
logger.log(`parsedecsig result: %s=>{%s\n%s}`, argname, helper, fnbody)
return new Function([argname], helper + '\n' + fnbody)
} catch (e) {
logger.error('parsedecsig error: %o', e)
logger.info('script content: %s', data)
logger.info(
'If you encounter this error, please copy the full "script content" to https://pastebin.com/ for me.'
)
}
}
上面的 Pre ES6 等效代码是什么?
一种选择是使用 Babel。插入你的代码,你会得到:
"use strict";
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }
function _iterableToArrayLimit(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
var parseDecsig = function parseDecsig(data) {
try {
if (data.startsWith('var script')) {
// they inject the script via script tag
var obj = {};
var document = {
createElement: function createElement() {
return obj;
},
head: {
appendChild: function appendChild() {}
}
};
eval(data);
data = obj.innerHTML;
}
var fnnameresult = /yt\.akamaized\.net.*encodeURIComponent\((\w+)/.exec(data);
var fnname = fnnameresult[1];
var _argnamefnbodyresult = new RegExp(fnname + '=function\((.+?)\){(.+?)}').exec(data);
var _argnamefnbodyresult2 = _slicedToArray(_argnamefnbodyresult, 3),
_ = _argnamefnbodyresult2[0],
argname = _argnamefnbodyresult2[1],
fnbody = _argnamefnbodyresult2[2];
var helpernameresult = /;(.+?)\..+?\(/.exec(fnbody);
var helpername = helpernameresult[1];
var helperresult = new RegExp('var ' + helpername + '={[\s\S]+?};').exec(data);
var helper = helperresult[0];
logger.log("parsedecsig result: %s=>{%s\n%s}", argname, helper, fnbody);
return new Function([argname], helper + '\n' + fnbody);
} catch (e) {
logger.error('parsedecsig error: %o', e);
logger.info('script content: %s', data);
logger.info('If you encounter this error, please copy the full "script content" to https://pastebin.com/ for me.');
}
};
看起来很丑,但这不重要 - 它会工作,并且可以自动完成。
您还有 一个 您正在使用的 ES6+ 方法,String.prototype.startsWith
,它不是语法,所以它不会转换 - 添加一个 polyfill 因为它比其他代码早 运行s。
if (!String.prototype.startsWith) {
Object.defineProperty(String.prototype, 'startsWith', {
value: function(search, pos) {
pos = !pos || pos < 0 ? 0 : +pos;
return this.substring(pos, pos + search.length) === search;
}
});
}
然后,代码应该完全符合 ES5,并且应该 运行 即使在像 IE11 这样的过时浏览器上也是如此。
ES6 的新特性在 ECMAScript 6 - ECMAScript 2015 or ECMAScript 2015 support in Mozilla 中列出,通过删除它们,代码将成为 ES6 之前的版本。例如:
- 将
const
或let
更改为var
- 将 Arrow function 更改为正常功能(有范围更改,但在大多数情况下不会导致任何问题)
- 将
startsWith
更改为 indexOf
- 改变Destructuring assignment
这里是代码的快速返工,向您展示了该过程。我没有仔细检查。
var parseDecsig = funnction(data) {
try {
if (data.indexOf('var script') !== -1) {
// they inject the script via script tag
var obj = {}
var document = { createElement: function(){ obj }, head: { appendChild: function(){} } }
eval(data)
data = obj.innerHTML
}
var fnnameresult = /yt\.akamaized\.net.*encodeURIComponent\((\w+)/.exec(data)
var fnname = fnnameresult[1]
var _argnamefnbodyresult = new RegExp(fnname + '=function\((.+?)\){(.+?)}').exec(data)
var _ = _argnamefnbodyresult[0]
var argname = _argnamefnbodyresult[1]
var fnbody = _argnamefnbodyresult[2]
var helpernameresult = /;(.+?)\..+?\(/.exec(fnbody)
var helpername = helpernameresult[1]
var helperresult = new RegExp('var ' + helpername + '={[\s\S]+?};').exec(data)
var helper = helperresult[0]
logger.log(`parsedecsig result: %s=>{%s\n%s}`, argname, helper, fnbody)
return new Function([argname], helper + '\n' + fnbody)
} catch (e) {
logger.error('parsedecsig error: %o', e)
logger.info('script content: %s', data)
logger.info(
'If you encounter this error, please copy the full "script content" to https://pastebin.com/ for me.'
)
}
}
尝试在自己的脚本中使用复杂的解密逻辑,但代码是我不知道的 ES6。
const parseDecsig = data => {
try {
if (data.startsWith('var script')) {
// they inject the script via script tag
const obj = {}
const document = { createElement: () => obj, head: { appendChild: () => {} } }
eval(data)
data = obj.innerHTML
}
const fnnameresult = /yt\.akamaized\.net.*encodeURIComponent\((\w+)/.exec(data)
const fnname = fnnameresult[1]
const _argnamefnbodyresult = new RegExp(fnname + '=function\((.+?)\){(.+?)}').exec(data)
const [_, argname, fnbody] = _argnamefnbodyresult
const helpernameresult = /;(.+?)\..+?\(/.exec(fnbody)
const helpername = helpernameresult[1]
const helperresult = new RegExp('var ' + helpername + '={[\s\S]+?};').exec(data)
const helper = helperresult[0]
logger.log(`parsedecsig result: %s=>{%s\n%s}`, argname, helper, fnbody)
return new Function([argname], helper + '\n' + fnbody)
} catch (e) {
logger.error('parsedecsig error: %o', e)
logger.info('script content: %s', data)
logger.info(
'If you encounter this error, please copy the full "script content" to https://pastebin.com/ for me.'
)
}
}
上面的 Pre ES6 等效代码是什么?
一种选择是使用 Babel。插入你的代码,你会得到:
"use strict";
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }
function _iterableToArrayLimit(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
var parseDecsig = function parseDecsig(data) {
try {
if (data.startsWith('var script')) {
// they inject the script via script tag
var obj = {};
var document = {
createElement: function createElement() {
return obj;
},
head: {
appendChild: function appendChild() {}
}
};
eval(data);
data = obj.innerHTML;
}
var fnnameresult = /yt\.akamaized\.net.*encodeURIComponent\((\w+)/.exec(data);
var fnname = fnnameresult[1];
var _argnamefnbodyresult = new RegExp(fnname + '=function\((.+?)\){(.+?)}').exec(data);
var _argnamefnbodyresult2 = _slicedToArray(_argnamefnbodyresult, 3),
_ = _argnamefnbodyresult2[0],
argname = _argnamefnbodyresult2[1],
fnbody = _argnamefnbodyresult2[2];
var helpernameresult = /;(.+?)\..+?\(/.exec(fnbody);
var helpername = helpernameresult[1];
var helperresult = new RegExp('var ' + helpername + '={[\s\S]+?};').exec(data);
var helper = helperresult[0];
logger.log("parsedecsig result: %s=>{%s\n%s}", argname, helper, fnbody);
return new Function([argname], helper + '\n' + fnbody);
} catch (e) {
logger.error('parsedecsig error: %o', e);
logger.info('script content: %s', data);
logger.info('If you encounter this error, please copy the full "script content" to https://pastebin.com/ for me.');
}
};
看起来很丑,但这不重要 - 它会工作,并且可以自动完成。
您还有 一个 您正在使用的 ES6+ 方法,String.prototype.startsWith
,它不是语法,所以它不会转换 - 添加一个 polyfill 因为它比其他代码早 运行s。
if (!String.prototype.startsWith) {
Object.defineProperty(String.prototype, 'startsWith', {
value: function(search, pos) {
pos = !pos || pos < 0 ? 0 : +pos;
return this.substring(pos, pos + search.length) === search;
}
});
}
然后,代码应该完全符合 ES5,并且应该 运行 即使在像 IE11 这样的过时浏览器上也是如此。
ES6 的新特性在 ECMAScript 6 - ECMAScript 2015 or ECMAScript 2015 support in Mozilla 中列出,通过删除它们,代码将成为 ES6 之前的版本。例如:
- 将
const
或let
更改为var
- 将 Arrow function 更改为正常功能(有范围更改,但在大多数情况下不会导致任何问题)
- 将
startsWith
更改为indexOf
- 改变Destructuring assignment
这里是代码的快速返工,向您展示了该过程。我没有仔细检查。
var parseDecsig = funnction(data) {
try {
if (data.indexOf('var script') !== -1) {
// they inject the script via script tag
var obj = {}
var document = { createElement: function(){ obj }, head: { appendChild: function(){} } }
eval(data)
data = obj.innerHTML
}
var fnnameresult = /yt\.akamaized\.net.*encodeURIComponent\((\w+)/.exec(data)
var fnname = fnnameresult[1]
var _argnamefnbodyresult = new RegExp(fnname + '=function\((.+?)\){(.+?)}').exec(data)
var _ = _argnamefnbodyresult[0]
var argname = _argnamefnbodyresult[1]
var fnbody = _argnamefnbodyresult[2]
var helpernameresult = /;(.+?)\..+?\(/.exec(fnbody)
var helpername = helpernameresult[1]
var helperresult = new RegExp('var ' + helpername + '={[\s\S]+?};').exec(data)
var helper = helperresult[0]
logger.log(`parsedecsig result: %s=>{%s\n%s}`, argname, helper, fnbody)
return new Function([argname], helper + '\n' + fnbody)
} catch (e) {
logger.error('parsedecsig error: %o', e)
logger.info('script content: %s', data)
logger.info(
'If you encounter this error, please copy the full "script content" to https://pastebin.com/ for me.'
)
}
}