查找“old”的所有实例 在网页中,使用 javascript 书签将每个内容替换为“new”
我想要做的是将网页中的所有“旧”实例替换为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
一个防破坏的函数。 这意味着这不会触及任何标签或属性,只会触及文本。
书签版本:
A function that is clobber-proof. That mean's this won't touch any tags or attributes, only text.
Bookmarklet version:
如果您替换了innerHtml,那么您将破坏页面上的所有dom 事件。 尝试遍历文档来替换文本:
如果不需要模式匹配,则拆分/连接比“替换”更快。 如果您需要模式匹配,请使用“替换”和正则表达式:
作为书签:
If you replace the innerHtml then you will destroy any dom events you have on the page. Try traversing the document to replace text:
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:
As a bookmarklet:
对于较旧的浏览器,需要将 Node.TEXT_NODE 更改为 3,并将 node.textContent 更改为 node.nodeValue; 所以最终的函数应该是:
For older browsers will need to change Node.TEXT_NODE to 3 and the node.textContent to node.nodeValue; so final function should read:
与 jQuery 一起工作的简单行:
没有 jQuery:不
返回任何内容的函数非常重要,因此在执行小书签后浏览器不会重定向到任何地方。
A simple line that works along jQuery:
Without jQuery:
The function returning nothing is very important, so the browser is not redirected anywhere after executing the bookmarklet.
另一种递归方法:
缩小书签:
Yet another recursive approach:
Minified bookmarklet:
好的,我只是将人们在一个答案中提出的一些精彩内容整合起来。
这里是 Sixgear 的 jQuery 代码,但是是可移植的(我从大 G 中获取 jQuery)并缩小到一个小书签中:
注意,“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:
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!
嘿,你可以尝试这个,问题是它会搜索整个身体,所以甚至属性等都会改变。
Hey you could try this, problem is it searches the entire body so even attributes and such get changed.
我正在尝试对此进行稍微修改,以便它提示输入要搜索的文本,然后提示要替换的文本,当所有处理完成后,显示一个对话框,让我知道它已完成。
我计划在 phpmyadmin 数据库编辑页面上使用它,该页面将有任意数量的文本框填充文本(这是我需要它来搜索和替换)。 另外,要搜索和替换的文本可能是也可能不是多行,因此我在正则表达式中添加了“m”参数,而且,由于我将进行可能包含 html 的搜索/替换,因此它们里面经常会有引号/双引号。 例如:
搜索:
并且可能不替换任何内容(只是为了删除所有代码)或其他一些 html。 到目前为止,这是我想出的小书签(javascript,尤其是小书签不是我经常弄乱的东西)但是,它在查找/替换方面没有任何作用,尽管它确实正确执行提示。
有人有主意吗?
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:
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.
Anyone have any ideas?