UNPKG

2.28 kBJavaScriptView Raw
1/**
2 Preload file that will be executed in the renderer process
3 */
4
5/**
6 * Note: This needs to be attached prior to the imports, as the they will delay
7 * the attachment till after the event has been raised.
8 */
9document.addEventListener('DOMContentLoaded', () => {
10 // Due to the early attachment, this triggers a linter error
11 // because it's not yet been defined.
12 // eslint-disable-next-line no-use-before-define
13 injectScripts();
14});
15
16// Disable imports being first due to the above event attachment
17import { ipcRenderer } from 'electron'; // eslint-disable-line import/first
18import path from 'path'; // eslint-disable-line import/first
19import fs from 'fs'; // eslint-disable-line import/first
20
21const INJECT_JS_PATH = path.join(__dirname, '../../', 'inject/inject.js');
22const log = require('loglevel');
23/**
24 * Patches window.Notification to:
25 * - set a callback on a new Notification
26 * - set a callback for clicks on notifications
27 * @param createCallback
28 * @param clickCallback
29 */
30function setNotificationCallback(createCallback, clickCallback) {
31 const OldNotify = window.Notification;
32 const newNotify = (title, opt) => {
33 createCallback(title, opt);
34 const instance = new OldNotify(title, opt);
35 instance.addEventListener('click', clickCallback);
36 return instance;
37 };
38 newNotify.requestPermission = OldNotify.requestPermission.bind(OldNotify);
39 Object.defineProperty(newNotify, 'permission', {
40 get: () => OldNotify.permission,
41 });
42
43 window.Notification = newNotify;
44}
45
46function injectScripts() {
47 const needToInject = fs.existsSync(INJECT_JS_PATH);
48 if (!needToInject) {
49 return;
50 }
51 // Dynamically require scripts
52 // eslint-disable-next-line global-require, import/no-dynamic-require
53 require(INJECT_JS_PATH);
54}
55
56function notifyNotificationCreate(title, opt) {
57 ipcRenderer.send('notification', title, opt);
58}
59function notifyNotificationClick() {
60 ipcRenderer.send('notification-click');
61}
62
63setNotificationCallback(notifyNotificationCreate, notifyNotificationClick);
64
65ipcRenderer.on('params', (event, message) => {
66 const appArgs = JSON.parse(message);
67 log.info('nativefier.json', appArgs);
68});
69
70ipcRenderer.on('debug', (event, message) => {
71 // eslint-disable-next-line no-console
72 log.info('debug:', message);
73});