Listening to an event from my greasemonkey script

吃可爱长大的小学妹 提交于 2019-12-25 09:11:43

问题


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);
});

回答1:


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.



来源:https://stackoverflow.com/questions/38356677/listening-to-an-event-from-my-greasemonkey-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!