Skip to content

Instantly share code, notes, and snippets.

@ccnokes
Created February 1, 2017 03:03
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ccnokes/6cde9022cef33106f7360af8f671a6c1 to your computer and use it in GitHub Desktop.
Save ccnokes/6cde9022cef33106f7360af8f671a6c1 to your computer and use it in GitHub Desktop.
Electron preload script
// 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