Firefox 中的 Javascript 文本范围 +油猴
我是初学者,但我需要一个可以在某些页面上帮助我的脚本。 我需要一个脚本来分析页面中的文本,如果有我正在搜索的单词,则会显示一个弹出窗口,如果没有,则不执行任何操作。 这是代码,但它不适用于 Firefox,因为文本范围功能仅适用于 IE(可能也不起作用,因为我无法执行 JavaScript)。 有人告诉我使用 tostrign 函数,但我不知道如何使用:P 因此,如果您可以更改它以使其能够正常工作,那就太好了。
document.body.onload = cerca();
function cerca() {
Range = document.body.createRange();
campo = toString();
var c = campo.findText("ciao");
if(c){
alert("Corrispondenza Trovata") } else {alert("nada"); }
}
非常感谢 :)
i m a beginner but i need a script that could help me in some pages.
I need a script that analazizes the text in the page and if there is the word that i m searching for it shows a popoup, if not does nothing.
here s the code but it doesn t work with firefox because the text range function is only for IE (probably won t work either because i cant do javascript).
Someone told me to use the tostrign function but i don't know how :P
So if you could please change it so that it could work that would be really great.
document.body.onload = cerca();
function cerca() {
Range = document.body.createRange();
campo = toString();
var c = campo.findText("ciao");
if(c){
alert("Corrispondenza Trovata") } else {alert("nada"); }
}
thank you very much :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Firefox 有
Range
而不是TextRange
。如果您只想检查页面中是否存在特定文本而不突出显示它,则以下内容将在 Firefox 中工作。使用Selection
对象的原因是,包含整个正文的 Range 将包含正文中的所有文本节点,包括元素内的文本节点,而结果在
Selection
对象上调用toString()
的方法仅包含可见文本,这正是您想要的。另请注意,此函数会清除当前选择(如果存在);如果这对您来说是个问题,那么您可以存储并稍后恢复所选范围。Firefox has
Range
s instead ofTextRange
s. If you just want to check for the presence of a particular piece of text in the page and not highlight it, the following will work in Firefox. The reason for using theSelection
object is that a Range encompassing the whole body would includes all text nodes within the body, including those inside<script>
elements, while the result of callingtoString()
on theSelection
object only includes visible text, which is what you want. Note also that this function wipes out the current selection, if one exists; if this is a problem for you then you can store and later restore the selected ranges.