2018-09-21 19:20:06 +02:00
|
|
|
// In order to give access to the webview to certain functions of the main process, we need
|
|
|
|
// this bridge which listens from the main process and sends to the webview and the other
|
|
|
|
// way around. This is necessary after having enabled the "contextIsolation" option, which
|
|
|
|
// prevents the webview from accessing low-level methods in the main process.
|
2018-06-22 20:18:15 +02:00
|
|
|
|
2018-09-21 19:20:06 +02:00
|
|
|
const ipcRenderer = require('electron').ipcRenderer;
|
|
|
|
|
2018-11-08 00:37:13 +02:00
|
|
|
ipcRenderer.on('setHtml', (event, html, options) => {
|
|
|
|
window.postMessage({ target: 'webview', name: 'setHtml', data: { html: html, options: options } }, '*');
|
2018-09-21 19:20:06 +02:00
|
|
|
});
|
|
|
|
|
2019-09-09 19:16:00 +02:00
|
|
|
ipcRenderer.on('scrollToHash', (event, hash) => {
|
|
|
|
window.postMessage({ target: 'webview', name: 'scrollToHash', data: { hash: hash } }, '*');
|
|
|
|
});
|
|
|
|
|
2018-09-21 19:20:06 +02:00
|
|
|
ipcRenderer.on('setPercentScroll', (event, percent) => {
|
|
|
|
window.postMessage({ target: 'webview', name: 'setPercentScroll', data: { percent: percent } }, '*');
|
|
|
|
});
|
|
|
|
|
2018-12-09 02:18:10 +02:00
|
|
|
ipcRenderer.on('setMarkers', (event, keywords, options) => {
|
|
|
|
window.postMessage({ target: 'webview', name: 'setMarkers', data: { keywords: keywords, options: options } }, '*');
|
2018-09-21 19:20:06 +02:00
|
|
|
});
|
|
|
|
|
2020-05-21 10:14:33 +02:00
|
|
|
window.addEventListener('message', event => {
|
2018-09-21 19:20:06 +02:00
|
|
|
// Here we only deal with messages that are sent from the webview to the main Electron process
|
|
|
|
if (!event.data || event.data.target !== 'main') return;
|
|
|
|
|
|
|
|
const callName = event.data.name;
|
|
|
|
const args = event.data.args;
|
|
|
|
|
|
|
|
if (args.length === 0) {
|
|
|
|
ipcRenderer.sendToHost(callName);
|
|
|
|
} else if (args.length === 1) {
|
|
|
|
ipcRenderer.sendToHost(callName, args[0]);
|
|
|
|
} else if (args.length === 2) {
|
|
|
|
ipcRenderer.sendToHost(callName, args[1]);
|
|
|
|
} else {
|
|
|
|
throw new Error('Unsupported number of args');
|
|
|
|
}
|
2018-11-08 00:37:13 +02:00
|
|
|
});
|