Electron

window.webContents.on('new-window', function(e, url) {
e.preventDefault()
require('electron').shell.openExternal(url)
})

Change Menu Labels #

Rebuild the menu when it changes. Not recommended.

contextMenu.items[0].label = 'New Label'
contextMenu = Menu.buildFromTemplate(contextMenu.items)

// show on click after
tray.popUpContextMenu(contextMenu)

Show Context Menu on Right Click #

const contextMenu = Menu.buildFromTemplate(...)

tray.on('right-click', function (event) {
tray.popUpContextMenu(contextMenu)
})

Store User Data #

Adapted from How to store user data in Electron article.

const { app } = require('electron');
const path = require('path');
const fs = require('fs');

const DEFAULTS = {
enabled: true,
}

class Store {
constructor() {
const userDataPath = (app || remote.app).getPath('userData');
this.path = path.join(userDataPath, 'prefs.json');
this.data = parseDataFile(this.path);
}

get(key) {
return this.data[key];
}

set(key, val) {
this.data[key] = val;
fs.writeFileSync(this.path, JSON.stringify(this.data));
}
}

function parseDataFile(filePath, defaults) {
try {
return JSON.parse(fs.readFileSync(filePath));
} catch (error) {
return DEFAULTS;
}
}

module.exports = Store;