2
2
mirror of https://github.com/Llewellynvdm/nativefier.git synced 2024-06-27 15:43:58 +00:00
nativefier/app/src/static/preload.js

66 lines
1.7 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
*/
import electron from 'electron';
var ipc = electron.ipcRenderer;
var webFrame = electron.webFrame;
2015-07-05 06:08:13 +00:00
2016-01-23 18:02:23 +00:00
setNotificationCallback(function(title, opt) {
2016-01-23 05:32:20 +00:00
ipc.send('notification', title, opt);
});
2016-01-23 18:02:23 +00:00
document.addEventListener('DOMContentLoaded', function(event) {
2016-01-23 05:32:20 +00:00
// do things
window.addEventListener('contextmenu', function(event) {
event.preventDefault();
const targetElement = event.srcElement;
const targetHref = targetElement.href;
if (!targetHref) {
ipc.once('contextMenuClosed', () => {
clickSelector(event.target);
ipc.send('cancelNewWindowOverride');
});
}
ipc.send('contextMenuOpened', targetHref);
}, false);
});
2016-01-23 18:02:23 +00:00
ipc.on('params', function(event, message) {
2015-07-05 06:08:13 +00:00
var appArgs = JSON.parse(message);
console.log('nativefier.json', appArgs);
});
2016-01-23 18:02:23 +00:00
ipc.on('change-zoom', function(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-23 18:02:23 +00:00
var OldNotify = window.Notification;
var newNotify = function(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-23 18:02:23 +00:00
get: function() {
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);
}