I'm trying to figure out how to listen to an event emitter from my greasemonkey script, but I keep getting access violation errors (Permission denied to access object
).
Page
The page contains a simple event emitter:
var emitter = function(){
this.events = {};
}
emitter.prototype.on = function(eventName, closure){
this.events[eventName] = this.events[eventName] || [];
this.events[eventName].push(closure);
};
emitter.prototype.emit = function(eventName, data){
if(this.events[eventName]){
this.events[eventName].forEach(function(fn){
return fn(data);
});
}
}
var test = new emitter();
test.emit('test', {data:'test'});
Script
This throws an access violation error (this used to work a while ago, but I guess it got patched or something):
unsafeWindow.test.on('test', function(data){
console.log(data);
});
I managed to get it working. The solution was to export the callback function into unsafe context via exportFunction(myFunction, unsafeWindow)
The script part should look like this:
unsafeWindow.test.on('test', exportFunction(function(data){
console.log(data);
}, unsafeWindow));
Big thanks to wOxxOm for pointing this out.