4

i want to replace "script.js" to "demo.js". anyone please help me

<head>

    <script src="script.js" type="text/javascript" language="javaScript"></script>     

</head>
1
  • Why was this downvoted? If it's a duplicate, it should be noted. Commented Feb 28, 2017 at 7:18

2 Answers 2

11

Run your script early by @run-at document-start. Add an event listener beforescriptexecute and check the script source. When you detect the desired script, call preventDefault method of the event and replace the script tag in the DOM.

// ==UserScript==
// @name        demo-scriptreplace
// @include     http://example.com/*
// @version     1
// @run-at      document-start
// ==/UserScript==

window.addEventListener('beforescriptexecute',
  function(event)
  {
    var originalScript = event.target;

    // debug output of full qualified script url
    console.log('script detected:', originalScript.src);

    // script ends with 'originalscript.js' ?
    // you can test as well: '<full qualified url>' === originalScript.src
    if(/\/originalscript\.js$/.test(originalScript.src)) 
    { 
      var replacementScript = document.createElement('script');
      replacementScript.src = 'replacementscript.js';

      originalScript.parentNode.replaceChild(replacementScript, originalScript);

      // prevent execution of the original script
      event.preventDefault();
    }
  }
);
Sign up to request clarification or add additional context in comments.

1 Comment

This solution works nicely in Firefox but the beforescriptexecute is a non-standard event and not supported by Chrome and other browsers.
0

The question is actually not really descriptive, but Greasemonkey is actually using javascript. So let's assume you have "head" element in your page and you want this as a single element inside of "head" element.

That in Javascript you can achieve this by following:

var e = '<script src="script.js" type="text/javascript" language="javaScript"></script>';
document.head.innerHTML = e;

In order to append it you can just add plus sign infront of equal sign in second command:

var e = '<script src="script.js" type="text/javascript" language="javaScript"></script>';
document.head.innerHTML += e;

EDIT: Eventually, if you have single script element pointing to such source javascript file, you can also use this to identify element and replace attribute value directly:

document.querySelectorAll('script[src="script.js"]')[0].setAttribute('src','demo.js');

EDIT: Important to point out is, that this answers the question how to replace. You still may need to prevent original script from being executed as mentioned by Quasimodo (though it was not part of question)

2 Comments

When the HTML parser comes across the <script src, it will immediately download and execute the script. Replacing the src won't help here, because either the <script> tag will not be on the page yet (nothing to select and replace), or the <script> tag has already been parsed and it has run.
True, if it's part of document and page that is being loaded, you're right. Question is not really descriptive as mentioned and this execution prevention is not part of it. It's quite logical assumption though. I'll update my answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.