2
2
mirror of https://github.com/Llewellynvdm/nativefier.git synced 2024-06-02 21:00:48 +00:00
nativefier/app/src/static/preload.js

87 lines
2.3 KiB
JavaScript
Raw Normal View History

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