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

如何覆蓋 JavaScript web api 通知對象

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

問題描述

我有一個場景,我需要為電子窗口內的 web 視圖中加載的網(wǎng)頁打開和關閉通知.為此,我在 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.

問題是我要控制的網(wǎng)頁正在使用 Notification.permission 方法 (參考這里).現(xiàn)在我的新通知對象上沒有權限屬性.我無法以可以更新其構造函數(shù)的方式覆蓋 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.

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

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模板網(wǎng)!

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

相關文檔推薦

How to fix BrowserWindow is not a constructor error when creating child window in Electron renderer process(在 Electron 渲染器進程中創(chuàng)建子窗口時如何修復 BrowserWindow 不是構造函數(shù)錯誤) - IT屋-程序員軟件開發(fā)技術
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 上執(zhí)行腳本)
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?(如何在電子應用程序中訪問相機/網(wǎng)絡攝像頭?)
主站蜘蛛池模板: 久久久婷 | 色香蕉在线 | 欧美精品一区二区三区四区五区 | 超碰在线国产 | 琪琪午夜伦伦电影福利片 | 天天艹| 久久福利网站 | 91麻豆精品一区二区三区 | 久久国产精品-国产精品 | 亚洲精彩免费视频 | 国产一级毛片精品完整视频版 | 日韩视频在线一区二区 | 综合久 | 黄色a视频 | 成人免费视频在线观看 | 日韩视频高清 | 欧美一区精品 | 国产欧美精品 | 欧美日韩成人影院 | 一级黄片一级毛片 | 欧美激情在线精品一区二区三区 | 免费在线观看成人av | 中文字幕在线免费观看 | 欧美九九九 | 久www| 亚洲精品1区2区3区 91免费看片 | 91精品国产综合久久久动漫日韩 | 精品亚洲一区二区 | 一区二区免费高清视频 | 精品久久99| 三级视频国产 | 国产一二区免费视频 | 午夜一区| 国产97碰免费视频 | 精品国产精品三级精品av网址 | 国产三区视频在线观看 | 99热欧美| 精久久 | 欧美日韩国产一区二区三区 | 久久小视频 | 久久精品欧美视频 |