Remove HTML5 notification permissions

后端 未结 2 930
孤街浪徒
孤街浪徒 2021-01-11 15:45

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

Notification.requestPermission(callback);

But is

2条回答
  •  粉色の甜心
    2021-01-11 16:17

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

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

    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.

提交回复
热议问题