Safari 扩展 - 如何响应设置更改?
我目前正在开发 Safari 5 的扩展,我想在设置发生更改时运行侦听器函数。苹果提供了一个例子,但它对我不起作用。我目前在我的全局 html 文件中有这个监听器函数:
function numberChanged()
{
if(event.key == "number")
alert("Number has changed!");
}
safari.self.addEventListener("change", numberChanged, false);
我希望有人可以帮助我。有人知道我做错了什么吗?
I'm currently working on an Extension for Safari 5 and I want to run a listener function whenever Settings changes are made. Apple provides an example for that, but it doesn't work for me. I currently have this listener function in my global html file:
function numberChanged()
{
if(event.key == "number")
alert("Number has changed!");
}
safari.self.addEventListener("change", numberChanged, false);
I hope somebody can help me. Does somebody know what I'm doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信您需要在函数中包含“事件”作为参数,因此它看起来像这样:
但是,也就是说,它对我来说也不能正常工作(无论有没有参数),所以我可能是错的。有趣的是,每次我更改字段或单击此 stackoverflow 表单上的按钮时,即使我没有更改设置,我的警报(与您的类似)也会触发。完全奇怪。
更新:我终于让它工作了。苹果提供的例子是错误的。所以答案有两个部分。我给出了上面的第一部分 - 您确实需要将“事件”作为参数添加到您的函数中。第二部分是 addeventlistener 必须在设置对象上完成,而不是像苹果向您展示的那样,使用 global.html 页面中的“self”。因此,工作调用对您来说将如下所示:
I believe that you need to include ‘event’ as a parameter in your function so it looks like this:
however, that said, it’s not working properly for me either (with or without the param), so I might be wrong. Interestingly, every time I change a field or click a button on this stackoverflow form, my alert (similar to yours) IS firing, even though I did not change my setting. totally weird.
update: I got it working, finally. The example that apple provides is just wrong. So there are two parts to the answer. I gave the first part above — you do need to add ‘event’ as a parameter to your function. the second part is that the addeventlistener has to be done on the settings object and not, as apple shows you, using ‘self’ from the global.html page. so the working call would look like this for you: