-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreload.js
More file actions
154 lines (148 loc) · 3.92 KB
/
Copy pathpreload.js
File metadata and controls
154 lines (148 loc) · 3.92 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
// All of the Node.js APIs are available in the preload process.
// It has the same sandbox as a Chrome extension.
window.addEventListener('DOMContentLoaded', () => {
const replaceText = (selector, text) => {
const element = document.getElementById(selector)
if (element) element.innerText = text
}
for (const dependency of ['chrome', 'node', 'electron']) {
replaceText(`${dependency}-version`, process.versions[dependency])
}
})
const { ipcRenderer, contextBridge } = require('electron');
const { spawn } = require('child_process');
let process_list = []
var kill = require('tree-kill');
contextBridge.exposeInMainWorld('electron', {
notificationApi: {
sendNotification(message) {
console.log(message)
ipcRenderer.send('notify', message);
}
},
appRemoteApi: {
minimizeWindow(){
ipcRenderer.send('minimize')
},
maximizeWindow(){
ipcRenderer.send('maximize')
},
closeWindow(){
ipcRenderer.send('exit')
}
},
shellApi: {
navigateTo(url){
ipcRenderer.send('navigateTo',url)
},
showMessageBox(options,callback){
(async ()=>{
var res = await ipcRenderer.invoke('show-message-box',options)
callback(res)
})();
},
showDeleteMessageBox(msg,callback){
(async ()=>{
var res = await ipcRenderer.invoke('show-delete-message-box',msg)
callback(res)
})();
},
createCmd(id,stdout_callback){
let cmd = spawn('C:\\Windows\\System32\\cmd.exe')
//cmd.stdin.write('cd "c:\\"\n')
cmd.stdout.on('data', (data)=>{ stdout_callback(data.toString())})
process_list.push({id:id,cmd:cmd,stdout_callback:stdout_callback})
},
removeCmd(id){
let index = process_list.findIndex(p=> p.id === id)
kill(process_list[index].cmd.pid);
process_list = process_list.filter(c=>c.id !== id)
},
getCmdList(){
return [...process_list]
},
exeCmdCommand(cmd_id,command){
let process = process_list.filter(c=>c.id === cmd_id)[0]
process.cmd.stdin.write(command+'\n')
}
},
filesApi: {
getFiles(srcpath,callback){
(async () => {
let res = await ipcRenderer.invoke('get-files',srcpath)
callback(res)
})();
},
readFile(srcpath,callback){
(async () => {
let res = await ipcRenderer.invoke('read-file',srcpath)
callback(res)
})();
},
writeFile(srcpath,text){
ipcRenderer.send('write-file',srcpath,text)
},
deleteFile(srcpath,callback){
(async () => {
await ipcRenderer.invoke('delete-file',srcpath)
callback()
})();
},
rename(srcpath,newpath,callback){
(async () => {
await ipcRenderer.invoke('rename-file-folder',srcpath,newpath)
callback()
})();
},
createFile(srcpath,text,callback){
(async () => {
await ipcRenderer.invoke('create-file',srcpath,text)
callback()
})();
},
},
directoriesApi: {
searchDirectory(callback){
ipcRenderer.send('select-directory')
ipcRenderer.on('select-directory-reply',(event, arg) => {
callback(arg)
})
},
getDirectories(srcpath,callback){
(async () => {
let res = await ipcRenderer.invoke('get-directories',srcpath)
callback(res)
})();
},
getDirectoriesAndFiles(srcpath,callback){
ipcRenderer.send('get-directories-and-files',srcpath)
ipcRenderer.on('get-directories-and-files-reply',(event, arg) => {
callback(arg)
})
},
getDirectoriesAndFilesRecursive(srcpath,callback){
(async () => {
let res = await ipcRenderer.invoke('get-directories-and-files-recursive',srcpath)
callback(res)
})();
},
deleteFolder(srcpath,callback){
(async () => {
await ipcRenderer.invoke('delete-folder',srcpath)
callback()
})();
},
rename(srcpath,newpath,callback){
(async () => {
await ipcRenderer.invoke('rename-file-folder',srcpath,newpath)
callback()
})();
},
createFolder(srcpath,callback){
(async () => {
await ipcRenderer.invoke('create-folder',srcpath)
callback()
})();
},
}
})