diff --git a/src/webview/notifications.ts b/src/webview/notifications.ts index e4401ab6e0..1ba4e12d10 100644 --- a/src/webview/notifications.ts +++ b/src/webview/notifications.ts @@ -1,3 +1,4 @@ +// eslint-disable-next-line max-classes-per-file import { ipcRenderer } from 'electron'; import { v4 as uuidV4 } from 'uuid'; @@ -5,10 +6,9 @@ import { v4 as uuidV4 } from 'uuid'; const debug = require('../preload-safe-debug')('Ferdium:Notifications'); export class NotificationsHandler { - onNotify = (data: { title: string; options: any; notificationId: string }) => - data; + onNotify = (data: any) => data; - displayNotification(title: string, options: any) { + displayNotification(title: string, options: string) { return new Promise(resolve => { debug('New notification', title, options); @@ -30,53 +30,54 @@ export class NotificationsHandler { } } -export const notificationsClassDefinition = `(() => { - class Notification { - static permission = 'granted'; - - constructor(title = '', options = {}) { - this.title = title; - this.options = options; - try { - window.ferdium.displayNotification(title, options) - .then(() => { - if (typeof (this.onClick) === 'function') { - this.onClick(); - } - }); - } catch(error) { - this.options.onClick = null; - window.ferdium.displayNotification(title, options) - .then(() => { - if (typeof (this.onClick) === 'function') { - this.onClick(); - } - }); - } - } +class FerdiumNotification extends window.Notification { + static permission: NotificationPermission = 'granted'; + + title: string; - static requestPermission(cb = null) { - if (!cb) { - return new Promise((resolve) => { - resolve(Notification.permission); - }); - } + options: any; - if (typeof (cb) === 'function') { - return cb(Notification.permission); - } + constructor(title = '', options = {}) { + super(title, options); + this.title = title; + this.options = options as { onClick?: () => void }; + try { + window.ferdium.displayNotification(title, options).then(() => { + if (typeof this.onClick === 'function') { + this.onClick(); + } + }); + } catch { + this.options.onClick = null; + window.ferdium.displayNotification(title, options).then(() => { + if (typeof this.onClick === 'function') { + this.onClick(); + } + }); + } + } - return Notification.permission; + static requestPermission(cb: any = null) { + if (!cb) { + return Promise.resolve(Notification.permission); } - onNotify(data) { - return data; + if (typeof cb === 'function') { + return cb(Notification.permission); } - onClick() {} + return Notification.permission; + } - close() {} + onNotify(data) { + return data; } - window.Notification = Notification; -})();`; + onClick() {} + + close() {} +} + +export const notificationInject = (() => { + window.Notification = FerdiumNotification; +})(); diff --git a/src/webview/recipe.ts b/src/webview/recipe.ts index 148ea6fab5..47ef4ff2a4 100644 --- a/src/webview/recipe.ts +++ b/src/webview/recipe.ts @@ -29,10 +29,7 @@ import { removeDarkModeStyle, } from './darkmode'; import FindInPage from './find'; -import { - notificationsClassDefinition, - NotificationsHandler, -} from './notifications'; +import { notificationInject, NotificationsHandler } from './notifications'; import { getDisplayMediaSelector, screenShareCss, @@ -125,6 +122,7 @@ contextBridge.exposeInMainWorld('ferdium', { setDialogTitle: (title: string | null | undefined) => dialogTitleHandler.setDialogTitle(title), displayNotification: (title: string, options: any) => { + debug('New notification BASE', title, options); notificationsHandler.displayNotification( title, // The following line is needed so that a proper clone of the "options" object is made. @@ -138,7 +136,7 @@ contextBridge.exposeInMainWorld('ferdium', { ipcRenderer.sendToHost( 'inject-js-unsafe', 'window.open = window.ferdium.open;', - notificationsClassDefinition, + `${notificationInject}`, screenShareJs, );