There's a page eg. http://somesite.com
that runs some javascript code located in path eg.http://somesite.com/code.js
In that file -among other code- there's a function (badFunction(x,y)
).
I'd like to change the value of one of it's local variables, in order to make it equal with some another (local) variable (which is used inside in another function, also located in that remote javascript file).
So, based on these two answers 1 and 2 (i.e this userscript),
I've tried the following simple userscript, in order to replace the entire badFunction
with my own implementation, but it doesn't work (i.e my badFunction
is not injected in the loaded code.js
- I've checked it via Firefox's Debugger).
What I'm trying to accomplish with the userscript, is, while the code.js
is loaded, at the time the badFunction
definition is reached, to inject my function implementation, and then, let the rest code.js
continue it's loading normally.
What is wrong in my userscript? Or is my approach wrong altogether?
I'd prefer not give the actual URL of the forementioned remote javascript file as it's NSFW.
// ==UserScript==
// @name @document-start Example
// @version 1
// @namespace
// @include http://somesite.com/*
// @run-at document-start
// ==/UserScript==
var changed = 1; // script need to be edited with
window.addEventListener('beforescriptexecute', function(e) {
///for external script:
src = e.target.src;
if (src.search(/code\.js/) == "badFunction") {
changed--;
e.preventDefault();
e.stopPropagation();
append(badFunction);
};
///when done, remove the listener:
if(changed == 0) window.removeEventListener(e.type, arguments.callee, true);
}, true);
////// append with new block function:
function append(s) {
document.head.appendChild(document.createElement('script'))
.innerHTML = s.toString().replace(/^function.*{|}$/g, '');
}
function badFunction(x, y)
{
// my code
};