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

发布于 2024-10-13 09:28:29 字数 1420 浏览 6 评论 0原文

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

我计划在 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,尤其是小书签不是我经常弄乱的东西)但是,它在查找/替换方面没有任何作用,尽管它确实正确执行提示。

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

有人有什么想法吗?

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 letting me know it's done.

I plan to use it on a phpmyadmin database edit page that'll have any number of textboxes filled with text (which is what I need it to search and replace in). Also, the text to search for and replace may or may not be multi-line, so I've added the 'm' param in the regex, and also, since I'll be doing searches/replaces that may contain html, they'll often have quotes/double quotes in them. ex:

Search for:

<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>

and maybe replace with nothing (just to erase all that code), or some other html. So far this is the bookmarklet I've come up with, (javascript, and especially bookmarklets aren't something I mess with often) however, it does nothing as far as finding/replacing, although it does do the prompting correctly.

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

Anyone have any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无力看清 2024-10-20 09:28:29

这是原始函数最直接的转换,用于搜索/替换文本区域和文本输入而不是 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');

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');
屌丝范 2024-10-20 09:28:29

这对我有用:

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

我所做的就是用 prompt() 函数替换 oldnew 。很好的书签。

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.

万人眼中万个我 2024-10-20 09:28:29

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

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 元素。
(我想大多数人不会关心替换页面上的所有文本,但这就是让我来到这里的用例。)

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文