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>
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();
}
}
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With