Firefox 中的 Javascript 文本范围 +油猴

发布于 2024-09-17 20:59:20 字数 488 浏览 3 评论 0原文

我是初学者,但我需要一个可以在某些页面上帮助我的脚本。 我需要一个脚本来分析页面中的文本,如果有我正在搜索的单词,则会显示一个弹出窗口,如果没有,则不执行任何操作。 这是代码,但它不适用于 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 技术交流群。

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

发布评论

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

评论(1

暮倦 2024-09-24 20:59:20

Firefox 有 Range 而不是 TextRange。如果您只想检查页面中是否存在特定文本而不突出显示它,则以下内容将在 Firefox 中工作。使用 Selection 对象的原因是,包含整个正文的 Range 将包含正文中的所有文本节点,包括

function visibleTextContains(str) {
    var range = document.createRange();
    range.selectNodeContents(document.body);
    var sel = window.getSelection();
    sel.removeAllRanges();
    sel.addRange(range);
    var visibleText = sel.toString();
    sel.removeAllRanges();
    return visibleText.indexOf(str) > -1;
}

Firefox has Ranges instead of TextRanges. 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 the Selection 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 calling toString() on the Selection 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.

function visibleTextContains(str) {
    var range = document.createRange();
    range.selectNodeContents(document.body);
    var sel = window.getSelection();
    sel.removeAllRanges();
    sel.addRange(range);
    var visibleText = sel.toString();
    sel.removeAllRanges();
    return visibleText.indexOf(str) > -1;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文