I saw the question Inject javascript code into anonymous function scope but it seems to be not understood at all by the answers there, so I'm making a new question, to clarify the matter.
Let's say there is some code on a website like:
(function() {
function method() {
//do some secret stuff
}
})();
And I have no access to the source code; say I want to make some kind of addon, or extension, that will be able to access "method". How should this be done?
I'm aware that in general, this is an anonymous function expression, so the contents of its scope are hidden, but with chrome extensions its possible to inject JavaScript code into a page before it loads, for example. Would such injected code be able to change the source code itself somehow, to add a global reference, inside the anonymous function, to method? For example, something that executes before the previous code would be, to modify the innerHTML of the script tag?
Or perhaps make a new browser, with a modified version of V8 which allows access to anonymous functions, and to host that browser online and control it with NodeJS, and browse the site, on the client side, purely through the lens of the server-side modified JavaScript engine browser? Is there perhaps another way?
If the script is not inline on the page, but rather, a link to another file which contains the anonymous function, like , would one be able to inject some JavaScript into the page, or do something else with chrome extensions, to simply redirect the HTTP reference of that link, to ones own server, which would process the JavaScript code and insert a global variable reference to the bottom of the anonymous function, or just remove the anonymous function altogether?
So for example, with chrome extensions, it's possible to redirect an HTTP request. So just append the original URL as a GET request to your server, so the above becomes ? Is there a better way?