Nativefier/app/src/static/preload.js

77 lines
1.9 KiB
JavaScript
Raw Normal View History

2015-07-05 08:08:13 +02:00
/**
Preload file that will be executed in the renderer process
2015-07-05 08:08:13 +02:00
*/
2016-05-26 10:51:07 +02:00
import {ipcRenderer, webFrame} from 'electron';
2016-02-25 07:11:48 +01:00
import path from 'path';
import fs from 'fs';
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');
2016-01-29 15:04:41 +01:00
setNotificationCallback((title, opt) => {
ipcRenderer.send('notification', title, opt);
});
document.addEventListener('DOMContentLoaded', () => {
2016-01-23 06:32:20 +01:00
// do things
2016-01-29 15:04:41 +01:00
window.addEventListener('contextmenu', event => {
event.preventDefault();
const targetElement = event.srcElement;
const targetHref = targetElement.href;
if (!targetHref) {
2016-01-29 15:04:41 +01:00
ipcRenderer.once('contextMenuClosed', () => {
clickSelector(event.target);
2016-01-29 15:04:41 +01:00
ipcRenderer.send('cancelNewWindowOverride');
});
}
2016-01-29 15:04:41 +01:00
ipcRenderer.send('contextMenuOpened', targetHref);
}, false);
2016-02-25 07:11:48 +01:00
injectScripts();
});
2016-01-29 15:04:41 +01:00
ipcRenderer.on('params', (event, message) => {
const appArgs = JSON.parse(message);
console.log('nativefier.json', appArgs);
});
2016-01-29 15:04:41 +01:00
ipcRenderer.on('change-zoom', (event, message) => {
webFrame.setZoomFactor(message);
});
2016-01-23 06:32:20 +01:00
/**
* Patches window.Notification to set a callback on a new Notification
* @param callback
*/
function setNotificationCallback(callback) {
2016-01-29 15:04:41 +01:00
const OldNotify = window.Notification;
const newNotify = (title, opt) => {
2016-01-23 06:32:20 +01:00
callback(title, opt);
2016-01-23 19:02:23 +01:00
return new OldNotify(title, opt);
2016-01-23 06:32:20 +01:00
};
2016-01-23 19:02:23 +01:00
newNotify.requestPermission = OldNotify.requestPermission.bind(OldNotify);
2016-01-23 06:32:20 +01:00
Object.defineProperty(newNotify, 'permission', {
2016-01-29 15:04:41 +01:00
get: () => {
2016-01-23 19:02:23 +01:00
return OldNotify.permission;
2016-01-23 06:32:20 +01:00
}
});
2016-01-23 06:32:20 +01:00
window.Notification = newNotify;
}
function clickSelector(element) {
const mouseEvent = new MouseEvent('click');
element.dispatchEvent(mouseEvent);
}
2016-02-25 07:11:48 +01:00
function injectScripts() {
const needToInject = fs.existsSync(INJECT_JS_PATH);
if (!needToInject) {
return;
}
require(INJECT_JS_PATH);
}