查找“old”的所有实例 在网页中,使用 javascript 书签将每个内容替换为“new”

发布于 2024-07-27 17:17:29 字数 128 浏览 10 评论 0原文

我想要做的是将网页中的所有“旧”实例替换为 JS 书签或 Greasemonkey 脚本中的“新”。 我怎样才能做到这一点? 我认为 jQuery 或其他框架都可以,因为有一些技巧可以将它们包含在书签和 Greasemonkey 脚本中。

What I want to do is replace all instances of 'old' in a webpage with 'new' in a JS bookmarklet or a greasemonkey script. How can I do this? I suppose jQuery or other frameworks are okay, as there're hacks to include them in both bookmarklets as well as greasemonkey scripts.

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

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

发布评论

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

评论(8

随风而去 2024-08-03 17:17:29

一个防破坏的函数。 这意味着这不会触及任何标签或属性,只会触及文本。

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].nodeType == Node.TEXT_NODE) {
            var r = new RegExp(a, 'gi');
            nodes[n].textContent = nodes[n].textContent.replace(r, b);
        } else {
            htmlreplace(a, b, nodes[n]);
        }
    }
}

htmlreplace('a', 'r');

书签版本:

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].nodeType==Node.TEXT_NODE){nodes[n].textContent=nodes[n].textContent.replace(new RegExp(a,'gi'),b);}else{htmlreplace(a,b,nodes[n]);}}}htmlreplace('old','new');

A function that is clobber-proof. That mean's this won't touch any tags or attributes, only text.

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].nodeType == Node.TEXT_NODE) {
            var r = new RegExp(a, 'gi');
            nodes[n].textContent = nodes[n].textContent.replace(r, b);
        } else {
            htmlreplace(a, b, nodes[n]);
        }
    }
}

htmlreplace('a', 'r');

Bookmarklet version:

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].nodeType==Node.TEXT_NODE){nodes[n].textContent=nodes[n].textContent.replace(new RegExp(a,'gi'),b);}else{htmlreplace(a,b,nodes[n]);}}}htmlreplace('old','new');
羁〃客ぐ 2024-08-03 17:17:29

如果您替换了innerHtml,那么您将破坏页面上的所有dom 事件。 尝试遍历文档来替换文本:

function newTheOlds(node) {
    node = node || document.body;
    if(node.nodeType == 3) {
        // Text node
        node.nodeValue = node.nodeValue.split('old').join('new');
    } else {
        var nodes = node.childNodes;
        if(nodes) {
            var i = nodes.length;
            while(i--) newTheOlds(nodes[i]);
        }
    }
}

newTheOlds();

如果不需要模式匹配,则拆分/连接比“替换”更快。 如果您需要模式匹配,请使用“替换”和正则表达式:

node.nodeValue = node.nodeValue.replace(/(?:dog|cat)(s?)/, 'buffalo$1');

作为书签:

javascript:function newTheOlds(node){node=node||document.body;if(node.nodeType==3){node.nodeValue=node.nodeValue.split('old').join('new');}else{var nodes=node.childNodes;if(nodes){var i=nodes.length;while(i--)newTheOlds(nodes[i]);}}}newTheOlds();

If you replace the innerHtml then you will destroy any dom events you have on the page. Try traversing the document to replace text:

function newTheOlds(node) {
    node = node || document.body;
    if(node.nodeType == 3) {
        // Text node
        node.nodeValue = node.nodeValue.split('old').join('new');
    } else {
        var nodes = node.childNodes;
        if(nodes) {
            var i = nodes.length;
            while(i--) newTheOlds(nodes[i]);
        }
    }
}

newTheOlds();

The split/join is faster than doing "replace" if you do not need pattern matching. If you need pattern matching then use "replace" and a regex:

node.nodeValue = node.nodeValue.replace(/(?:dog|cat)(s?)/, 'buffalo$1');

As a bookmarklet:

javascript:function newTheOlds(node){node=node||document.body;if(node.nodeType==3){node.nodeValue=node.nodeValue.split('old').join('new');}else{var nodes=node.childNodes;if(nodes){var i=nodes.length;while(i--)newTheOlds(nodes[i]);}}}newTheOlds();
不一样的天空 2024-08-03 17:17:29

对于较旧的浏览器,需要将 Node.TEXT_NODE 更改为 3,并将 node.textContent 更改为 node.nodeValue; 所以最终的函数应该是:

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].nodeType == 3) { //Node.TEXT_NODE == 3
            var r = new RegExp(a, 'gi');
            nodes[n].nodeValue = nodes[n].nodeValue.replace(r, b);
        } else {
            htmlreplace(a, b, nodes[n]);
        }
    }
}

