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 linkIsInternal = helpers.linkIsInternal;
import initContextMenu from './../contextMenu/contextMenu';
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);
initContextMenu(mainWindow);
if (options.userAgent) {
mainWindow.webContents.setUserAgent(options.userAgent);
@ -65,7 +68,6 @@ function createMainWindow(options, onAppQuit, setDockBadge) {
if (options.counter) {
mainWindow.on('page-title-updated', function() {
if (mainWindow.isFocused()) {
return;
}

View File

@ -1,8 +1,7 @@
/**
Preload file that will be executed in the renderer process
*/
var electron = require('electron');
import electron from 'electron';
var ipc = electron.ipcRenderer;
var webFrame = electron.webFrame;
@ -12,6 +11,15 @@ setNotificationCallback(function(title, opt) {
document.addEventListener('DOMContentLoaded', function(event) {
// 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) {
@ -43,3 +51,8 @@ function setNotificationCallback(callback) {
window.Notification = newNotify;
}
function clickSelector(element) {
const mouseEvent = new MouseEvent('click');
element.dispatchEvent(mouseEvent);
}