-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpreload.ts
More file actions
85 lines (73 loc) · 3.18 KB
/
preload.ts
File metadata and controls
85 lines (73 loc) · 3.18 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
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Pic Display - A cute image viewer ❤ *
* Copyright © 2026 Moebytes <moebytes.com> *
* Licensed under CC BY-NC 4.0. See license.txt for details. *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
import {contextBridge, ipcRenderer, webUtils, IpcRendererEvent} from "electron"
type SystemPath = "home" | "appData" | "userData" | "temp" | "exe" | "module"
| "desktop" | "documents" | "downloads" | "music" | "pictures" | "videos" | "recent"
| "logs" | "crashDumps"
declare global {
interface Window {
platform: "mac" | "windows",
ipcRenderer: {
invoke: (channel: string, ...args: any[]) => Promise<any>
send: (channel: string, ...args: any[]) => void
on: (channel: string, listener: (...args: any[]) => void) => any
removeListener: (channel: string, listener: (...args: any[]) => void) => void
},
clipboard: {
readText: () => Promise<string>
writeText: (text: string) => Promise<void>
clear: () => Promise<void>
readImage: () => Promise<string>
writeImage: (image: string) => Promise<void>
},
app: {
getPath: (location: SystemPath) => Promise<string>
},
webUtils: {
getPathForFile: (file: File) => string
},
path: {
basename: (filepath: string, suffix?: string) => Promise<string>
extname: (filepath: string) => Promise<string>
normalize: (filepath: string) => Promise<string>
}
}
}
contextBridge.exposeInMainWorld("ipcRenderer", {
invoke: async (channel: string, ...args: any[]) => {
return ipcRenderer.invoke(channel, ...args)
},
send: (channel: string, ...args: any[]) => {
ipcRenderer.send(channel, ...args)
},
on: (channel: string, listener: (...args: any[]) => void) => {
const subscription = (event: IpcRendererEvent, ...args: any[]) => listener(event, ...args)
ipcRenderer.on(channel, subscription)
return subscription
},
removeListener: (channel: string, listener: (...args: any[]) => void) => {
ipcRenderer.removeListener(channel, listener)
}
})
contextBridge.exposeInMainWorld("clipboard", {
readText: () => ipcRenderer.invoke("clipboard:readText"),
writeText: (text: string) => ipcRenderer.invoke("clipboard:writeText", text),
clear: () => ipcRenderer.invoke("clipboard:clear"),
readImage: () => ipcRenderer.invoke("clipboard:readImage"),
writeImage: (image: string) => ipcRenderer.invoke("clipboard:writeImage", image)
})
contextBridge.exposeInMainWorld("app", {
getPath: (location: SystemPath) => ipcRenderer.invoke("app:getPath", location)
})
contextBridge.exposeInMainWorld("webUtils", {
getPathForFile: (file: File) => webUtils.getPathForFile(file)
})
contextBridge.exposeInMainWorld("path", {
basename: (filepath: string, suffix?: string) => ipcRenderer.invoke("path:basename", filepath, suffix),
extname: (filepath: string) => ipcRenderer.invoke("path:extname", filepath),
normalize: (filepath: string) => ipcRenderer.invoke("path:normalize", filepath)
})
contextBridge.exposeInMainWorld("platform", process.platform === "darwin" ? "mac" : "windows")