Bookmarklet帮助:创建查找/替换书签

时间:2011-01-22 19:11:39

标签: javascript replace bookmarklet

我正在尝试稍微修改this,以便它提示要搜索的文本,然后是要替换的文本,并在完成所有处理后,显示一个对话框让我知道它已完成。

我计划在phpmyadmin数据库编辑页面上使用它,该页面将包含任意数量的文本框(,这是我需要它来搜索和替换)。此外,搜索和替换的文本可能是多行,也可能不是多行,所以我在正则表达式中添加了'm'参数,而且,因为我将进行可能包含html的搜索/替换,所以它们经常会有引号/双引号。例如:

搜索:

<img height="76" width="92" src="http://www.gifs.net/Animation11/Hobbies_and_Entertainment/Games_and_Gambling/Slot_machine.gif" /></div>
<div class="rtecenter"> <strong><em><font color="#ff0000">Vegas Baby!<br />
</font></em></strong></div>

并且可能没有替换(只是为了删除所有代码)或其他一些html。到目前为止,这是我提出的书签,( javascript,特别是bookmarklet不是我经常搞乱的东西)然而,它找不到任何东西,只要找到/替换,尽管它确实正确地进行提示。

javascript:var%20scrEl=document.createElement('script');scrEl.setAttribute('language','javascript');scrEl.setAttribute('type','text/javascript');scrEl.setAttribute('src','http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js');function%20htmlreplace(a,b,element){if(!element)element=document.body;var%20nodes=$(element).contents().each(function(){if(this.nodeType==Node.TEXT_NODE){var%20r=new%20RegExp(a,'gim');this.textContent=this.textContent.replace(r,b);}else{htmlreplace(a,b,this);alert('Done%20processing.');}});}htmlreplace(prompt('Text%20to%20find:',''),prompt('Replace%20with:',''));

有人有什么想法吗?

3 个答案:

答案 0 :(得分:2)

这对我有用:

javascript:function%20htmlreplace(a,b,element){if(!element)element=document.body;var%20nodes=element.childNodes;for(var%20n=0;n<nodes.length;n++){if(nodes[n].nodeType==Node.TEXT_NODE){nodes[n].textContent=nodes[n].textContent.replace(new%20RegExp(a,'gi'),b);}else{htmlreplace(a,b,nodes[n]);}}}htmlreplace(prompt("Text%20to%20replace:","old"),prompt("Replacement%20text:","new"));

我所做的就是用old函数替换newprompt()。好的书签。

答案 1 :(得分:2)

这是原始函数最直接的转换,用于搜索/替换textarea和文本输入而不是HTML。我还在正则表达式中添加了“m”并在结尾添加了警告(“完成”)。但是,我认为使用'm'可能无法完美解决您的问题,但我可能错了。

function htmlreplace(a, b, element) {
    if (!element) element = document.body;    
    var nodes = element.childNodes;
    for (var n=0; n<nodes.length; n++) {
        if ( nodes[n].type && (nodes[n].type.toLowerCase() == 'textarea' || nodes[n].type.toLowerCase() == 'text') ) {
            var r = new RegExp(a, 'gim');
            nodes[n].value = nodes[n].value.replace(r, b);
        } else {
            htmlreplace(a, b, nodes[n]);
        }
    }
}

htmlreplace(prompt('find'), prompt('replace'));
alert('done');

这里是一个书签。

javascript:function htmlreplace(a,b,element){if(!element)element=document.body;var nodes=element.childNodes;for(var n=0;n<nodes.length;n++){if(nodes[n].type&&(nodes[n].type.toLowerCase()=='textarea'||nodes[n].type.toLowerCase()=='text')){var r=new RegExp(a,'gim');nodes[n].value=nodes[n].value.replace(r,b)}else{htmlreplace(a,b,nodes[n])}}}htmlreplace(prompt('find'),prompt('replace'));alert('done');

答案 2 :(得分:0)

搜索让我来到这里,上面的内容是错误的(或至少过时了),我经历了更新它的麻烦,直到它工作,所以我想我会把它粘贴在这里:

javascript:var count=0;
function htmlreplace(a,b,element){
if(!element)element=document.body;
var nodes=element.childNodes;
for(var n=0;n<nodes.length;n++){
   if(nodes[n].type&&nodes[n].type.toLowerCase()=='textarea'){
      var r=new RegExp(a,'gim');
      if(nodes[n].value.match(r)){
        count++;
      }
      nodes[n].value=nodes[n].value.replace(r,b)
    }
    else if(nodes[n].nodeValue && nodes[n].nodeValue.length > 0){
      var r=new RegExp(a,'gim');
      if(nodes[n].nodeValue.match(r)){
        count++;
      }
      nodes[n].nodeValue=nodes[n].nodeValue.replace(r,b)
    }
    else{
      htmlreplace(a,b,nodes[n])
    }
  }
}
htmlreplace(prompt('find'),prompt('replace'));
alert('replaced '+count+' words.');

(在Chrome中测试) 它有一个字数而不仅仅是一个“完成”的消息。我将其更改为扫描所有textNode元素。 (我想大多数人都不会关心替换页面上的所有文本,但这是将我带到这里的用例。)