Executing [removed] elements inserted with [removed]

后端 未结 20 2860
囚心锁ツ
囚心锁ツ 2020-11-22 00:12

I\'ve got a script that inserts some content into an element using innerHTML.

The content could for example be:



        
20条回答
  •  故里飘歌
    2020-11-22 01:06

    Made this new helper function in TypeScript, maybe someone will appreciate it. If you remove type declaration from script parameter it will just be plain JS.

    const evalPageScripts = () => {
      const scripts = document.querySelectorAll('script');
    
      scripts.forEach((script: HTMLScriptElement) => {
        const newScript = document.createElement('script');
        newScript.type = 'text/javascript';
        newScript.src = script.src;
    
        if (script.parentNode) {
          script.parentNode.removeChild(script);
        }
    
        return document.body.appendChild(newScript);
      })
    };
    
    export default evalPageScripts;

提交回复
热议问题