Gracie williams
Gracie williams

Reputation: 1145

Remove Script tag in production website with defer tag

I want to remove remote js and load my local js in one production website.

So I use following userscript

// ==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 });

Following is the code present in production website

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

since there is defer code in script tag , still the actual source is getting executed , eventhough tag is removed..How do I completely remove that script tag and stop execution and load my local file.

Upvotes: 0

Views: 856

Answers (2)

mscdeveloper
mscdeveloper

Reputation: 2889

May be Like This:

 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);

Upvotes: 0

Juan Daniel Ornella
Juan Daniel Ornella

Reputation: 623

Try to change the selector changing the line:

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

with:

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

Upvotes: 0

Related Questions