Add contextmenu to Electron app

Former-commit-id: 48c745f3fc3c5f57536b0424db103910759f3645 [formerly 70a7eae3da801be3f77df32df1a6fd82b0466557 [formerly c17a331bd6b219e787d66773a30150469cf9bb29]]
Former-commit-id: 6f5442d908bfb8cb6d9d44052ca5a4c079209775
Former-commit-id: da98de6b149ab6a85feb7439d19a472ca01c715f
This commit is contained in:
Jaifroid 2021-09-05 23:56:37 +01:00
parent 8b76a3aaae
commit be39e18a9e
2 changed files with 166 additions and 138 deletions

299
main.js
View File

@ -1,136 +1,165 @@
// Modules to control application life and create native browser window // Modules to control application life and create native browser window
const { const {
app, app,
protocol, protocol,
BrowserWindow, BrowserWindow,
shell shell
} = require('electron'); } = require('electron');
const path = require('path'); const path = require('path');
// This is used to set capabilities of the app: protocol in onready event below const contextMenu = require('electron-context-menu');
// protocol.registerSchemesAsPrivileged([{
// scheme: 'app', contextMenu({
// privileges: { labels: {
// standard: true, cut: 'Cut',
// secure: true, copy: 'Copy',
// allowServiceWorkers: true, paste: 'Paste',
// supportFetchAPI: true save: 'Save Image',
// } saveImageAs: 'Save Image As…',
// }]); copyLink: 'Copy Link',
saveLinkAs: 'Save Link As…',
// Keep a global reference of the window object, if you don't, the window will inspect: 'Inspect Element'
// be closed automatically when the JavaScript object is garbage collected. },
let mainWindow; prepend: () => { },
append: () => { },
function createWindow() { showCopyImageAddress: true,
// Create the browser window. showSaveImageAs: true,
mainWindow = new BrowserWindow({ showInspectElement: true,
titleBarStyle: 'hidden', showSaveLinkAs: true,
width: 1281, cut: true,
height: 800, copy: true,
minWidth: 640, paste: true,
minHeight: 480, save: true,
autoHideMenuBar: true, saveImageAs: true,
icon: path.join(__dirname, 'www/img/icons/kiwix-64.png'), copyLink: true,
webPreferences: { saveLinkAs: true,
nodeIntegration: false inspect: true
// contextIsolation: true, });
, preload: path.join(__dirname, 'preload.js')
, nativeWindowOpen: true // This is used to set capabilities of the app: protocol in onready event below
// , webSecurity: false // protocol.registerSchemesAsPrivileged([{
// , session: ses // scheme: 'app',
// , partition: 'persist:kiwixjs' // privileges: {
} // standard: true,
}); // secure: true,
// allowServiceWorkers: true,
// and load the index.html of the app. // supportFetchAPI: true
// mainWindow.loadURL(`https://${__dirname}/www/index.html`); // }
// mainWindow.loadURL(`https://pwa.kiwix.org/`); // }]);
// DEV: If you need Service Worker more than you need document.cookie, load app like this:
mainWindow.loadFile('www/index.html'); // Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
//mainWindow.autoHideMenuBar = true; let mainWindow;
// mainWindow.webContents.setWindowOpenHandler(({ url }) => { function createWindow() {
// if (!/blob:/i.test(url)) { // Create the browser window.
// return { action: 'allow' }; mainWindow = new BrowserWindow({
// } titleBarStyle: 'hidden',
// return { action: 'deny' }; width: 1281,
// }); height: 800,
minWidth: 640,
// mainWindow.webContents.on('new-window', function(e, url) { minHeight: 480,
// // Make sure blob urls stay in electron perimeter autoHideMenuBar: true,
// if(/^blob:/i.test(url)) { icon: path.join(__dirname, 'www/img/icons/kiwix-64.png'),
// return; webPreferences: {
// } nodeIntegration: false
// // And open every other protocol in the OS browser // contextIsolation: true,
// e.preventDefault(); , preload: path.join(__dirname, 'preload.js')
// shell.openExternal(url); , nativeWindowOpen: true
// }); // , webSecurity: false
// , session: ses
// DEV: Enable code below to check cookies saved by app in console log // , partition: 'persist:kiwixjs'
// mainWindow.webContents.on('did-finish-load', function() { }
// mainWindow.webContents.session.cookies.get({}, (error, cookies) => { });
// console.log(cookies);
// }); // and load the index.html of the app.
// }); // mainWindow.loadURL(`https://${__dirname}/www/index.html`);
// mainWindow.loadURL(`https://pwa.kiwix.org/`);
// Open the DevTools. // DEV: If you need Service Worker more than you need document.cookie, load app like this:
// mainWindow.webContents.openDevTools() mainWindow.loadFile('www/index.html');
// Emitted when the window is closed. //mainWindow.autoHideMenuBar = true;
mainWindow.on('closed', function () {
// Dereference the window object, usually you would store windows // mainWindow.webContents.setWindowOpenHandler(({ url }) => {
// in an array if your app supports multi windows, this is the time // if (!/blob:/i.test(url)) {
// when you should delete the corresponding element. // return { action: 'allow' };
mainWindow = null; // }
}); // return { action: 'deny' };
} // });
// let dirnameParts = __dirname.match(/[^\/\\]+(?:[\/\\]|$)/g); // mainWindow.webContents.on('new-window', function(e, url) {
// // Make sure blob urls stay in electron perimeter
// This method will be called when Electron has finished // if(/^blob:/i.test(url)) {
// initialization and is ready to create browser windows. // return;
// Some APIs can only be used after this event occurs. // }
// // And open every other protocol in the OS browser
app.on('ready', () => { // e.preventDefault();
// //protocol.registerFileProtocol('app', (request, callback) => { // shell.openExternal(url);
// protocol.registerHttpProtocol('app', (request, callback) => { // });
// const url = request.url.replace(/^app:\/\/([^?#]*?)([^?#\/\\]+)([#?].*$|$)/, function(_p0, relPath, linkUrl, hash) {
// let replaceLink = relPath + linkUrl; // DEV: Enable code below to check cookies saved by app in console log
// // This prevents the querystring from being passed to Electron on main app reload // mainWindow.webContents.on('did-finish-load', function() {
// if (/www\/index\.html/.test(replaceLink)) return replaceLink; // mainWindow.webContents.session.cookies.get({}, (error, cookies) => {
// return replaceLink + hash; // console.log(cookies);
// }); // });
// //let returnPath = path.normalize(`${__dirname}/${url}`); // });
// let returnPath = __dirname + '/' + url;
// returnPath = path.normalize(returnPath); // Open the DevTools.
// console.log(returnPath); // mainWindow.webContents.openDevTools()
// callback({
// path: returnPath // Emitted when the window is closed.
// // url: 'file://' + path.normalize(`${__dirname}/${url}`), mainWindow.on('closed', function () {
// // method: 'GET' // Dereference the window object, usually you would store windows
// }); // in an array if your app supports multi windows, this is the time
// // console.log(path.normalize(`${__dirname}/${url}` + ':' + url)); // when you should delete the corresponding element.
// }, (error) => { mainWindow = null;
// if (error) console.error('Failed to register protocol'); });
// }); }
// Create the new window
createWindow(); // let dirnameParts = __dirname.match(/[^\/\\]+(?:[\/\\]|$)/g);
});
// This method will be called when Electron has finished
// Quit when all windows are closed. // initialization and is ready to create browser windows.
app.on('window-all-closed', function () { // Some APIs can only be used after this event occurs.
// On macOS it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q app.on('ready', () => {
if (process.platform !== 'darwin') app.quit(); // //protocol.registerFileProtocol('app', (request, callback) => {
}); // protocol.registerHttpProtocol('app', (request, callback) => {
// const url = request.url.replace(/^app:\/\/([^?#]*?)([^?#\/\\]+)([#?].*$|$)/, function(_p0, relPath, linkUrl, hash) {
app.on('activate', function () { // let replaceLink = relPath + linkUrl;
// On macOS it's common to re-create a window in the app when the // // This prevents the querystring from being passed to Electron on main app reload
// dock icon is clicked and there are no other windows open. // if (/www\/index\.html/.test(replaceLink)) return replaceLink;
if (mainWindow === null) createWindow(); // return replaceLink + hash;
}); // });
// //let returnPath = path.normalize(`${__dirname}/${url}`);
// In this file you can include the rest of your app's specific main process // let returnPath = __dirname + '/' + url;
// returnPath = path.normalize(returnPath);
// console.log(returnPath);
// callback({
// path: returnPath
// // url: 'file://' + path.normalize(`${__dirname}/${url}`),
// // method: 'GET'
// });
// // console.log(path.normalize(`${__dirname}/${url}` + ':' + url));
// }, (error) => {
// if (error) console.error('Failed to register protocol');
// });
// Create the new window
createWindow();
});
// Quit when all windows are closed.
app.on('window-all-closed', function () {
// On macOS it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') app.quit();
});
app.on('activate', function () {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (mainWindow === null) createWindow();
});
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here. // code. You can also put them in separate files and require them here.

View File

@ -86,8 +86,7 @@
"electron-packager": "^15.2.0" "electron-packager": "^15.2.0"
}, },
"dependencies": { "dependencies": {
"@types/fs-extra": "^9.0.11" "@types/fs-extra": "^9.0.11",
"electron-context-menu": "^3.1.1"
} }
} }