2015-07-05 08:08:13 +02:00
|
|
|
/**
|
2016-01-22 12:57:39 +01:00
|
|
|
Preload file that will be executed in the renderer process
|
2015-07-05 08:08:13 +02:00
|
|
|
*/
|
2019-01-02 05:38:45 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Note: This needs to be attached prior to the imports, as the they will delay
|
|
|
|
* the attachment till after the event has been raised.
|
|
|
|
*/
|
|
|
|
document.addEventListener('DOMContentLoaded', () => {
|
|
|
|
// Due to the early attachment, this triggers a linter error
|
|
|
|
// because it's not yet been defined.
|
|
|
|
// eslint-disable-next-line no-use-before-define
|
|
|
|
injectScripts();
|
|
|
|
});
|
|
|
|
|
|
|
|
// Disable imports being first due to the above event attachment
|
|
|
|
import { ipcRenderer } from 'electron'; // eslint-disable-line import/first
|
|
|
|
import path from 'path'; // eslint-disable-line import/first
|
|
|
|
import fs from 'fs'; // eslint-disable-line import/first
|
2015-07-05 08:08:13 +02:00
|
|
|
|
2016-02-25 07:11:48 +01:00
|
|
|
const INJECT_JS_PATH = path.join(__dirname, '../../', 'inject/inject.js');
|
2017-12-19 14:42:06 +01:00
|
|
|
const log = require('loglevel');
|
2017-04-29 16:52:12 +02:00
|
|
|
/**
|
2018-08-22 22:06:44 +02:00
|
|
|
* Patches window.Notification to:
|
|
|
|
* - set a callback on a new Notification
|
|
|
|
* - set a callback for clicks on notifications
|
|
|
|
* @param createCallback
|
|
|
|
* @param clickCallback
|
2017-04-29 16:52:12 +02:00
|
|
|
*/
|
2018-08-22 22:06:44 +02:00
|
|
|
function setNotificationCallback(createCallback, clickCallback) {
|
2017-04-29 16:52:12 +02:00
|
|
|
const OldNotify = window.Notification;
|
|
|
|
const newNotify = (title, opt) => {
|
2018-08-22 22:06:44 +02:00
|
|
|
createCallback(title, opt);
|
|
|
|
const instance = new OldNotify(title, opt);
|
|
|
|
instance.addEventListener('click', clickCallback);
|
|
|
|
return instance;
|
2017-04-29 16:52:12 +02:00
|
|
|
};
|
|
|
|
newNotify.requestPermission = OldNotify.requestPermission.bind(OldNotify);
|
|
|
|
Object.defineProperty(newNotify, 'permission', {
|
|
|
|
get: () => OldNotify.permission,
|
|
|
|
});
|
|
|
|
|
|
|
|
window.Notification = newNotify;
|
|
|
|
}
|
|
|
|
|
|
|
|
function injectScripts() {
|
|
|
|
const needToInject = fs.existsSync(INJECT_JS_PATH);
|
|
|
|
if (!needToInject) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Dynamically require scripts
|
|
|
|
// eslint-disable-next-line global-require, import/no-dynamic-require
|
|
|
|
require(INJECT_JS_PATH);
|
|
|
|
}
|
|
|
|
|
2018-08-22 22:06:44 +02:00
|
|
|
function notifyNotificationCreate(title, opt) {
|
2017-04-29 16:52:12 +02:00
|
|
|
ipcRenderer.send('notification', title, opt);
|
2018-08-22 22:06:44 +02:00
|
|
|
}
|
|
|
|
function notifyNotificationClick() {
|
|
|
|
ipcRenderer.send('notification-click');
|
|
|
|
}
|
|
|
|
|
|
|
|
setNotificationCallback(notifyNotificationCreate, notifyNotificationClick);
|
2016-01-22 19:44:42 +01:00
|
|
|
|
2016-01-29 15:04:41 +01:00
|
|
|
ipcRenderer.on('params', (event, message) => {
|
2017-04-29 16:52:12 +02:00
|
|
|
const appArgs = JSON.parse(message);
|
2017-12-19 14:42:06 +01:00
|
|
|
log.info('nativefier.json', appArgs);
|
2016-01-19 13:26:19 +01:00
|
|
|
});
|
2016-01-22 19:44:42 +01:00
|
|
|
|
2016-06-16 09:28:36 +02:00
|
|
|
ipcRenderer.on('debug', (event, message) => {
|
2017-04-29 16:52:12 +02:00
|
|
|
// eslint-disable-next-line no-console
|
2017-12-19 14:42:06 +01:00
|
|
|
log.info('debug:', message);
|
2016-06-16 09:28:36 +02:00
|
|
|
});
|