For older browsers will need to change Node.TEXT_NODE to 3 and the node.textContent to node.nodeValue; so final function should read:

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].nodeType == 3) { //Node.TEXT_NODE == 3
            var r = new RegExp(a, 'gi');
            nodes[n].nodeValue = nodes[n].nodeValue.replace(r, b);
        } else {
            htmlreplace(a, b, nodes[n]);
        }
    }
}
谁的年少不轻狂 2024-08-03 17:17:29

与 jQuery 一起工作的简单行:

`javascript:var a = function(){$("body").html($("body").html().replace(/old/g,'new'));return;}; a();`

没有 jQuery:不

`javascript:function a (){document.body.innerHTML=document.body.innerHTML.replace(/old/g, "new" );return;}; a();`

返回任何内容的函数非常重要,因此在执行小书签后浏览器不会重定向到任何地方。

A simple line that works along jQuery:

`javascript:var a = function(){$("body").html($("body").html().replace(/old/g,'new'));return;}; a();`

Without jQuery:

`javascript:function a (){document.body.innerHTML=document.body.innerHTML.replace(/old/g, "new" );return;}; a();`

The function returning nothing is very important, so the browser is not redirected anywhere after executing the bookmarklet.

绮烟 2024-08-03 17:17:29

另一种递归方法:

function replaceText(oldText, newText, node){ 
  node = node || document.body; 

  var childs = node.childNodes, i = 0;

  while(node = childs[i]){ 
    if (node.nodeType == Node.TEXT_NODE){ 
      node.textContent = node.textContent.replace(oldText, newText); 
    } else { 
      replaceText(oldText, newText, node); 
    } 
    i++; 
  } 
}

缩小书签:

javascript:function replaceText(ot,nt,n){n=n||document.body;var cs=n.childNodes,i=0;while(n=cs[i]){if(n.nodeType==Node.TEXT_NODE){n.textContent=n.textContent.replace(ot,nt);}else{replaceText(ot,nt,n);};i++;}};replaceText('old','new');

Yet another recursive approach:

function replaceText(oldText, newText, node){ 
  node = node || document.body; 

  var childs = node.childNodes, i = 0;

  while(node = childs[i]){ 
    if (node.nodeType == Node.TEXT_NODE){ 
      node.textContent = node.textContent.replace(oldText, newText); 
    } else { 
      replaceText(oldText, newText, node); 
    } 
    i++; 
  } 
}

Minified bookmarklet:

javascript:function replaceText(ot,nt,n){n=n||document.body;var cs=n.childNodes,i=0;while(n=cs[i]){if(n.nodeType==Node.TEXT_NODE){n.textContent=n.textContent.replace(ot,nt);}else{replaceText(ot,nt,n);};i++;}};replaceText('old','new');
甜中书 2024-08-03 17:17:29

好的,我只是将人们在一个答案中提出的一些精彩内容整合起来。

这里是 Sixgear 的 jQuery 代码,但是是可移植的(我从大 G 中获取 jQuery)并缩小到一个小书签中:

javascript:var scrEl=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 htmlreplace(a,b,element){if(!element)element=document.body;var nodes=$(element).contents().each(function(){if(this.nodeType==Node.TEXT_NODE){var r=new RegExp(a,'gi');this.textContent=this.textContent.replace(r,b);}else{htmlreplace(a,b,this);}});}htmlreplace('old','new');

注意,“old”可以是“字符串文字”,也可以是“reg[Ee]x”。

事实上,现在我想起来,sixgear 是最好的答案,尤其是我的增强。 我找不到其他答案添加的任何内容,使用 jQuery 实现了令人难以置信的 X 浏览器兼容性。 另外,我实在是太懒了。 社区维基,享受吧!

Okay, I'm just consolidating some of the great stuff that people are putting up in one answer.

Here is sixthgear's jQuery code, but made portable (I source jQuery from the big G) and minified into a bookmarklet:

javascript:var scrEl=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 htmlreplace(a,b,element){if(!element)element=document.body;var nodes=$(element).contents().each(function(){if(this.nodeType==Node.TEXT_NODE){var r=new RegExp(a,'gi');this.textContent=this.textContent.replace(r,b);}else{htmlreplace(a,b,this);}});}htmlreplace('old','new');

NOTE that 'old' can be either a 'string literal', or a 'reg[Ee]x'.

Actually, now that I think about it, sixthgear's is the best answer, especially with my enhancements. I can't find anything that the other answers add over it, using jQuery achieves incredible X-browser compatibility. Plus, I'm just too damn lazy. community wiki, Enjoy!

夏末的微笑 2024-08-03 17:17:29

嘿,你可以尝试这个,问题是它会搜索整个身体,所以甚至属性等都会改变。

javascript:document.body.innerHTML=document.body.innerHTML.replace( /old/g, "new" );

Hey you could try this, problem is it searches the entire body so even attributes and such get changed.

javascript:document.body.innerHTML=document.body.innerHTML.replace( /old/g, "new" );
小伙你站住 2024-08-03 17:17:29

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

我计划在 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?

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