将ES6 JavaScript功能代码转换为非ES6代码

时间:2019-06-20 04:41:33

标签: javascript ecmascript-6 firefox-webextensions userscripts

试图在自己的脚本中使用复杂的解密逻辑,但是代码是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 6等效代码是什么?

2 个答案:

答案 0 :(得分:3)

一种选择是使用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.');
  }
};

看起来很丑,但这没关系-它会起作用,并且可以自动完成。

您还使用了 one 个ES6 +方法String.prototype.startsWith,它不是语法,因此不会转换-为此添加一个polyfill在其他代码之前运行。

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等过时的浏览器上运行。

答案 1 :(得分:2)

ECMAScript 6 - ECMAScript 2015ECMAScript 2015 support in Mozilla中列出了ES6的新功能,通过删除它们,代码将成为ES6之前的版本。例如:

  • constlet更改为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.'
        )
    }
}