-
Notifications
You must be signed in to change notification settings - Fork 379
/
Copy pathboosthub-preload.js
127 lines (111 loc) · 3.52 KB
/
boosthub-preload.js
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/* eslint-disable @typescript-eslint/no-var-requires */
const { ipcRenderer, remote } = require('electron')
const { parse } = require('url')
function sendToHost(channel, ...args) {
ipcRenderer.sendToHost(channel, ...args)
}
function addHostListener(channel, listener) {
ipcRenderer.addListener(channel, listener)
}
function addHostListenerOnce(channel, listener) {
ipcRenderer.once(channel, listener)
}
function removeHostListener(channel, listener) {
ipcRenderer.removeListener(channel, listener)
}
function removeAllHostListeners(channel) {
ipcRenderer.removeAllListeners(channel)
}
function openInBrowser(url) {
const { protocol } = parse(url)
switch (protocol) {
case 'http:':
case 'https:':
remote.shell.openExternal(url)
return
default:
console.warn(`${protocol} protocol is not supported`)
}
}
function convertHtmlStringToPdfBlob(htmlString, printOptions) {
return new Promise((resolve, reject) => {
const encodedStr = encodeURIComponent(htmlString)
const { BrowserWindow } = remote
const windowOptions = {
webPreferences: {
nodeIntegration: false,
webSecurity: false,
javascript: false,
},
show: false,
}
const browserWindow = new BrowserWindow(windowOptions)
browserWindow.loadURL('data:text/html;charset=UTF-8,' + encodedStr)
browserWindow.webContents.on('did-finish-load', async () => {
try {
const pdfFileBuffer = await browserWindow.webContents.printToPDF(
printOptions
)
const blob = new Blob([pdfFileBuffer], {
type: 'application/pdf',
})
resolve(blob)
} catch (error) {
reject(error)
} finally {
browserWindow.destroy()
}
})
})
}
function findInPage(text, options) {
const webContents = remote.getCurrentWebContents()
if (webContents) {
return webContents.findInPage(text, options)
}
}
function stopFindInPage(action) {
const webContents = remote.getCurrentWebContents()
if (webContents) {
return webContents.stopFindInPage(action)
}
}
function foundInPageListener(callback) {
return (_, result) => {
callback(result.matches)
}
}
function addFoundInPageListener(callback) {
const webContents = remote.getCurrentWebContents()
if (webContents != null) {
webContents.on('found-in-page', foundInPageListener(callback))
}
}
function removeFoundInPageListener(callback) {
const webContents = remote.getCurrentWebContents()
if (webContents) {
return webContents.removeListener(
'found-in-page',
foundInPageListener(callback)
)
}
}
window.__ELECTRON_ONLY__ = {}
window.__ELECTRON_ONLY__.sendToHost = sendToHost
window.__ELECTRON_ONLY__.convertHtmlStringToPdfBlob = convertHtmlStringToPdfBlob
window.__ELECTRON_ONLY__.addHostListener = addHostListener
window.__ELECTRON_ONLY__.removeHostListener = removeHostListener
window.__ELECTRON_ONLY__.removeAllHostListeners = removeAllHostListeners
window.__ELECTRON_ONLY__.addHostListenerOnce = addHostListenerOnce
window.__ELECTRON_ONLY__.openInBrowser = openInBrowser
window.__ELECTRON_ONLY__.findInPage = findInPage
window.__ELECTRON_ONLY__.stopFindInPage = stopFindInPage
window.__ELECTRON_ONLY__.addFoundInPageListener = addFoundInPageListener
window.__ELECTRON_ONLY__.removeFoundInPageListener = removeFoundInPageListener
const handler = (event) => {
event.preventDefault()
event.stopPropagation()
sendToHost('open-context-menu')
}
window.addEventListener('contextmenu', handler)
window.__ELECTRON_ONLY__.globalContextMenuIsConfigured = true