-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathpreload.js
More file actions
174 lines (158 loc) · 4.63 KB
/
preload.js
File metadata and controls
174 lines (158 loc) · 4.63 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import { contextBridge, ipcRenderer } from "electron"
// --------- Expose some API to the Renderer process ---------
// Whitelist of allowed IPC channels for security
const ALLOWED_INVOKE_CHANNELS = [
"fla:open-file",
"fla:get-recent-logs",
"fla:clear-recent-logs",
"missions:get-save-mission-file-path",
"app:get-node-env",
"app:get-version",
"app:is-mac",
"settings:fetch-settings",
"settings:save-settings",
"app:open-webcam-window",
"app:close-webcam-window",
"app:open-about-window",
"app:close-about-window",
"app:open-link-stats-window",
"app:close-link-stats-window",
"app:update-link-stats",
]
const ALLOWED_SEND_CHANNELS = [
"window:close",
"window:minimise",
"window:maximise",
"window:reload",
"window:force-reload",
"window:toggle-developer-tools",
"window:actual-size",
"window:toggle-fullscreen",
"window:zoom-in",
"window:zoom-out",
"window:open-file-in-explorer",
]
const ALLOWED_ON_CHANNELS = [
"main-process-message",
"app:webcam-closed",
"app:send-link-stats",
"fla:log-parse-progress",
]
contextBridge.exposeInMainWorld("ipcRenderer", {
// Secure invoke method - only allows whitelisted channels
invoke: (channel, ...args) => {
if (ALLOWED_INVOKE_CHANNELS.includes(channel)) {
return ipcRenderer.invoke(channel, ...args)
}
throw new Error(`IPC invoke channel '${channel}' is not allowed`)
},
// Secure send method - only allows whitelisted channels
send: (channel, ...args) => {
if (ALLOWED_SEND_CHANNELS.includes(channel)) {
return ipcRenderer.send(channel, ...args)
}
throw new Error(`IPC send channel '${channel}' is not allowed`)
},
// Secure on method - only allows whitelisted channels
on: (channel, callback) => {
if (ALLOWED_ON_CHANNELS.includes(channel)) {
return ipcRenderer.on(channel, callback)
}
throw new Error(`IPC on channel '${channel}' is not allowed`)
},
// Secure removeAllListeners - only for whitelisted channels
removeAllListeners: (channel) => {
if (ALLOWED_ON_CHANNELS.includes(channel)) {
return ipcRenderer.removeAllListeners(channel)
}
throw new Error(
`IPC removeAllListeners channel '${channel}' is not allowed`,
)
},
})
// --------- Preload scripts loading ---------
function domReady(condition = ["complete", "interactive"]) {
return new Promise((resolve) => {
if (condition.includes(document.readyState)) {
resolve(true)
} else {
document.addEventListener("readystatechange", () => {
if (condition.includes(document.readyState)) {
resolve(true)
}
})
}
})
}
const safeDOM = {
append(parent, child) {
if (!Array.from(parent.children).find((e) => e === child)) {
parent.appendChild(child)
}
},
remove(parent, child) {
if (Array.from(parent.children).find((e) => e === child)) {
parent.removeChild(child)
}
},
}
/**
* https://tobiasahlin.com/spinkit
* https://connoratherton.com/loaders
* https://projects.lukehaas.me/css-loaders
* https://matejkustec.github.io/SpinThatShit
*/
function useLoading() {
const className = `loaders-css__square-spin`
const styleContent = `
@keyframes square-spin {
25% { transform: perspective(100px) rotateX(180deg) rotateY(0); }
50% { transform: perspective(100px) rotateX(180deg) rotateY(180deg); }
75% { transform: perspective(100px) rotateX(0) rotateY(180deg); }
100% { transform: perspective(100px) rotateX(0) rotateY(0); }
}
.${className} > div {
animation-fill-mode: both;
width: 50px;
height: 50px;
background: #fff;
animation: square-spin 3s 0s cubic-bezier(0.09, 0.57, 0.49, 0.9) infinite;
}
.app-loading-wrap {
position: fixed;
top: 0;
left: 0;
width: 100vw;
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
background: #282c34;
z-index: 9;
}
`
const oStyle = document.createElement("style")
const oDiv = document.createElement("div")
oStyle.id = "app-loading-style"
oStyle.innerHTML = styleContent
oDiv.className = "app-loading-wrap"
oDiv.innerHTML = `<div class="${className}"><div></div></div>`
return {
appendLoading() {
safeDOM.append(document.head, oStyle)
safeDOM.append(document.body, oDiv)
},
removeLoading() {
safeDOM.remove(document.head, oStyle)
safeDOM.remove(document.body, oDiv)
},
}
}
// ----------------------------------------------------------------------
// eslint-disable-next-line react-hooks/rules-of-hooks
const { appendLoading, removeLoading } = useLoading()
domReady().then(appendLoading)
window.onmessage = (ev) => {
ev.data.payload === "removeLoading" && removeLoading()
}
setTimeout(removeLoading, 4999)