Bookmarklet Help: Creating a Find/Replace Bookmarklet

后端 未结 3 1283
无人及你
无人及你 2021-01-07 07:11

I\'m trying to slightly modify this so that it prompts for the text to search for, followed by the text to replace with, and when all done processing, show a dialog box lett

相关标签:
3条回答
  • 2021-01-07 07:37

    This worked for me:

    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"));
    

    All I did was replace the old and new with a prompt() function. good bookmarklet.

    0 讨论(0)
  • 2021-01-07 07:42

    Here is the most direct conversion of the original function to search/replace textarea and text inputs instead of HTML. I also added 'm' to regex and added alert('done') at end. However, I think that using 'm' may not solve your problem perfectly, but I may be wrong.

    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');
    

    Here it is as a bookmarklet.

    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');
    
    0 讨论(0)
  • 2021-01-07 08:02

    A search landed me here, and the stuff above is wrong (or at least outdated), and I went through the trouble of updating it until it worked, so I figured I'd paste it here:

    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.');
    

    (Tested in Chrome) It's got a word count instead of just a 'done' message. I changed it to scan through all textNode elements. (I suppose MOST people wouldn't care about replacing all text on the page, but that's the use case that brought me here.)

    0 讨论(0)
提交回复
热议问题