2
2
mirror of https://github.com/Llewellynvdm/nativefier.git synced 2024-11-11 15:51:06 +00:00

Implement right click context menu for regular href links

This commit is contained in:
Jia Hao 2016-01-25 15:56:33 +08:00
parent dfd0f83b63
commit 949fce5e3c
3 changed files with 54 additions and 3 deletions

View File

@ -0,0 +1,36 @@
import electron from 'electron';
const Menu = electron.Menu;
const ipcMain = electron.ipcMain;
const shell = electron.shell;
const BrowserWindow = electron.BrowserWindow;
function initContextMenu(mainWindow, sendMessage) {
ipcMain.on('contextMenuOpened', function(event, targetHref) {
const contextMenuTemplate = [
{
label: 'Open in default browser',
click: function() {
if (!targetHref) {
return;
}
shell.openExternal(targetHref);
}
},
{
label: 'Open in new window',
click: function() {
if (!targetHref) {
return;
}
new BrowserWindow().loadURL(targetHref);
}
}
];
const contextMenu = Menu.buildFromTemplate(contextMenuTemplate);
contextMenu.popup(mainWindow);
mainWindow.contextMenuOpen = true;
});
}
export default initContextMenu;

View File

@ -8,6 +8,8 @@ var shell = electron.shell;
var isOSX = helpers.isOSX; var isOSX = helpers.isOSX;
var linkIsInternal = helpers.linkIsInternal; var linkIsInternal = helpers.linkIsInternal;
import initContextMenu from './../contextMenu/contextMenu';
const ZOOM_INTERVAL = 0.1; const ZOOM_INTERVAL = 0.1;
/** /**
@ -54,6 +56,7 @@ function createMainWindow(options, onAppQuit, setDockBadge) {
}; };
createMenu(options.nativefierVersion, onAppQuit, mainWindow.webContents.goBack, mainWindow.webContents.goForward, onZoomIn, onZoomOut); createMenu(options.nativefierVersion, onAppQuit, mainWindow.webContents.goBack, mainWindow.webContents.goForward, onZoomIn, onZoomOut);
initContextMenu(mainWindow);
if (options.userAgent) { if (options.userAgent) {
mainWindow.webContents.setUserAgent(options.userAgent); mainWindow.webContents.setUserAgent(options.userAgent);
@ -65,7 +68,6 @@ function createMainWindow(options, onAppQuit, setDockBadge) {
if (options.counter) { if (options.counter) {
mainWindow.on('page-title-updated', function() { mainWindow.on('page-title-updated', function() {
if (mainWindow.isFocused()) { if (mainWindow.isFocused()) {
return; return;
} }

View File

@ -1,8 +1,7 @@
/** /**
Preload file that will be executed in the renderer process Preload file that will be executed in the renderer process
*/ */
import electron from 'electron';
var electron = require('electron');
var ipc = electron.ipcRenderer; var ipc = electron.ipcRenderer;
var webFrame = electron.webFrame; var webFrame = electron.webFrame;
@ -12,6 +11,15 @@ setNotificationCallback(function(title, opt) {
document.addEventListener('DOMContentLoaded', function(event) { document.addEventListener('DOMContentLoaded', function(event) {
// do things // do things
window.addEventListener('contextmenu', function(event) {
event.preventDefault();
const targetElement = event.srcElement;
const targetHref = targetElement.href;
ipc.send('contextMenuOpened', targetHref);
}, false);
}); });
ipc.on('params', function(event, message) { ipc.on('params', function(event, message) {
@ -43,3 +51,8 @@ function setNotificationCallback(callback) {
window.Notification = newNotify; window.Notification = newNotify;
} }
function clickSelector(element) {
const mouseEvent = new MouseEvent('click');
element.dispatchEvent(mouseEvent);
}