久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何覆蓋 JavaScript web api 通知對象

How to override JavaScript web api Notification object(如何覆蓋 JavaScript web api 通知對象)
本文介紹了如何覆蓋 JavaScript web api 通知對象的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有一個場景,我需要為電子窗口內的 web 視圖中加載的網頁打開和關閉通知.為此,我在 webview 中注入了一個 preload 文件,它像這樣覆蓋 Notification 對象.

I have a scenario where I need to toggle notifications on and off for a webpage that is loaded in a webview inside an electron window. To do that I have injected a preload file inside the webview that overrides the Notification object like this.

window.oldNotification = window.Notification;
window.Notification = function() {
    let notificationEnabled = localStorage.getItem('notification-permissions') === 'true';
    if (notificationEnabled) {
        new window.oldNotification(...arguments);
    }
};

我正在通過更改本地存儲變量來啟用和禁用通知.

I am enabling and disabling notifications by changing a local storage variable.

問題是我要控制的網頁正在使用 Notification.permission 方法 (參考這里).現在我的新通知對象上沒有權限屬性.我無法以可以更新其構造函數的方式覆蓋 Notification 對象,以便我可以禁用通知并擁有原始 Notification 對象的其他屬性.

The issue is that the webpage that I want to control is using Notification.permission method (refer this). Now my new Notification object has no permission property on it. I am not able to override the Notification object in a way where I can update its constructor so that I can disable the notification and also have other properties of the original Notification object.

有沒有辦法實現這一點,或者這根本不可能?絕對歡迎任何幫助或建議.

Is there a way to achieve this or is this not possible at all? Any help or suggestion is absolutely welcome.

推薦答案

您可以輕松模擬 Notification API

You can easily emulate Notification API

window.Notification = function() {
  const notificationEnabled = Notification.permission === 'granted';
  return notificationEnabled ? new window.oldNotification(...arguments) : {};
};

Object.defineProperty(Notification, 'permission', {
  get() {
    return localStorage.getItem('notification-permissions') === 'true' ? 'granted' : 'denied';
  }
});

Notification.requestPermission = (callback) => {
  if (typeof callback === 'function') {
    callback(Notification.permission);
  }

  return Promise.resolve(Notification.permission);
};

這篇關于如何覆蓋 JavaScript web api 通知對象的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to fix BrowserWindow is not a constructor error when creating child window in Electron renderer process(在 Electron 渲染器進程中創建子窗口時如何修復 BrowserWindow 不是構造函數錯誤) - IT屋-程序員軟件開發技術
mainWindow.loadURL(quot;https://localhost:3000/quot;) show white screen on Electron app(mainWindow.loadURL(https://localhost:3000/) 在 Electron 應用程序上顯示白屏)
Electron webContents executeJavaScript : Cannot execute script on second on loadURL(Electron webContents executeJavaScript:無法在第二個 loadURL 上執行腳本)
how to use electron browser window inside components in angular-cli?(如何在angular-cli的組件內使用電子瀏覽器窗口?)
ElectronJS - sharing redux store between windows?(ElectronJS - 在 Windows 之間共享 redux 存儲?)
How to access camera/webcamera inside electron app?(如何在電子應用程序中訪問相機/網絡攝像頭?)
主站蜘蛛池模板: 亚洲成人一区二区在线 | 国产精品一区二区在线 | 一区二区三区播放 | 天堂一区在线观看 | 我要看黄色录像一级片 | 激情综合五月天 | 日韩欧美大片 | 久久久久一区 | 97天天干 | 欧美日韩亚洲一区 | 日韩精品中文字幕一区二区三区 | 日韩欧美不卡 | 日韩在线精品视频 | 精品国产第一区二区三区 | 一区二区三区回区在观看免费视频 | 成年人在线视频 | 精品亚洲一区二区三区 | 久久久91精品国产一区二区三区 | 欧美老少妇一级特黄一片 | 国产一区二区三区 | 欧美一区二区免费在线 | 日韩综合在线 | 久久久久黄色 | 成人在线免费看 | 青青草一区二区三区 | 久久久www成人免费无遮挡大片 | 一区在线观看 | 欧美成人免费在线视频 | 九九热这里只有精品在线观看 | 97国产成人 | 国产精品亚洲二区 | 久久久精品影院 | 精品一区二区三区中文字幕 | 成人在线不卡 | 日韩不卡在线观看 | 中文字幕一区二区三区日韩精品 | www日本在线观看 | 免费一区 | 久久久久国产一区二区三区 | 视频一区二区在线观看 | 亚洲欧美成人影院 |