Safari 扩展 - 如何响应设置更改?

发布于 2024-09-05 20:48:19 字数 312 浏览 3 评论 0原文

我目前正在开发 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淑女气质 2024-09-12 20:48:19

我相信您需要在函数中包含“事件”作为参数,因此它看起来像这样:

function numberChanged(event) 
{
     if(event.key == "number")
         alert("Number has changed!");
}

但是,也就是说,它对我来说也不能正常工作(无论有没有参数),所以我可能是错的。有趣的是,每次我更改字段或单击此 stackoverflow 表单上的按钮时,即使我没有更改设置,我的警报(与您的类似)也会触发。完全奇怪。

更新:我终于让它工作了。苹果提供的例子是错误的。所以答案有两个部分。我给出了上面的第一部分 - 您确实需要将“事件”作为参数添加到您的函数中。第二部分是 addeventlistener 必须在设置对象上完成,而不是像苹果向您展示的那样,使用 global.html 页面中的“self”。因此,工作调用对您来说将如下所示:

safari.extension.settings.addEventListener("change",numberChanged,false);

I believe that you need to include ‘event’ as a parameter in your function so it looks like this:

function numberChanged(event) 
{
     if(event.key == "number")
         alert("Number has changed!");
}

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:

safari.extension.settings.addEventListener("change",numberChanged,false);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文