Created
February 1, 2017 03:03
-
-
Save ccnokes/6cde9022cef33106f7360af8f671a6c1 to your computer and use it in GitHub Desktop.
Electron preload script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// in preload scripts, we have access to node.js and electron APIs | |
// the remote web app will not have access, so this is safe | |
const { ipcRenderer: ipc, remote } = require('electron'); | |
init(); | |
function init() { | |
// Expose a bridging API to by setting an global on `window`. | |
// We'll add methods to it here first, and when the remote web app loads, | |
// it'll add some additional methods as well. | |
// | |
// !CAREFUL! do not expose any functionality or APIs that could compromise the | |
// user's computer. E.g. don't directly expose core Electron (even IPC) or node.js modules. | |
window.Bridge = { | |
setDockBadge: setDockBadge | |
}; | |
// we get this message from the main process | |
ipc.on('markAllComplete', () => { | |
// the todo app defines this function | |
window.Bridge.markAllComplete(); | |
}); | |
} | |
// the todo app calls this when the todo count changes | |
function setDockBadge(count) { | |
if(process.platform === 'darwin') { | |
//Coerce count into a string. Passing an empty string makes the badge disappear. | |
remote.app.dock.setBadge('' + (count || '')); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment