Skip to content

Commit

Permalink
refactor class
Browse files Browse the repository at this point in the history
  • Loading branch information
SpecialAro committed Feb 25, 2024
1 parent 28cbc56 commit 7e266d7
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 48 deletions.
87 changes: 44 additions & 43 deletions src/webview/notifications.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// eslint-disable-next-line max-classes-per-file
import { ipcRenderer } from 'electron';

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);

Expand All @@ -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;
})();
8 changes: 3 additions & 5 deletions src/webview/recipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.
Expand All @@ -138,7 +136,7 @@ contextBridge.exposeInMainWorld('ferdium', {
ipcRenderer.sendToHost(
'inject-js-unsafe',
'window.open = window.ferdium.open;',
notificationsClassDefinition,
`${notificationInject}`,
screenShareJs,
);

Expand Down

0 comments on commit 7e266d7

Please sign in to comment.