forked from kiwix/kiwix-js-pwa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
preload.cjs
76 lines (67 loc) · 2.91 KB
/
preload.cjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// All of the Node.js APIs are available in the preload process.
// It has the same sandbox as a Chrome extension.
'use strict';
// A regular expression that matches the hash of the Kiwix publisher on the Microsoft Store (CN=0A5438F5-EEA6-4300-9B77-E45BBD148885)
// If the app is installed from the Store rather than from the signed GitHub release, we need to disable update checking
const regexpInstalledFromMicrosoftStore = /_mc3511b08yc0e/;
console.log('[Preload] App directory: ' + __dirname);
console.log('[Preload] Is app installed from Microsoft Store? ' + (process.windowsStore && regexpInstalledFromMicrosoftStore.test(__dirname) ? 'Yes' : 'No'));
console.log('[Preload] Window location: ' + window.location.pathname + '\nStore publisher hash: ' + regexpInstalledFromMicrosoftStore);
// DEV: TO SUPPORT ELECTRON ^12 YOU WILL NEED THIS
const { ipcRenderer, contextBridge, webFrame } = require('electron');
const { open, read, close, stat, readdir } = require('fs');
console.log('Inserting required Electron functions into DOM...');
// DEV: FOR ELECTRON ^12 DO IT THIS WAY:
contextBridge.exposeInMainWorld('fs', {
open: open,
read: read,
readdir: readdir,
close: close,
stat: stat
});
// Exposed events and Event callback for electronAPI (you can add events to listen to, so long as main.js sends a message with name of the event)
contextBridge.exposeInMainWorld('electronAPI', {
checkForUpdates: function () {
ipcRenderer.send('check-updates');
},
setStoreValue: function (key, value) {
ipcRenderer.send('set-store-value', key, value);
},
getStoreValue: function (key) {
ipcRenderer.send('get-store-value', key);
},
openExternal: function (url) {
ipcRenderer.send('open-external', url);
},
setZoomLimits: function (min, max) {
console.log('Setting zoom limits to ' + min + ' and ' + max);
webFrame.setVisualZoomLevelLimits(min, max);
},
isMicrosoftStoreApp: process.windowsStore && regexpInstalledFromMicrosoftStore.test(__dirname),
__dirname: __dirname,
on: function (event, callback) {
ipcRenderer.on(event, function (_, data1, data2) {
callback(data1, data2);
});
}
});
// Adapted from: https://stackoverflow.com/questions/69717365/using-electron-save-dialog-in-renderer-with-context-isolation
contextBridge.exposeInMainWorld('dialog', {
openFile: function () {
ipcRenderer.send('file-dialog'); // adjust naming for your project
},
openDirectory: function () {
ipcRenderer.send('dir-dialog'); // adjust naming for your project
},
// Provide an easier way to listen to events
on: function (channel, callback) {
ipcRenderer.on(channel, function (_, data) {
callback(data);
});
}
});
// window.Buffer = Buffer;
// console.log(win.session.cookies);
// win.session.cookies.get({}, (error, cookies) => {
// console.log(cookies);
// });