electron-preload-link
v0.0.2
Published
Communicate between electron preload scripts and context-isolated processes.
Downloads
2
Readme
electron-preload-link
A simple utility to send and listen for messages via the window.postMessage
API. Useful for communication between electron preload scripts and sandboxed + context isolated child processes.
Example:
Child Process
const preloadLink = require('electron-preload-link')
preloadLink.on('shutdown-imminent', (timeRemaining) => {
window.alert(
`You have ${timeRemaining}ms before this process is shut down.`
)
})
function handleSuccess(clipboardContents) {
window.alert(`The contents of your clipboard are: ${clipboardContents}`)
preloadLink.off('clipboard-request-fail', handleFail)
}
function handleFail(err) {
console.error(err)
window.alert('Failed to get clipboard contents!')
preloadLink.off('clipboard-request-success', handleSuccess)
}
preloadLink.once('clipboard-request-success', handleSuccess)
preloadLink.once('clipboard-request-fail', handleFail)
preloadLink.emit('clipboard-request')
Preload Script
const preloadLink = require('electron-preload-link')
const { clipboard } = require('electron')
preloadLink.emit('shutdown-imminent', 60 * 1000)
preloadLink.on('clipboard-request', () => {
try {
const clipboardContent = clipboard.readText()
preloadLink.emit('clipboard-request-success', clipboardContent)
} catch (err) {
preloadLink.emit('clipboard-request-fail', err)
}
})