iPromise Grandmaster Cheater Reputation: -1
Joined: 27 Jun 2009 Posts: 529 Location: Canada
|
Posted: Thu Mar 28, 2019 4:03 pm Post subject: Implicit-override in Javascript |
|
|
I'm no Javascript expert, so I'm hoping someone here might have suggestions.
I'm developing a chrome extension and need to access several properties defined inside an external script (that's running concurrently with mine).
The original script defines an click listener for button like so:
Code: |
(function() {
...
var config = getUserConfig(...);
...
function begin(config) {
// Initialize stuff here
}
...
$("#button").click(function() {
begin(config);
});
})(); |
The original listener has access to a config property in it's scope. I need to access that property in my code, and I am trying to get a reference to it somehow. Notice that the begin() function takes the config property as a parameter.
I am trying to get a reference to config by overriding begin(). I define my own begin method, then I replace the old click listener with a new one that, binds the old click listener to the current scope, and then calls the old click listener, in hopes that the old click listener calls the overridden version of begin (I really hope that made sense).
Code: |
$.each($._data($("#button")[0], "events"), function(i, event) {
$.each(event, function(j, h) {
if (i == "click") {
var oldHandler = h.handler;
h.handler = function() {
begin = function(config) {
console.log("new begin() called");
console.log(config);
}
var newFunc = oldHandler.bind(this);
newFunc();
}
}
});
}); |
The old handler still calls the old begin() function. I've tried a bunch of other techniques as well, but I haven't had any success. I set a breakpoint in Chrome Dev Tools and followed the stack trace. When I step into newFunc(), it still calls the old begin() function.
|
|