这个javascript代码是如何编写的
How this javascript code is wroking?
我想知道这个JavaScript代码是如何在我的页面上找到这些文本字符串("aaa"、"bbb"answers"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');
}``
好的,让我们尽可能地分解它。
// -------------------------
// 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()的工作是将其参数(一个函数)"添加"到窗口加载处理程序中。
// -------------------------
// 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"
// -------------------------------
}
}
// -------------------------------
相关文章:
- 如何在单击“提交”按钮时为“新建”窗口编写JavaScript,用“确定”和“取消”显示注册信息
- 我应该用K&编写javascript时的R样式
- 编写JavaScript库:如何授权方法
- Javascript渲染.如何编写Javascript;t在函数调用结束之前继续执行代码
- 需要帮助编写 Javascript 来添加值
- 在 HTML 页面中编写 JavaScript
- 获取用于编写JavaScript cookie的复选框
- 在IE8中编写JavaScript文档
- Java开发人员在不编写Javascript的情况下生成Javascript的最佳方式是什么
- 为什么使用与输入参数具有相同结束参数的函数来编写Javascript
- 动态编写javascript部分
- 在JavaScript中编写JavaScript代码,全部使用PHP
- 使用Kotlin编写javascript应用程序
- 有没有办法用pdf.js为pdf渲染编写javascript事件
- 如何在php中编写javascript代码
- 响应时出错.编写 javascript 警报
- 从用户输入到文档编写 javascript 变量
- 如何在 JSP 页面中编写 javascript 倒数计时器,以便重新加载不会重新启动计时器
- 第一次正确编写Javascript
- 尝试在 PHP 中编写 JavaScript 时出错