The problem is that an arrow function can't be used as a constructor (Source).
The project that uses this code still has an arrow function: https://github.com/jiahaog/nativefier/blob/master/app/src/static/preload.js but it runs in Electron which might explain why it behaves differently.
If targeting recent browsers, rather use a named function like this:
(function () {
function notifyCallback(title, opt) {
console.log("title", title);
}
const OldNotify = window.Notification;
function newNotify(title, opt) {
notifyCallback(title, opt);
return new OldNotify(title, opt);
}
newNotify.requestPermission = OldNotify.requestPermission.bind(OldNotify);
Object.defineProperty(newNotify, 'permission', {
get: function() {
return OldNotify.permission;
}
});
window.Notification = newNotify;
})();
Notification.requestPermission(function (permission) {
if (permission === "granted") {
const notif = new Notification('My title');
}
});
The proxy thus created will then be effective when other code/libraries call new Notification()
like in my example. I have moved the proxifying logic to the top-level to ensure that the other code/libraries won't keep a reference on the native Notification
before the user accepts to receive notifications. You must also put the code at the very first place to guarantee that.
And if your target browsers support ECMAScript 6, there is a much more elegant way to do it:
(function () {
function notifyCallback(title, opt) {
console.log("title", title);
}
const handler = {
construct(target, args) {
notifyCallback(...args);
return new target(...args);
}
};
const ProxifiedNotification = new Proxy(Notification, handler);
window.Notification = ProxifiedNotification;
})();
Notification.requestPermission(function (permission) {
if (permission === "granted") {
const notif = new Notification('My title');
}
});
It's much more scalable (no impact when the Notification
API changes in future ECMAScript versions since it allows to manipulate the native Notification
rather than a handmade one).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…