从我的greasemonkey脚本中听一个事件 [英] Listening to an event from my greasemonkey script
问题描述
我试图弄清楚如何从我的greasemonkey脚本中监听事件发送器,但是我一直在收到访问冲突错误(访问对象的权限被拒绝
)。
Page
该页面包含一个简单的事件发射器:
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'});
脚本
这会引发访问冲突错误(这曾经工作了一阵子,但我想它得到了补丁或什么):
pre $ unsafeWindow.test.on(' test',函数(data){
console.log(data);
});
我设法让它工作。解决的办法是通过 exportFunction(myFunction,unsafeWindow)
将回调函数导出到不安全的上下文中看起来像这样:
$ b $ pre $ unsafeWindow.test.on('test',exportFunction(function(data){
console .log(data);
},unsafeWindow));
非常感谢 wOxxOm 指出这一点。
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.
这篇关于从我的greasemonkey脚本中听一个事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!