2016-05-16 4 views
-5

Ich möchte wissen, wie dieser JavaScript-Code diese Textzeichenfolgen ("aaa", "bbb" und "ccc") auf meiner Seite findet.Wie schreibt dieser JavaScript-Code?

Jeder ist im Code getrennt.

Danke

Hier ist der Code:

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'); 
    }`` 
+0

Sie möchten mehr Details über den Code –

+0

die Antwort auf Ihre Frage viel Geisteskraft erfordert hinzufügen, aber geben keinen Wert für die Gemeinschaft ... , so dass Sie in Gefahr sind zu Opfer eines negativen Voting Shit-Storm, nur weil solche Fragen hier nicht erwünscht sind – webdeb

+0

Auch wenn dieser Code genial ist, sollte man nicht so programmieren, denn diese Art der Codierung ist unmöglich debuggen. – Steffomio

Antwort

0

ok, also lassen Sie uns es brechen so gut wir können.

Die Aufgabe von ae() besteht darin, ihren Parameter (eine Funktion) dem Fenster-Onload-Handler "hinzuzufügen".

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

fg() ruft einfach qw() ein paar Mal ...

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

An diesem Punkt, was wir haben, ist die Funktion fg() wird an den Fenstern onload() hinzugefügt werden dank zur Arbeit von ae(). Schließlich wird onload, qw() dreimal ausgeführt (zusätzlich zu jeder anderen Arbeit, die bereits für den Onload definiert wurde)

Dies hinterlässt uns mit qw(), die ich aus Gründen der Klarheit erweitert und umstrukturiert habe ...

// ------------------------------- 
// "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" 
     // ------------------------------- 
    } 
} 
// -------------------------------