admin 管理员组文章数量: 1086019
I have some text on an HTML page. Need a bookmarklet (no jQuery) that will find a segment of the text using regex, then replace it with a link with the text as a parameter
Example before:
aaa bbb ccc ddd
Example after:
aaa <a href="http:www.whatever?bbb">bbb</a> ccc ddd
assuming we were looking for "bbb"
I have some text on an HTML page. Need a bookmarklet (no jQuery) that will find a segment of the text using regex, then replace it with a link with the text as a parameter
Example before:
aaa bbb ccc ddd
Example after:
aaa <a href="http:www.whatever.?bbb">bbb</a> ccc ddd
assuming we were looking for "bbb"
Share Improve this question edited Jan 21, 2012 at 0:05 zzzzBov 179k56 gold badges327 silver badges371 bronze badges asked Jan 21, 2012 at 0:00 Ron HarlevRon Harlev 16.7k26 gold badges92 silver badges139 bronze badges 02 Answers
Reset to default 9This solution will crawl the DOM search for text nodes within the document elements, skipping any elements you want. For example, you probably want to skip <a> tags, as well as <script> tags and others. This way, you won't replace element nodes or essential page functionality.
(function(){
// don't replace text within these tags
var skipTags = { 'a': 1, 'style': 1, 'script': 1, 'iframe': 1 };
// find text nodes to apply replFn to
var findKW = function ( el, term, replFn ) {
var child, tag;
for (var i = el.childNodes.length - 1; i >= 0; i--) {
child = el.childNodes[i];
if (child.nodeType == 1) { // ELEMENT_NODE
tag = child.nodeName.toLowerCase();
if (!(tag in skipTags)) {
findKW(child, term, replFn);
}
} else if (child.nodeType == 3) { // TEXT_NODE
replaceKW(child, term, replFn);
}
}
};
// replace terms in text according to replFn
var replaceKW = function ( text, term, replFn ) {
var match,
matches = [];
while (match = term.exec(text.data)) {
matches.push(match);
}
for (var i = matches.length - 1; i >= 0; i--) {
match = matches[i];
// cut out the text node to replace
text.splitText(match.index);
text.nextSibling.splitText(match[1].length);
text.parentNode.replaceChild(replFn(match[1]), text.nextSibling);
}
};
var replTerm = prompt('Please enter term to replace');
findKW(
document.body,
// using \\b to only replace when the term is the whole word
// e.g. if term is "bbb" then "aabbbccc" will not match
new RegExp('\\b(' + replTerm + ')\\b', 'g'),
// your replacement function, change URL accordingly
function (match) {
var link = document.createElement('a');
link.href = 'http://google./#q=' + match;
link.target = '_blank';
link.innerHTML = match;
return link;
}
);
}());
Here it is minimized in bookmarklet form:
javascript:(function(){var a={a:1,style:1,script:1,iframe:1};var b=function(d,e,f){var g,h;for(var i=d.childNodes.length-1;i>=0;i--){g=d.childNodes[i];if(g.nodeType==1){h=g.nodeName.toLowerCase();if(!(h in a)){b(g,e,f)}}else if(g.nodeType==3){c(g,e,f)}}};var c=function(a,b,c){var d,e=[];while(d=b.exec(a.data)){e.push(d)}for(var f=e.length-1;f>=0;f--){d=e[f];a.splitText(d.index);a.nextSibling.splitText(d[1].length);a.parentNode.replaceChild(c(d[1]),a.nextSibling)}};var d=prompt("Please enter term to replace");b(document.body,new RegExp("\\b("+d+")\\b","g"),function(a){var b=document.createElement("a");b.href="http://google./#q="+a;b.target="_blank";b.innerHTML=a;return b})})()
Copy that into a bookmark and try it out on any page! Note: search is case sensitive, but you can add the 'i' flag to the RegExp to prevent that.
Simplest regex:
document.body.innerHTML = document.body.innerHTML.replace(/bbb/ , '<a href="http://Google.">bbb</a>');
Better:
document.body.innerHTML = document.body.innerHTML.replace(/(bbb)/ , '<a href="http://Google.">$1</a>');
Best:
var srch = "bbb";
var rg = new RegExp("("+srch+")");
document.body.innerHTML = document.body.innerHTML.replace(rg , '<a href="http://Google.">$1</a>');
The parentheses in regexp signify a matched group. The "$1" in the second argument is the first matched group.
本文标签: Javascript Bookmarklet to replace text with a linkStack Overflow
版权声明:本文标题:Javascript Bookmarklet to replace text with a link - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744090889a2532011.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论