Javascript Bookmarklet用链接替换文本
我在HTML页面上有一些文本。 需要一个bookmarklet(无jQuery),它将使用regex查找文本段,然后将其替换为以文本作为参数的链接 之前的示例:Javascript Bookmarklet用链接替换文本,javascript,bookmarklet,Javascript,Bookmarklet,我在HTML页面上有一些文本。 需要一个bookmarklet(无jQuery),它将使用regex查找文本段,然后将其替换为以文本作为参数的链接 之前的示例: aaa bbb ccc ddd 示例如下: aaa <a href="http:www.whatever.com?bbb">bbb</a> ccc ddd aaa-ccc-ddd 假设我们正在寻找“bbb”最简单的正则表达式: document.body.innerHTML = document.body.
aaa bbb ccc ddd
示例如下:
aaa <a href="http:www.whatever.com?bbb">bbb</a> ccc ddd
aaa-ccc-ddd
假设我们正在寻找“bbb”最简单的正则表达式:
document.body.innerHTML = document.body.innerHTML.replace(/bbb/ , '<a href="http://Google.com">bbb</a>');
document.body.innerHTML=document.body.innerHTML.replace(/bbb/,“”);
更好:
document.body.innerHTML = document.body.innerHTML.replace(/(bbb)/ , '<a href="http://Google.com">$1</a>');
document.body.innerHTML=document.body.innerHTML.replace(/(bbb)/,“”);
最佳:
var srch=“bbb”;
var rg=新的RegExp(“(“+srch+”);
document.body.innerHTML=document.body.innerHTML.replace(rg');
regexp中的括号表示匹配的组。第二个参数中的“$1”是第一个匹配的组。此解决方案将在DOM搜索中爬网文档元素中的文本节点,跳过所需的任何元素。例如,您可能希望跳过标记以及标记和其他标记。这样,您就不会替换元素节点或基本页面功能
(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.com/#q=' + match;
link.target = '_blank';
link.innerHTML = match;
return link;
}
);
}());
在这里,它以bookmarklet形式最小化:
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.com/#q="+a;b.target="_blank";b.innerHTML=a;return b})})()
将其复制到书签中,并在任何页面上试用!注意:搜索区分大小写,但您可以将“i”标志添加到RegExp以防止出现这种情况。如果您的搜索词是“div”或“img”或“href”或“script”,则此实现可能会遇到问题……它似乎无法与parent.document一起使用。我在iframe中尝试了这段代码,结果是将HTML链接编写为文本,而不是可单击的链接。请问您是否有在Iframe中使用此脚本来修改父文档的想法?
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.com/#q="+a;b.target="_blank";b.innerHTML=a;return b})})()