如何在React中更新DOM后使ScriptTagger.js文件重新运行?

huangapple go评论61阅读模式
英文:

How do I make ScriptTagger.js file re-run after updating the dom in react?

问题

I want to use the following websites file:
https://www.blueletterbible.org/webtools/BLB_ScriptTagger.cfm
In the Website / Non-Wordpress Blog Installation section, I have added the script in index.html as well as in the helmet, and it works only with the already existing elements on the page.
For example, if [Romans 1:16-18] was already there, the script works fine. But that is not actually what I need.
If I get an API response from the backend that contains these Romans 1:16-18 or Zephaniah 3:17 and display them on my page, the script doesn't work? How can I rerun the script each time I get a response from the backend because the user can send multiple ones? I have tried to use a useEffect to remove the script and add it every time the user sends a request, but in vain.

useEffect(() => {
    const script = document.createElement('script');
    
    script.src = "https://www.blueletterbible.org/assets-v3/scripts/blbToolTip/BLB_ScriptTagger-min.js";
    script.async = true;
    console.log("reload");
    document.body.appendChild(script);

    return () => {
        document.body.removeChild(script);
    }
}, [chatLog]);
英文:

I want to use the following websites file:
https://www.blueletterbible.org/webtools/BLB_ScriptTagger.cfm
In the Website / Non-Wordpress Blog Installation section I have added the script in index.html as well as in the helmet and it works only with the already existing elements in the page.
For example if [Romans 1:16-18] was already there the script works fine. But that is not actually what I need.
If I get an API response from the backend that contains these Romans 1:16-18 or Zephaniah 3:17 and display them in my page the script doesnt work? How can I re run the script each time I get a response from the backend because the user can send multiple ones. I have tried to use a useEffect to remove the script and add it every time the users sends a request but in vain.
Any suggestions please?

 useEffect(() => {
      const script = document.createElement('script');
  
      script.src = "https://www.blueletterbible.org/assets-v3/scripts/blbToolTip/BLB_ScriptTagger-min.js";
      script.async = true;
        console.log("reload")
      document.body.appendChild(script);
  
      return () => {
        document.body.removeChild(script);
      }
    }, [chatLog]); 

答案1

得分: 1

BLB ScriptTagger 在全局窗口对象上公开为 BLB.Tagger。您可以在每次文本更改后手动调用 BLB.Tagger.pageInit(),以便更新文本。

注意: 这可能会导致内存泄漏,因为 BLB 没有任何清理机制,但您需要进行性能分析并进行检查。

const { useState, useEffect } = React

const ps = [
  "此脚本将扫描您网页的文本以查找经文引用,例如:罗马书 1:16-18 或 约翰福音 3:16。然后将其链接并自动创建悬停功能。",
  "您可以设置首选翻译版本和各种其他设置。脚本还会检测与引用相关的有效翻译缩写,例如:西番雅书 3:17 ESV。",
  "此脚本还会自动捕捉其他引用样式,例如:申命记 8:2-6、10、17-18 和创世记 1:26-28;3:15。"
]

const Demo = () => {
  const [paragraphs, setParagraphs] = useState([])
  
  useEffect(() => {
    const len = paragraphs.length;
    
    if(len < 3) {
      setTimeout(() => {
        setParagraphs(ps.slice(0, len + 1))
      }, 1000)
    }
  }, [paragraphs])
  
  useEffect(() => {
    if(paragraphs.length) {
      BLB.Tagger.pageInit()
    }
  }, [paragraphs]);
  
  return (
    <div>
      {paragraphs.map((p, i) => <p key={i}>{p}</p>)}
    </div>
  )
}

ReactDOM
  .createRoot(root)
  .render(<Demo />)
<script crossorigin src="https://unpkg.com/react@18/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>

<script src='https://www.blueletterbible.org/assets-v3/scripts/blbToolTip/BLB_ScriptTagger.js' type='text/javascript'></script>
<script type='text/javascript'></script>

<div id="root"></div>
英文:

BLB ScriptTagger exposes itself as BLB.Tagger on the global window object. You can call BLB.Tagger.pageInit() manually after each text change, so it will update the text.

Note: This might cause memory leaks, because BLB doesn't have any cleanup mechanism, but you'll need to profile and check it.

<!-- begin snippet: js hide: false console: true babel: true -->

<!-- language: lang-js -->

const { useState, useEffect } = React

const ps = [&quot;This script will scan the text of your webpage for verse references such as this: Romans 1:16-18 or Jhn 3:16. It will then link them and automatically create and enable the hover.&quot;,&quot;You can set your preferred translation and various other settings. The script will also detect valid translation abbreviations connected with the reference, such as: Zephaniah 3:17 ESV&quot;,&quot;Other reference styles that this will automatically catch are: Deut 8:2-6, 10, 17-18 and Genesis 1:26-28; 3:15.&quot;]

const Demo = () =&gt; {
  const [paragraphs, setParagraphs] = useState([])
  
  useEffect(() =&gt; {
    const len = paragraphs.length;
    
    if(len &lt; 3) {
      setTimeout(() =&gt; {
        setParagraphs(ps.slice(0, len + 1))
      }, 1000)
    }
  }, [paragraphs])
  
  useEffect(() =&gt; {
    if(paragraphs.length) {
      BLB.Tagger.pageInit()
    }
  }, [paragraphs]);
  
  return (
    &lt;div&gt;
      {paragraphs.map((p, i) =&gt; &lt;p key={i}&gt;{p}&lt;/p&gt;)}
    &lt;/div&gt;
  )
}

ReactDOM
  .createRoot(root)
  .render(&lt;Demo /&gt;)

<!-- language: lang-html -->

&lt;script crossorigin src=&quot;https://unpkg.com/react@18/umd/react.development.js&quot;&gt;&lt;/script&gt;
&lt;script crossorigin src=&quot;https://unpkg.com/react-dom@18/umd/react-dom.development.js&quot;&gt;&lt;/script&gt;

&lt;script src=&#39;https://www.blueletterbible.org/assets-v3/scripts/blbToolTip/BLB_ScriptTagger.js&#39; type=&#39;text/javascript&#39;&gt;&lt;/script&gt;
&lt;script type=&#39;text/javascript&#39;&gt;&lt;/script&gt;

&lt;div id=&quot;root&quot;&gt;&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月17日 21:47:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76272840.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定