这个javascript代码如何变形?

时间:2016-05-16 12:56:01

标签: javascript

我想知道这个JavaScript代码是如何在我的页面上找到这些文本字符串,(" aaa"," bbb"和" ccc")。

每个都在代码中分开。

谢谢

以下是代码:

    function ae(func) {
    var oldonload = window.onload;
    if (typeof oldonload != 'function') {
      window.onload = func;
    } else {
      window.onload = function () {
        oldonload();
        func();
      }
    }
  }
  function qw(e, t, n) {
    if (e && "undefined" != typeof t)for (var r = "string" == typeof e ? new RegExp(e, "g") : e, a = (n || document.body).childNodes, d = a.length, i = "html,head,style,title,link,meta,script,object,iframe"; d--;) {
      var o = a[d];
      if (1 === o.nodeType && -1 === (i + ",").indexOf(o.nodeName.toLowerCase() + ",") && arguments.callee(e, t, o), 3 === o.nodeType && r.test(o.data)) {
        var l = o.parentNode, c = function () {
          var e = o.data.replace(r, t), n = document.createElement("div"), a = document.createDocumentFragment();
          for (n.innerHTML = e; n.firstChild;)a.appendChild(n.firstChild);
          return a
        }();
        l.insertBefore(c, o), l.removeChild(o)
      }
    }
  }
  ae(fg);
  function fg() {
    qw('aaa', 'a');
    qw('bbb', 'b');
    qw('ccc', 'c');
  }``

1 个答案:

答案 0 :(得分:0)

好的,所以让我们尽可能地分解它。

// -------------------------
// add an additional step (function) to the onload handler.
// -------------------------
function ae(func) {
    // -------------------------
    // save the current onload handler to a variable
    // -------------------------
    var oldonload = window.onload;
    // -------------------------

    // -------------------------
    // meld the current onload and the new "func" together.
    // -------------------------
    if (typeof oldonload != 'function') {
        window.onload = func;
    } else {
      window.onload = function () {
        oldonload();
        func();
    }
    // -------------------------
}
// -------------------------

ae()的工作是"添加"它的窗口onload处理程序的参数(函数)。

// -------------------------
// call qw() a few times
// -------------------------
function fg() {
    qw('a', 'a');
    qw('b', 'b');
    qw('ccc', 'c');
}
// -------------------------

fg()只需调用qw()几次......

// -------------------------
// add a function "fg" to the onload handler
// -------------------------
ae(fg);
// -------------------------

此时我们所拥有的是函数fg()将被添加到windows onload()中,这要归功于ae()的工作。最终,onload,qw()将被执行三次(除了已经为onload定义的任何其他工作...

这给我们留下了qw(),为了清晰起见,我已经扩展和重组了......

// -------------------------------
// "e" regex pattern
// "t" replacement string
// "n" root node
// -------------------------------
// under "n" defaulting to <body /> recursively find text nodes
// and apply the regex pattern "e" to search and place with "t"
// -------------------------------
function qw(e, t, n) {
    // -------------------------------
    // if the parameters are not good, abort
    // -------------------------------
    if (!e || "undefined" === typeof t) { return; }
    // -------------------------------

    // -------------------------------
    // "a" holds child nodes of <body />
    // unless we passing in a new parent as parameter "n"
    // -------------------------------
    var a = (n || document.body).childNodes;
    // -------------------------------

    // -------------------------------
    // d indicates how many nodes we found
    // -------------------------------
    var d = a.length;
    // -------------------------------

    // -------------------------------
    // "i" holds a list of tags that we will not process as we will discover later
    // -------------------------------
    var i = "html,head,style,title,link,meta,script,object,iframe";
    // -------------------------------

    // -------------------------------
    // if "e" is a string "r" shall hold a regex based on "e"
    // otherwise "r" shall hold "e"
    // -------------------------------
    var r = (("string" == typeof e) ? new RegExp(e, "g") : e);
    // -------------------------------

    // -------------------------------
    // do until d === 0
    // i.e. do once for each child node identified above...
    // -------------------------------
    for (; d--; ) {
        // -------------------------------
        // o shall be the "current" child node
        // -------------------------------
        var o = a[d];
        // -------------------------------

        // -------------------------------
        // cnd1 shall indicate if "o" is an "Element" (div, p, ...)
        // -------------------------------
        var cnd1 = (1 === o.nodeType);
        // -------------------------------

        // -------------------------------
        // cnd2 shall indicate if "o" is not in the list "i"
        // -------------------------------
        var cnd2 = (-1 === (i + ",").indexOf(o.nodeName.toLowerCase() + ","));
        // -------------------------------

        // -------------------------------
        // The comma operator is intentionally messing with us!
        // if "o" is an Element but not a "bad" one recurse
        // -------------------------------
        if (cnd1 && cnd2) { arguments.callee(e, t, o); }
        // -------------------------------

        // -------------------------------
        // "cnd3" shall indicate if "o" is actual text
        // -------------------------------
        var cnd3 = (3 === o.nodeType);
        // -------------------------------

        // -------------------------------
        // "cnd4" shall indicate if "o" matches our regex "r"
        // -------------------------------
        var cnd4 = (r.test(o.data));
        // -------------------------------

        // -------------------------------
        // if "o" is not the text part of a node or if it is but not a hit then move on
        // -------------------------------
        if (!cnd3 || !cnd4) { continue; )
        // -------------------------------

        // -------------------------------
        // Get the parent of this text (the prior "o")
        // -------------------------------
        var l = o.parentNode;
        // -------------------------------

        // -------------------------------
        // "c" will be a collection of text nodes (single) the result of a search and replace based on the initial parameters
        // -------------------------------
        var c = function () {
            // -------------------------------
            // NOTE: after this point local "e" scopes parameter "e"
            // in this context "e" is now the results of our search and replace
            // -------------------------------
            var e = o.data.replace(r, t);
            // -------------------------------

            // -------------------------------
            // create a template div to hold our new text "e"
            // in turn this will allow us to essentially convert "e" to a node
            // -------------------------------
            var n = document.createElement("div");
            n.innerHTML = e;
            // -------------------------------

            // -------------------------------
            // "a" shall be a container for new child nodes
            // -------------------------------
            var a = document.createDocumentFragment();
            // -------------------------------

            // -------------------------------
            // note sure if there are ever any more than 1 but this
            // moves all the children of "n" to be children of "a"
            // -------------------------------
            for (; n.firstChild; ) { a.appendChild( n.firstChild ); }
            // -------------------------------

            // -------------------------------
            // return our new node container with the results of the search and replace
            // -------------------------------
            return a
            // -------------------------------
        }();
        // -------------------------------

        // -------------------------------
        // add the results node "c" to the parent just prior to the current "o"
        // -------------------------------
        l.insertBefore(c, o);
        // -------------------------------

        // -------------------------------
        // now remove from the parent the current "o"
        // -------------------------------
        l.removeChild(o)
        // -------------------------------

        // -------------------------------
        // effectively we have replaced "o" with "c"
        // -------------------------------
    }
}
// -------------------------------