使用延迟标记删除生产网站中的脚本标记

问题描述 投票:0回答:2

我想删除远程js并将本地js加载到一个生产网站中。

所以我使用以下用户脚本

// ==UserScript==
// @name         TV full screen
// @match        https://example.com/*
// @grant        none
// @run-at       document-start
// ==/UserScript==

new MutationObserver((mutations, observer) => {
    const script =  document.querySelector('script[src^="/static/bundles/main"]');
  if (!script ) {
    return;
  }
  observer.disconnect();
  script .remove();
  console.log('SRC removed');
   document.head.appendChild(document.createElement('script')).src = "https://localhost/tv/my.js";



})
  .observe(document.documentElement, { childList: true, subtree: true });

以下是生产网站中的代码

<script defer crossorigin="anonymous" type="text/javascript" src="/static/bundles/main.js" ></script>

由于脚本标记中存在延迟代码,即使删除了标记,实际的源代码仍在执行中。如何完全删除该脚本标记并停止执行并加载本地文件。

javascript tampermonkey userscripts
2个回答
0
投票

尝试更改选择器以更改行:

const script =  document.querySelector('script[src^="/static/bundles/main_chart"]');

with:

const script =  document.querySelector('script[src^="/static/bundles/main.js"]');

0
投票

可能像这样:

 var myScript = document.createElement('script');         

 if (location.hostname === "localhost" || location.hostname === "127.0.0.1"){
    myScript.src = '/path/to/my_LOCAL_Script...';
 }else{
    myScript.src = '/path/to/my_REMOTE_Script...';
 }

 document.head.appendChild(myScript);
© www.soinside.com 2019 - 2024. All rights reserved.