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
|
|
|
*/
|
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);
|
2016-01-22 19:44:42 +01:00
|
|
|
});
|
|
|
|
|
2016-02-27 17:49:54 +01:00
|
|
|
document.addEventListener('DOMContentLoaded', () => {
|
2016-01-23 06:32:20 +01:00
|
|
|
// do things
|
2016-01-25 08:56:33 +01:00
|
|
|
|
2016-01-29 15:04:41 +01:00
|
|
|
window.addEventListener('contextmenu', event => {
|
2016-01-25 08:56:33 +01:00
|
|
|
event.preventDefault();
|
2016-10-09 08:16:44 +02:00
|
|
|
let targetElement = event.srcElement;
|
2016-10-09 13:05:43 +02:00
|
|
|
|
2016-09-29 20:10:28 +02:00
|
|
|
// the clicked element is the deepest in the DOM, and may not be the <a> bearing the href
|
|
|
|
// for example, <a href="..."><span>Google</span></a>
|
|
|
|
while (!targetElement.href && targetElement.parentElement) {
|
|
|
|
targetElement = targetElement.parentElement;
|
|
|
|
}
|
2016-01-25 08:56:33 +01:00
|
|
|
const targetHref = targetElement.href;
|
|
|
|
|
2016-01-25 09:49:11 +01:00
|
|
|
if (!targetHref) {
|
2016-01-29 15:04:41 +01:00
|
|
|
ipcRenderer.once('contextMenuClosed', () => {
|
2016-01-25 09:49:11 +01:00
|
|
|
clickSelector(event.target);
|
2016-01-29 15:04:41 +01:00
|
|
|
ipcRenderer.send('cancelNewWindowOverride');
|
2016-01-25 09:49:11 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-01-29 15:04:41 +01:00
|
|
|
ipcRenderer.send('contextMenuOpened', targetHref);
|
2016-01-25 08:56:33 +01:00
|
|
|
}, false);
|
|
|
|
|
2016-02-25 07:11:48 +01:00
|
|
|
injectScripts();
|
2016-01-21 18:32:21 +01:00
|
|
|
});
|
2016-01-19 13:26:19 +01:00
|
|
|
|
2016-01-29 15:04:41 +01:00
|
|
|
ipcRenderer.on('params', (event, message) => {
|
|
|
|
const appArgs = JSON.parse(message);
|
2016-01-22 12:57:39 +01:00
|
|
|
console.log('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) => {
|
|
|
|
console.log('debug:', message);
|
|
|
|
});
|
|
|
|
|
2016-01-29 15:04:41 +01:00
|
|
|
ipcRenderer.on('change-zoom', (event, message) => {
|
2016-01-23 02:27:09 +01:00
|
|
|
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-22 19:44:42 +01:00
|
|
|
|
2016-01-23 06:32:20 +01:00
|
|
|
window.Notification = newNotify;
|
2016-01-22 19:44:42 +01:00
|
|
|
}
|
2016-01-25 08:56:33 +01:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|