2
2
mirror of https://github.com/Llewellynvdm/nativefier.git synced 2024-06-10 08:12:19 +00:00
nativefier/app/src/preload.ts
Adam Weeden bcdbd58f06
App: replace console.xyz calls with loglevel.xyz, with a level controlled by app argv --verbose (#1172)
In reference to request in https://github.com/nativefier/nativefier/pull/1168/files#r623753290 ,
this PR fixes a lot of the disparity in logging in the app, and fleshes the logging out a bit.
2021-04-30 23:21:37 -04:00

82 lines
2.4 KiB
TypeScript

/**
* Preload file that will be executed in the renderer process.
* Note: This needs to be attached **prior to imports**, as imports
* would delay the attachment till after the event has been raised.
*/
document.addEventListener('DOMContentLoaded', () => {
injectScripts(); // eslint-disable-line @typescript-eslint/no-use-before-define
});
import * as fs from 'fs';
import * as path from 'path';
import { ipcRenderer } from 'electron';
import * as log from 'loglevel';
export const INJECT_DIR = path.join(__dirname, '..', 'inject');
/**
* Patches window.Notification to:
* - set a callback on a new Notification
* - set a callback for clicks on notifications
* @param createCallback
* @param clickCallback
*/
function setNotificationCallback(createCallback, clickCallback) {
const OldNotify = window.Notification;
const newNotify = function (title, opt) {
createCallback(title, opt);
const instance = new OldNotify(title, opt);
instance.addEventListener('click', clickCallback);
return instance;
};
newNotify.requestPermission = OldNotify.requestPermission.bind(OldNotify);
Object.defineProperty(newNotify, 'permission', {
get: () => OldNotify.permission,
});
// @ts-ignore
window.Notification = newNotify;
}
function injectScripts() {
const needToInject = fs.existsSync(INJECT_DIR);
if (!needToInject) {
return;
}
// Dynamically require scripts
try {
const jsFiles = fs
.readdirSync(INJECT_DIR, { withFileTypes: true })
.filter(
(injectFile) => injectFile.isFile() && injectFile.name.endsWith('.js'),
)
.map((jsFileStat) => path.join('..', 'inject', jsFileStat.name));
for (const jsFile of jsFiles) {
log.debug('Injecting JS file', jsFile);
require(jsFile);
}
} catch (error) {
log.error('Error encoutered injecting JS files', error);
}
}
function notifyNotificationCreate(title, opt) {
ipcRenderer.send('notification', title, opt);
}
function notifyNotificationClick() {
ipcRenderer.send('notification-click');
}
setNotificationCallback(notifyNotificationCreate, notifyNotificationClick);
ipcRenderer.on('params', (event, message) => {
log.debug('ipcRenderer.params', { event, message });
const appArgs = JSON.parse(message);
log.info('nativefier.json', appArgs);
});
ipcRenderer.on('debug', (event, message) => {
log.debug('ipcRenderer.debug', { event, message });
});