鍍金池/ 問答/HTML/ Electron怎么監(jiān)聽網(wǎng)頁的Notification通知

Electron怎么監(jiān)聽網(wǎng)頁的Notification通知

我的項目是直接 loadURL 一個遠程網(wǎng)頁。
網(wǎng)頁可能會發(fā)出 HTML5 的 Notification 桌面通知,怎樣在 Electron 捕獲到網(wǎng)頁發(fā)出的 Notification 通知?
我需要處理網(wǎng)頁發(fā)出的桌面通知,然后用 Electron 內置的 Notification 觸發(fā)。

回答
編輯回答
殘淚

這里引用Stackoverflow上的一個回答
https://stackoverflow.com/que...

function setNotificationCallback(callback) {

    const OldNotify = window.Notification;
    const newNotify = (title, opt) => {
        callback(title, opt);
        return new OldNotify(title, opt);
    };
    newNotify.requestPermission = OldNotify.requestPermission.bind(OldNotify);
    Object.defineProperty(newNotify, 'permission', {
        get: () => {
            return OldNotify.permission;
        }
    });

    window.Notification = newNotify;
}

(注意將上面的語法改一下。

接下來運行

setNotificationCallback(function(t, o){

});

即可

2017年8月9日 23:12