javascript 自动标记来自搜索结果页的关键字
2010-01-25 00:00
267 查看
/*定义样式*/
.hilite1{background:#69c;}
Hilite = {
exact: true,
max_nodes: 1000,
style_name: 'hilite',
style_name_suffix: true
};
Hilite.hiliteElement = function (elm, query) {
if (!query || elm.childNodes.length == 0)
return;
query = query.split(/[\s,\+\.]+/);
var qre = new Array();
for (var i = 0; i < query.length; i++) {
query[i] = query[i].toLowerCase();
if (Hilite.exact)
qre.push('\\b' + query[i] + '\\b');
else
qre.push(query[i]);
}
qre = new RegExp(qre.join("|"), "i");
var stylemapper = {};
for (var i = 0; i < query.length; i++) {
if (Hilite.style_name_suffix)
stylemapper[query[i]] = Hilite.style_name + (i + 1);
else
stylemapper[query[i]] = Hilite.style_name;
}
var textproc = function (node) {
var match = qre.exec(node.data);
if (match) {
var val = match[0];
var k = '';
var node2 = node.splitText(match.index);
var node3 = node2.splitText(val.length);
var span = node.ownerDocument.createElement('SPAN');
node.parentNode.replaceChild(span, node2);
span.className = stylemapper[val.toLowerCase()];
span.appendChild(node2);
return span;
} else {
return node;
}
};
Hilite.walkElements(elm.childNodes[0], 1, textproc);
};
Hilite.walkElements = function (node, depth, textproc) {
var skipre = /^(script|style|textarea)/i;
var count = 0;
while (node && depth > 0) {
count++;
if (count >= Hilite.max_nodes) {
var handler = function () {
Hilite.walkElements(node, depth, textproc);
};
setTimeout(handler, 50);
return;
}
if (node.nodeType == 1) {
if (!skipre.test(node.tagName) && node.childNodes.length > 0) {
node = node.childNodes[0];
depth++;
continue;
}
} else if (node.nodeType == 3) {
node = textproc(node);
}
if (node.nextSibling) {
node = node.nextSibling;
} else {
while (depth > 0) {
node = node.parentNode;
depth--;
if (node.nextSibling) {
node = node.nextSibling;
break;
}
}
}
}
};
Hilite.autoForSeachEngine = function () {
var re = new RegExp('[\\?&](q|keyword|qk|wd|p)=([^&]+)','ig');
var ref = document.referrer || 'http://search.yahoo.com/search?p=javascript&toggle=1&cop=mss&ei=UTF-8&fr=yfp-t-701';
addLoadEvent(function () {
var match = re.exec(ref);
if (match) {
var query = decodeURIComponent(match[2]);
Hilite.hiliteElement(document.getElementById('divMain'), query);
}
});
};
function addLoadEvent(f) {
var old = window.onload;
if (typeof old != 'function') window.onload = f;
else { window.onload = function () { old(); f(); }; }
}
Hilite.autoForSeachEngine();
我是test,我要被highlight。我们是javascript,我们是javascript
[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]
.hilite1{background:#69c;}
Hilite = {
exact: true,
max_nodes: 1000,
style_name: 'hilite',
style_name_suffix: true
};
Hilite.hiliteElement = function (elm, query) {
if (!query || elm.childNodes.length == 0)
return;
query = query.split(/[\s,\+\.]+/);
var qre = new Array();
for (var i = 0; i < query.length; i++) {
query[i] = query[i].toLowerCase();
if (Hilite.exact)
qre.push('\\b' + query[i] + '\\b');
else
qre.push(query[i]);
}
qre = new RegExp(qre.join("|"), "i");
var stylemapper = {};
for (var i = 0; i < query.length; i++) {
if (Hilite.style_name_suffix)
stylemapper[query[i]] = Hilite.style_name + (i + 1);
else
stylemapper[query[i]] = Hilite.style_name;
}
var textproc = function (node) {
var match = qre.exec(node.data);
if (match) {
var val = match[0];
var k = '';
var node2 = node.splitText(match.index);
var node3 = node2.splitText(val.length);
var span = node.ownerDocument.createElement('SPAN');
node.parentNode.replaceChild(span, node2);
span.className = stylemapper[val.toLowerCase()];
span.appendChild(node2);
return span;
} else {
return node;
}
};
Hilite.walkElements(elm.childNodes[0], 1, textproc);
};
Hilite.walkElements = function (node, depth, textproc) {
var skipre = /^(script|style|textarea)/i;
var count = 0;
while (node && depth > 0) {
count++;
if (count >= Hilite.max_nodes) {
var handler = function () {
Hilite.walkElements(node, depth, textproc);
};
setTimeout(handler, 50);
return;
}
if (node.nodeType == 1) {
if (!skipre.test(node.tagName) && node.childNodes.length > 0) {
node = node.childNodes[0];
depth++;
continue;
}
} else if (node.nodeType == 3) {
node = textproc(node);
}
if (node.nextSibling) {
node = node.nextSibling;
} else {
while (depth > 0) {
node = node.parentNode;
depth--;
if (node.nextSibling) {
node = node.nextSibling;
break;
}
}
}
}
};
Hilite.autoForSeachEngine = function () {
var re = new RegExp('[\\?&](q|keyword|qk|wd|p)=([^&]+)','ig');
var ref = document.referrer || 'http://search.yahoo.com/search?p=javascript&toggle=1&cop=mss&ei=UTF-8&fr=yfp-t-701';
addLoadEvent(function () {
var match = re.exec(ref);
if (match) {
var query = decodeURIComponent(match[2]);
Hilite.hiliteElement(document.getElementById('divMain'), query);
}
});
};
function addLoadEvent(f) {
var old = window.onload;
if (typeof old != 'function') window.onload = f;
else { window.onload = function () { old(); f(); }; }
}
Hilite.autoForSeachEngine();
我是test,我要被highlight。我们是javascript,我们是javascript
[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]
相关文章推荐
- javascript 自动标记来自搜索结果页的关键字
- 使用javascript自动标记来自搜索结果页的关键字
- google API v3 (javascript) 之关键字自动完成提示
- 让VS2010添加新类时自动添加public关键字(来自dudu博文)
- javaScript输入框关键字添加,并且加入了jquery自动补全功能
- javascript中new关键字详解
- javascript tostring自动调用
- javascript关键字加亮加连接
- 分步解析JavaScript实现tab选项卡自动切换功能
- JavaScript实现iframe自动高度调整和不同主域名跨域
- Javascript + Asp.net 让图片自动成比例缩放
- JavaScript Time时间自动刷新
- Javascript基础教程之关键字和保留字汇总
- javaScript input失去焦点自动保存事件
- JavaScript 关键字
- 自动最大化窗口的Javascript代码
- 最新javascript自动按比例显示图片,按比例压缩图片显示
- 【javaweb:前端】修改a标签的href值为javascript代码使页面不自动跳转到当前页,改成不操作
- Javascript输入汉字自动转换为首字母的拼音码
- PHP类CMS如何自动获取关键字