删除HTML5通知权限 [英] Remove HTML5 notification permissions

查看:76
本文介绍了删除HTML5通知权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您可以通过运行来提示用户允许或拒绝来自浏览器的桌面通知:

You can prompt a user to allow or deny desktop notifications from the browser by running:

Notification.requestPermission(callback);

但是可以通过代码删除该权限吗?我们希望用户可以选择切换通知。这可以通过JavaScript实现,还是我们需要在其他地方保存该选项?

But is it possible to remove that permission by code? We want our users to have the option to toggle notifications. Can this be achieved by JavaScript or do we need to save that option elsewhere?

推荐答案

不,你的脚本没办法以编程方式放弃显示通知的权限。 API规范除了 requestPermission 。 (当然,浏览器可能有一个选项菜单,允许用户撤消域的权限,但这是浏览器级选项,而不是网站级选项。例如,在Chrome中,您可以看到此选项菜单单击地址栏左侧的图标。)

No, there is no way for your script to programmatically relinquish permission to show notifications. The API specification does not have any permission-related functions aside from requestPermission. (Of course, a browser may have an options menu that allows the user to revoke permission for a domain, but that's a browser-level option, not a site-level option. For example, in Chrome, you can see this options menu by clicking the icon in the left of the address bar.)

如果您不想显示通知,只需不要调用新通知

If you don't want to show notifications, simply don't call new Notification.

您可以将所有来电换行至新通知内部条件:

You can either wrap all your calls to new Notification inside conditions:

if(notifications_allowed) {
    new Notification(...);
}

或者你可以重写通知构造函数包含一个contiditional并根据需要调用原始的通知

Or you can rewrite the Notification constructor to contain a contiditional and call the original Notification as appropriate:

(function() {
    var oldNofitication = Notification;
    Notification = function() {
        if(notifications_allowed) {
            oldNotification.apply(this, arguments);
        }
    }
})();

如果使用供应商前缀构造函数或函数(例如, webkitNotifications.createNotification ),那么你还需要重写每一个以你的选项变量为条件。

If you use vendor-prefixed constructors or functions (e.g., webkitNotifications.createNotification), then you'll need to rewrite each of those as well to be conditional on your options variable.

这篇关于删除HTML5通知权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