clickgo-native 0.0.1-dev → 0.0.2-dev2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +141 -239
- package/dist/index.ts +178 -268
- package/dist/pre.js +8 -0
- package/dist/pre.ts +7 -0
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -9,14 +9,134 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.run = exports.
|
|
12
|
+
exports.run = exports.ready = exports.verifyToken = exports.unbind = exports.once = exports.bind = void 0;
|
|
13
13
|
const electron = require("electron");
|
|
14
|
+
const path = require("path");
|
|
14
15
|
electron.Menu.setApplicationMenu(null);
|
|
15
16
|
let isReady = false;
|
|
16
17
|
let isFrame = false;
|
|
17
18
|
let isQuit = true;
|
|
18
|
-
|
|
19
|
+
let win;
|
|
20
|
+
const platform = process.platform;
|
|
19
21
|
let token = '';
|
|
22
|
+
const methods = {
|
|
23
|
+
'cg-init': {
|
|
24
|
+
'once': true,
|
|
25
|
+
handler: function (t) {
|
|
26
|
+
if (!t || !win) {
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
win.resizable = true;
|
|
30
|
+
token = t;
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
'cg-quit': {
|
|
34
|
+
'once': false,
|
|
35
|
+
handler: function (t) {
|
|
36
|
+
if (!win || !t) {
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
if (!verifyToken(t)) {
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
electron.app.quit();
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
'cg-set-size': {
|
|
46
|
+
'once': false,
|
|
47
|
+
handler: function (t, width, height) {
|
|
48
|
+
if (!win || !width || !height) {
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
if (!verifyToken(t)) {
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
win.setSize(width, height);
|
|
55
|
+
win.center();
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
'cg-set-state': {
|
|
59
|
+
'once': false,
|
|
60
|
+
handler: function (t, state) {
|
|
61
|
+
if (!win || !state) {
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
if (!verifyToken(t)) {
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
switch (state) {
|
|
68
|
+
case 'max': {
|
|
69
|
+
win.maximize();
|
|
70
|
+
break;
|
|
71
|
+
}
|
|
72
|
+
case 'min': {
|
|
73
|
+
win.minimize();
|
|
74
|
+
break;
|
|
75
|
+
}
|
|
76
|
+
default: {
|
|
77
|
+
win.restore();
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
},
|
|
82
|
+
'cg-close': {
|
|
83
|
+
'once': false,
|
|
84
|
+
handler: function (t) {
|
|
85
|
+
if (!win) {
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
if (!verifyToken(t)) {
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
win.close();
|
|
92
|
+
}
|
|
93
|
+
},
|
|
94
|
+
'cg-mouse-ignore': {
|
|
95
|
+
'once': false,
|
|
96
|
+
handler: function (t, val) {
|
|
97
|
+
if (!win) {
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
if (!verifyToken(t)) {
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
if (val) {
|
|
104
|
+
win.setIgnoreMouseEvents(true, { 'forward': true });
|
|
105
|
+
}
|
|
106
|
+
else {
|
|
107
|
+
win.setIgnoreMouseEvents(false);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
function bind(name, handler, once = false) {
|
|
113
|
+
methods[name] = {
|
|
114
|
+
'once': once,
|
|
115
|
+
'handler': handler
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
exports.bind = bind;
|
|
119
|
+
function once(name, handler) {
|
|
120
|
+
bind(name, handler, true);
|
|
121
|
+
}
|
|
122
|
+
exports.once = once;
|
|
123
|
+
function unbind(name) {
|
|
124
|
+
if (!methods[name]) {
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
delete methods[name];
|
|
128
|
+
}
|
|
129
|
+
exports.unbind = unbind;
|
|
130
|
+
electron.ipcMain.handle('pre', function (e, name, ...param) {
|
|
131
|
+
if (!methods[name]) {
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
const r = methods[name].handler(...param);
|
|
135
|
+
if (methods[name].once) {
|
|
136
|
+
delete methods[name];
|
|
137
|
+
}
|
|
138
|
+
return r;
|
|
139
|
+
});
|
|
20
140
|
function verifyToken(t) {
|
|
21
141
|
if (t !== token) {
|
|
22
142
|
return false;
|
|
@@ -31,41 +151,13 @@ function ready() {
|
|
|
31
151
|
});
|
|
32
152
|
}
|
|
33
153
|
exports.ready = ready;
|
|
34
|
-
|
|
35
|
-
function on(name, handler, once = false) {
|
|
36
|
-
if (!listeners[name]) {
|
|
37
|
-
listeners[name] = [];
|
|
38
|
-
}
|
|
39
|
-
listeners[name].push({
|
|
40
|
-
'once': once,
|
|
41
|
-
'handler': handler
|
|
42
|
-
});
|
|
43
|
-
}
|
|
44
|
-
exports.on = on;
|
|
45
|
-
function once(name, handler) {
|
|
46
|
-
on(name, handler, true);
|
|
47
|
-
}
|
|
48
|
-
exports.once = once;
|
|
49
|
-
function off(name, handler) {
|
|
50
|
-
if (!listeners[name]) {
|
|
51
|
-
return;
|
|
52
|
-
}
|
|
53
|
-
for (let i = 0; i < listeners[name].length; ++i) {
|
|
54
|
-
if (listeners[name][i].handler !== handler) {
|
|
55
|
-
continue;
|
|
56
|
-
}
|
|
57
|
-
listeners[name].splice(i, 1);
|
|
58
|
-
if (listeners[name].length === 0) {
|
|
59
|
-
delete listeners[name];
|
|
60
|
-
break;
|
|
61
|
-
}
|
|
62
|
-
--i;
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
exports.off = off;
|
|
66
|
-
let win;
|
|
67
|
-
function createForm(path) {
|
|
154
|
+
function createForm(p) {
|
|
68
155
|
const op = {
|
|
156
|
+
'webPreferences': {
|
|
157
|
+
'nodeIntegration': false,
|
|
158
|
+
'contextIsolation': true,
|
|
159
|
+
'preload': path.join(__dirname, '/pre.js')
|
|
160
|
+
},
|
|
69
161
|
'width': 500,
|
|
70
162
|
'height': 400,
|
|
71
163
|
'frame': isFrame,
|
|
@@ -87,89 +179,13 @@ function createForm(path) {
|
|
|
87
179
|
else {
|
|
88
180
|
}
|
|
89
181
|
win.show();
|
|
90
|
-
const timerFunc = function () {
|
|
91
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
92
|
-
if (!win) {
|
|
93
|
-
return;
|
|
94
|
-
}
|
|
95
|
-
try {
|
|
96
|
-
const rtn = yield win.webContents.executeJavaScript('window.clickGoNative && clickGoNative.isReady');
|
|
97
|
-
if (!rtn) {
|
|
98
|
-
setTimeout(function () {
|
|
99
|
-
timerFunc().catch(function (e) {
|
|
100
|
-
console.log(e);
|
|
101
|
-
});
|
|
102
|
-
}, 100);
|
|
103
|
-
return;
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
catch (_a) {
|
|
107
|
-
setTimeout(function () {
|
|
108
|
-
timerFunc().catch(function (e) {
|
|
109
|
-
console.log(e);
|
|
110
|
-
});
|
|
111
|
-
}, 100);
|
|
112
|
-
return;
|
|
113
|
-
}
|
|
114
|
-
const list = JSON.parse(yield win.webContents.executeJavaScript('clickGoNative.cgInnerGetSends()'));
|
|
115
|
-
for (const item of list) {
|
|
116
|
-
if (!listeners[item.name]) {
|
|
117
|
-
continue;
|
|
118
|
-
}
|
|
119
|
-
for (let i = 0; i < listeners[item.name].length; ++i) {
|
|
120
|
-
const it = listeners[item.name][i];
|
|
121
|
-
const result = it.handler(item.param);
|
|
122
|
-
if (it.once) {
|
|
123
|
-
listeners[item.name].splice(i, 1);
|
|
124
|
-
--i;
|
|
125
|
-
}
|
|
126
|
-
if (result instanceof Promise) {
|
|
127
|
-
result.then(function (result) {
|
|
128
|
-
if (!win) {
|
|
129
|
-
return;
|
|
130
|
-
}
|
|
131
|
-
win.webContents.executeJavaScript(`clickGoNative.cgInnerReceive(${item.id}, "${item.name}", ${result !== undefined ? (', "' + result.replace(/"/g, '\\"') + '"') : ''})`).catch(function (e) {
|
|
132
|
-
console.log(e);
|
|
133
|
-
});
|
|
134
|
-
}).catch(function (e) {
|
|
135
|
-
console.log(e);
|
|
136
|
-
});
|
|
137
|
-
}
|
|
138
|
-
else {
|
|
139
|
-
if (!win) {
|
|
140
|
-
return;
|
|
141
|
-
}
|
|
142
|
-
win.webContents.executeJavaScript(`clickGoNative.cgInnerReceive(${item.id}, "${item.name}", ${result !== undefined ? (', "' + result.replace(/"/g, '\\"') + '"') : ''})`).catch(function (e) {
|
|
143
|
-
console.log(e);
|
|
144
|
-
});
|
|
145
|
-
}
|
|
146
|
-
if (it.once) {
|
|
147
|
-
listeners[item.name].splice(i, 1);
|
|
148
|
-
if (listeners[item.name].length === 0) {
|
|
149
|
-
delete listeners[item.name];
|
|
150
|
-
break;
|
|
151
|
-
}
|
|
152
|
-
--i;
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
setTimeout(function () {
|
|
157
|
-
timerFunc().catch(function (e) {
|
|
158
|
-
console.log(e);
|
|
159
|
-
});
|
|
160
|
-
}, 100);
|
|
161
|
-
});
|
|
162
|
-
};
|
|
163
|
-
timerFunc().catch(function (e) {
|
|
164
|
-
console.log(e);
|
|
165
|
-
});
|
|
166
182
|
});
|
|
167
|
-
const lio =
|
|
168
|
-
const search = lio === -1 ? '' :
|
|
183
|
+
const lio = p.indexOf('?');
|
|
184
|
+
const search = lio === -1 ? '' : p.slice(lio + 1);
|
|
169
185
|
if (lio !== -1) {
|
|
170
|
-
|
|
186
|
+
p = p.slice(0, lio);
|
|
171
187
|
}
|
|
172
|
-
win.loadFile(
|
|
188
|
+
win.loadFile(p, {
|
|
173
189
|
'search': search
|
|
174
190
|
}).catch(function (e) {
|
|
175
191
|
throw e;
|
|
@@ -189,140 +205,26 @@ function run(path, opt = {}) {
|
|
|
189
205
|
isQuit = opt.quit;
|
|
190
206
|
}
|
|
191
207
|
createForm(path);
|
|
192
|
-
once('cg-init', function (t) {
|
|
193
|
-
if (!t || !win) {
|
|
194
|
-
return;
|
|
195
|
-
}
|
|
196
|
-
win.resizable = true;
|
|
197
|
-
token = t;
|
|
198
|
-
});
|
|
199
|
-
on('cg-quit', function (j) {
|
|
200
|
-
if (!win || !j) {
|
|
201
|
-
return;
|
|
202
|
-
}
|
|
203
|
-
try {
|
|
204
|
-
const rtn = JSON.parse(j);
|
|
205
|
-
if (!verifyToken(rtn.token)) {
|
|
206
|
-
return;
|
|
207
|
-
}
|
|
208
|
-
electron.app.quit();
|
|
209
|
-
}
|
|
210
|
-
catch (e) {
|
|
211
|
-
console.log(e);
|
|
212
|
-
}
|
|
213
|
-
});
|
|
214
|
-
on('cg-set-size', function (j) {
|
|
215
|
-
if (!win || !j) {
|
|
216
|
-
return;
|
|
217
|
-
}
|
|
218
|
-
try {
|
|
219
|
-
const rtn = JSON.parse(j);
|
|
220
|
-
if (!verifyToken(rtn.token)) {
|
|
221
|
-
return;
|
|
222
|
-
}
|
|
223
|
-
if (!rtn.width || !rtn.height) {
|
|
224
|
-
return;
|
|
225
|
-
}
|
|
226
|
-
win.setSize(rtn.width, rtn.height);
|
|
227
|
-
win.center();
|
|
228
|
-
}
|
|
229
|
-
catch (e) {
|
|
230
|
-
console.log(e);
|
|
231
|
-
}
|
|
232
|
-
});
|
|
233
|
-
on('cg-set-state', function (j) {
|
|
234
|
-
if (!win || !j) {
|
|
235
|
-
return;
|
|
236
|
-
}
|
|
237
|
-
try {
|
|
238
|
-
const rtn = JSON.parse(j);
|
|
239
|
-
if (!verifyToken(rtn.token)) {
|
|
240
|
-
return;
|
|
241
|
-
}
|
|
242
|
-
switch (rtn.state) {
|
|
243
|
-
case 'max': {
|
|
244
|
-
win.maximize();
|
|
245
|
-
break;
|
|
246
|
-
}
|
|
247
|
-
case 'min': {
|
|
248
|
-
win.minimize();
|
|
249
|
-
break;
|
|
250
|
-
}
|
|
251
|
-
default: {
|
|
252
|
-
win.restore();
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
catch (e) {
|
|
257
|
-
console.log(e);
|
|
258
|
-
}
|
|
259
|
-
});
|
|
260
|
-
on('cg-main-close', function (j) {
|
|
261
|
-
if (!win || !j) {
|
|
262
|
-
return;
|
|
263
|
-
}
|
|
264
|
-
try {
|
|
265
|
-
const rtn = JSON.parse(j);
|
|
266
|
-
if (!verifyToken(rtn.token)) {
|
|
267
|
-
return;
|
|
268
|
-
}
|
|
269
|
-
if (isQuit) {
|
|
270
|
-
electron.app.quit();
|
|
271
|
-
}
|
|
272
|
-
else {
|
|
273
|
-
win.close();
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
catch (e) {
|
|
277
|
-
console.log(e);
|
|
278
|
-
}
|
|
279
|
-
});
|
|
280
|
-
on('cg-mouse-ignore', function (j) {
|
|
281
|
-
if (!win || !j) {
|
|
282
|
-
return;
|
|
283
|
-
}
|
|
284
|
-
try {
|
|
285
|
-
const rtn = JSON.parse(j);
|
|
286
|
-
if (!verifyToken(rtn.token)) {
|
|
287
|
-
return;
|
|
288
|
-
}
|
|
289
|
-
if (rtn.param) {
|
|
290
|
-
win.setIgnoreMouseEvents(true, { 'forward': true });
|
|
291
|
-
}
|
|
292
|
-
else {
|
|
293
|
-
win.setIgnoreMouseEvents(false);
|
|
294
|
-
}
|
|
295
|
-
}
|
|
296
|
-
catch (e) {
|
|
297
|
-
console.log(e);
|
|
298
|
-
}
|
|
299
|
-
});
|
|
300
208
|
electron.app.on('window-all-closed', function () {
|
|
301
209
|
if (isQuit) {
|
|
302
210
|
electron.app.quit();
|
|
303
211
|
}
|
|
304
212
|
});
|
|
305
213
|
electron.app.on('activate', function () {
|
|
306
|
-
if (electron.BrowserWindow.getAllWindows().length
|
|
307
|
-
|
|
308
|
-
|
|
214
|
+
if (electron.BrowserWindow.getAllWindows().length > 0) {
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
createForm(path);
|
|
218
|
+
methods['cg-init'] = {
|
|
219
|
+
'once': true,
|
|
220
|
+
handler: function (t) {
|
|
309
221
|
if (!t || !win) {
|
|
310
222
|
return;
|
|
311
223
|
}
|
|
312
224
|
win.resizable = true;
|
|
313
225
|
token = t;
|
|
314
|
-
}
|
|
315
|
-
}
|
|
226
|
+
}
|
|
227
|
+
};
|
|
316
228
|
});
|
|
317
229
|
}
|
|
318
230
|
exports.run = run;
|
|
319
|
-
setInterval(function () {
|
|
320
|
-
const keysCount = {};
|
|
321
|
-
let count = 0;
|
|
322
|
-
for (const key in listeners) {
|
|
323
|
-
keysCount[key] = listeners[key].length;
|
|
324
|
-
count += keysCount[key];
|
|
325
|
-
}
|
|
326
|
-
console.log('keysCount', keysCount);
|
|
327
|
-
console.log(`All count: ${count}`);
|
|
328
|
-
}, 5000);
|
package/dist/index.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
// npm publish --tag dev --access public
|
|
2
2
|
// --- sass --watch dist/:dist/ --style compressed --no-source-map ---
|
|
3
3
|
import * as electron from 'electron';
|
|
4
|
+
import * as path from 'path';
|
|
5
|
+
|
|
4
6
|
electron.Menu.setApplicationMenu(null);
|
|
5
7
|
|
|
6
8
|
/** --- 环境是否已经准备好了 --- */
|
|
@@ -9,11 +11,163 @@ let isReady: boolean = false;
|
|
|
9
11
|
let isFrame: boolean = false;
|
|
10
12
|
/** --- 是否关闭窗口就退出 --- */
|
|
11
13
|
let isQuit: boolean = true;
|
|
14
|
+
/** --- 主窗体 --- */
|
|
15
|
+
let win: electron.BrowserWindow | undefined;
|
|
16
|
+
/** --- 当前系统平台 --- */
|
|
17
|
+
const platform: NodeJS.Platform = process.platform;
|
|
18
|
+
// const platform: NodeJS.Platform = 'darwin';
|
|
19
|
+
/** --- 当前设定的 token --- */
|
|
20
|
+
let token: string = '';
|
|
12
21
|
|
|
13
|
-
|
|
14
|
-
const
|
|
22
|
+
/** --- 监听前台的执行的方法 --- */
|
|
23
|
+
const methods: Record<string, {
|
|
24
|
+
'once': boolean;
|
|
25
|
+
'handler': (...param: any[]) => any | Promise<any>;
|
|
26
|
+
}> = {
|
|
27
|
+
// --- 成功运行一个 task 后 init ---
|
|
28
|
+
'cg-init': {
|
|
29
|
+
'once': true,
|
|
30
|
+
handler: function(t: string) {
|
|
31
|
+
if (!t || !win) {
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
win.resizable = true;
|
|
35
|
+
token = t;
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
// --- 完全退出软件 ---
|
|
39
|
+
'cg-quit': {
|
|
40
|
+
'once': false,
|
|
41
|
+
handler: function(t: string): void {
|
|
42
|
+
if (!win || !t) {
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
if (!verifyToken(t)) {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
electron.app.quit();
|
|
49
|
+
}
|
|
50
|
+
},
|
|
51
|
+
// --- 设置实体窗体大小,推荐仅限非 windows ---
|
|
52
|
+
'cg-set-size': {
|
|
53
|
+
'once': false,
|
|
54
|
+
handler: function(t: string, width: number, height: number): void {
|
|
55
|
+
if (!win || !width || !height) {
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
if (!verifyToken(t)) {
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
win.setSize(width, height);
|
|
62
|
+
win.center();
|
|
63
|
+
}
|
|
64
|
+
},
|
|
65
|
+
// --- 设置窗体最大化、最小化、还原 ---
|
|
66
|
+
'cg-set-state': {
|
|
67
|
+
'once': false,
|
|
68
|
+
handler: function(t: string, state: string): void {
|
|
69
|
+
if (!win || !state) {
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
if (!verifyToken(t)) {
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
switch (state) {
|
|
76
|
+
case 'max': {
|
|
77
|
+
win.maximize();
|
|
78
|
+
break;
|
|
79
|
+
}
|
|
80
|
+
case 'min': {
|
|
81
|
+
win.minimize();
|
|
82
|
+
break;
|
|
83
|
+
}
|
|
84
|
+
default: {
|
|
85
|
+
win.restore();
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
},
|
|
90
|
+
// --- 关闭窗体但不退出软件 ---
|
|
91
|
+
'cg-close': {
|
|
92
|
+
'once': false,
|
|
93
|
+
handler: function(t: string): void {
|
|
94
|
+
if (!win) {
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
if (!verifyToken(t)) {
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
win.close();
|
|
101
|
+
}
|
|
102
|
+
},
|
|
103
|
+
// --- 设置 IgnoreMouseEvents,仅限 windows ---
|
|
104
|
+
'cg-mouse-ignore': {
|
|
105
|
+
'once': false,
|
|
106
|
+
handler: function(t: string, val: boolean): void {
|
|
107
|
+
if (!win) {
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
if (!verifyToken(t)) {
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
if (val) {
|
|
114
|
+
win.setIgnoreMouseEvents(true, { 'forward': true });
|
|
115
|
+
}
|
|
116
|
+
else {
|
|
117
|
+
win.setIgnoreMouseEvents(false);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* --- node 端绑定前端来调用的方法 ---
|
|
125
|
+
* @param name 方法名
|
|
126
|
+
* @param handler 执行的函数
|
|
127
|
+
* @param once 是否只执行一次
|
|
128
|
+
*/
|
|
129
|
+
export function bind(
|
|
130
|
+
name: string,
|
|
131
|
+
handler: (...param: any[]) => any | Promise<any>,
|
|
132
|
+
once: boolean = false
|
|
133
|
+
): void {
|
|
134
|
+
methods[name] = {
|
|
135
|
+
'once': once,
|
|
136
|
+
'handler': handler
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* --- node 端绑定前端来调用的方法只执行一次 ---
|
|
142
|
+
* @param name 方法名
|
|
143
|
+
* @param handler 执行的函数
|
|
144
|
+
*/
|
|
145
|
+
export function once(name: string, handler: (...param: any[]) => any | Promise<any>): void {
|
|
146
|
+
bind(name, handler, true);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* --- 解绑 node 方法 ---
|
|
151
|
+
* @param name 方法名
|
|
152
|
+
*/
|
|
153
|
+
export function unbind(name: string): void {
|
|
154
|
+
if (!methods[name]) {
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
delete methods[name];
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
electron.ipcMain.handle('pre', function(e: electron.IpcMainInvokeEvent, name: string, ...param: any[]): any | Promise<any> {
|
|
161
|
+
if (!methods[name]) {
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
const r = methods[name].handler(...param);
|
|
165
|
+
if (methods[name].once) {
|
|
166
|
+
delete methods[name];
|
|
167
|
+
}
|
|
168
|
+
return r;
|
|
169
|
+
});
|
|
15
170
|
|
|
16
|
-
let token: string = '';
|
|
17
171
|
/**
|
|
18
172
|
* --- 验证 token 是否正确 ---
|
|
19
173
|
* @param t 要验证的 token
|
|
@@ -33,66 +187,24 @@ export async function ready(): Promise<void> {
|
|
|
33
187
|
isReady = true;
|
|
34
188
|
}
|
|
35
189
|
|
|
36
|
-
|
|
37
|
-
const listeners: Record<string, Array<{
|
|
38
|
-
'once': boolean;
|
|
39
|
-
'handler': (param?: string) => any | Promise<any>;
|
|
40
|
-
}>> = {};
|
|
41
|
-
// --- 添加前台监听 ---
|
|
42
|
-
export function on(
|
|
43
|
-
name: string,
|
|
44
|
-
handler: (param?: string) => any | Promise<any>,
|
|
45
|
-
once: boolean = false
|
|
46
|
-
): void {
|
|
47
|
-
if (!listeners[name]) {
|
|
48
|
-
listeners[name] = [];
|
|
49
|
-
}
|
|
50
|
-
listeners[name].push({
|
|
51
|
-
'once': once,
|
|
52
|
-
'handler': handler
|
|
53
|
-
});
|
|
54
|
-
}
|
|
55
|
-
export function once(name: string, handler: (param?: string) => any | Promise<any>): void {
|
|
56
|
-
on(name, handler, true);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
// --- 移除前台监听 ---
|
|
60
|
-
export function off(name: string, handler: (param?: string) => any | Promise<any>): void {
|
|
61
|
-
if (!listeners[name]) {
|
|
62
|
-
return;
|
|
63
|
-
}
|
|
64
|
-
for (let i = 0; i < listeners[name].length; ++i) {
|
|
65
|
-
if (listeners[name][i].handler !== handler) {
|
|
66
|
-
continue;
|
|
67
|
-
}
|
|
68
|
-
listeners[name].splice(i, 1);
|
|
69
|
-
if (listeners[name].length === 0) {
|
|
70
|
-
delete listeners[name];
|
|
71
|
-
break;
|
|
72
|
-
}
|
|
73
|
-
--i;
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
let win: electron.BrowserWindow | undefined;
|
|
78
|
-
function createForm(path: string): void {
|
|
190
|
+
function createForm(p: string): void {
|
|
79
191
|
const op: Electron.BrowserWindowConstructorOptions = {
|
|
192
|
+
'webPreferences': {
|
|
193
|
+
'nodeIntegration': false,
|
|
194
|
+
'contextIsolation': true,
|
|
195
|
+
'preload': path.join(__dirname, '/pre.js')
|
|
196
|
+
},
|
|
80
197
|
'width': 500,
|
|
81
198
|
'height': 400,
|
|
82
199
|
'frame': isFrame,
|
|
83
200
|
'resizable': false,
|
|
84
201
|
'show': false,
|
|
85
|
-
// 'hasShadow': false,
|
|
86
202
|
'center': true,
|
|
87
203
|
'transparent': isFrame ? undefined : true
|
|
88
204
|
};
|
|
89
|
-
/*
|
|
90
|
-
if (platform === 'win32') {
|
|
91
|
-
op.transparent = true;
|
|
92
|
-
}
|
|
93
|
-
*/
|
|
94
205
|
win = new electron.BrowserWindow(op);
|
|
95
206
|
win.webContents.userAgent = 'electron/' + electron.app.getVersion() + ' ' + platform + '/' + process.arch;
|
|
207
|
+
// win.webContents.openDevTools();
|
|
96
208
|
win.once('ready-to-show', function(): void {
|
|
97
209
|
if (!win) {
|
|
98
210
|
return;
|
|
@@ -105,93 +217,13 @@ function createForm(path: string): void {
|
|
|
105
217
|
// --- 设置为第一个窗体的大小 ---
|
|
106
218
|
}
|
|
107
219
|
win.show();
|
|
108
|
-
// --- timer ---
|
|
109
|
-
const timerFunc = async function(): Promise<void> {
|
|
110
|
-
if (!win) {
|
|
111
|
-
return;
|
|
112
|
-
}
|
|
113
|
-
try {
|
|
114
|
-
const rtn = await win.webContents.executeJavaScript('window.clickGoNative && clickGoNative.isReady');
|
|
115
|
-
if (!rtn) {
|
|
116
|
-
// --- 下一次循环 ---
|
|
117
|
-
setTimeout(function() {
|
|
118
|
-
timerFunc().catch(function(e) {
|
|
119
|
-
console.log(e);
|
|
120
|
-
});
|
|
121
|
-
}, 100);
|
|
122
|
-
return;
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
catch {
|
|
126
|
-
// --- 下一次循环 ---
|
|
127
|
-
setTimeout(function() {
|
|
128
|
-
timerFunc().catch(function(e) {
|
|
129
|
-
console.log(e);
|
|
130
|
-
});
|
|
131
|
-
}, 100);
|
|
132
|
-
return;
|
|
133
|
-
}
|
|
134
|
-
// --- 检测浏览器是否有要执行的内容 ---
|
|
135
|
-
const list = JSON.parse(await win.webContents.executeJavaScript('clickGoNative.cgInnerGetSends()')) as Array<{ 'id': number; 'name': string; 'param': string | undefined; }>;
|
|
136
|
-
for (const item of list) {
|
|
137
|
-
if (!listeners[item.name]) {
|
|
138
|
-
continue;
|
|
139
|
-
}
|
|
140
|
-
// --- 根据 name 执行相关函数 ---
|
|
141
|
-
for (let i = 0; i < listeners[item.name].length; ++i) {
|
|
142
|
-
const it = listeners[item.name][i];
|
|
143
|
-
const result = it.handler(item.param);
|
|
144
|
-
if (it.once) {
|
|
145
|
-
listeners[item.name].splice(i, 1);
|
|
146
|
-
--i;
|
|
147
|
-
}
|
|
148
|
-
if (result instanceof Promise) {
|
|
149
|
-
result.then(function(result) {
|
|
150
|
-
if (!win) {
|
|
151
|
-
return;
|
|
152
|
-
}
|
|
153
|
-
win.webContents.executeJavaScript(`clickGoNative.cgInnerReceive(${item.id}, "${item.name}", ${result !== undefined ? (', "' + (result as string).replace(/"/g, '\\"') + '"') : ''})`).catch(function(e) {
|
|
154
|
-
console.log(e);
|
|
155
|
-
});
|
|
156
|
-
}).catch(function(e) {
|
|
157
|
-
console.log(e);
|
|
158
|
-
});
|
|
159
|
-
}
|
|
160
|
-
else {
|
|
161
|
-
if (!win) {
|
|
162
|
-
return;
|
|
163
|
-
}
|
|
164
|
-
win.webContents.executeJavaScript(`clickGoNative.cgInnerReceive(${item.id}, "${item.name}", ${result !== undefined ? (', "' + (result as string).replace(/"/g, '\\"') + '"') : ''})`).catch(function(e) {
|
|
165
|
-
console.log(e);
|
|
166
|
-
});
|
|
167
|
-
}
|
|
168
|
-
if (it.once) {
|
|
169
|
-
listeners[item.name].splice(i, 1);
|
|
170
|
-
if (listeners[item.name].length === 0) {
|
|
171
|
-
delete listeners[item.name];
|
|
172
|
-
break;
|
|
173
|
-
}
|
|
174
|
-
--i;
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
// --- 下一次循环 ---
|
|
179
|
-
setTimeout(function() {
|
|
180
|
-
timerFunc().catch(function(e) {
|
|
181
|
-
console.log(e);
|
|
182
|
-
});
|
|
183
|
-
}, 100);
|
|
184
|
-
};
|
|
185
|
-
timerFunc().catch(function(e) {
|
|
186
|
-
console.log(e);
|
|
187
|
-
});
|
|
188
220
|
});
|
|
189
|
-
const lio =
|
|
190
|
-
const search = lio === -1 ? '' :
|
|
221
|
+
const lio = p.indexOf('?');
|
|
222
|
+
const search = lio === -1 ? '' : p.slice(lio + 1);
|
|
191
223
|
if (lio !== -1) {
|
|
192
|
-
|
|
224
|
+
p = p.slice(0, lio);
|
|
193
225
|
}
|
|
194
|
-
win.loadFile(
|
|
226
|
+
win.loadFile(p, {
|
|
195
227
|
'search': search
|
|
196
228
|
}).catch(function(e): void {
|
|
197
229
|
throw e;
|
|
@@ -212,120 +244,6 @@ export function run(path: string, opt: { 'frame'?: boolean; 'quit'?: boolean; }
|
|
|
212
244
|
isQuit = opt.quit;
|
|
213
245
|
}
|
|
214
246
|
createForm(path);
|
|
215
|
-
// --- 成功运行一个 task 后 init ---
|
|
216
|
-
once('cg-init', function(t): void {
|
|
217
|
-
if (!t || !win) {
|
|
218
|
-
return;
|
|
219
|
-
}
|
|
220
|
-
win.resizable = true;
|
|
221
|
-
token = t;
|
|
222
|
-
});
|
|
223
|
-
// --- 退出软件 ---
|
|
224
|
-
on('cg-quit', function(j): void {
|
|
225
|
-
if (!win || !j) {
|
|
226
|
-
return;
|
|
227
|
-
}
|
|
228
|
-
try {
|
|
229
|
-
const rtn = JSON.parse(j);
|
|
230
|
-
if (!verifyToken(rtn.token)) {
|
|
231
|
-
return;
|
|
232
|
-
}
|
|
233
|
-
electron.app.quit();
|
|
234
|
-
}
|
|
235
|
-
catch (e) {
|
|
236
|
-
console.log(e);
|
|
237
|
-
}
|
|
238
|
-
});
|
|
239
|
-
// --- 设置窗体大小 ---
|
|
240
|
-
on('cg-set-size', function(j) {
|
|
241
|
-
if (!win || !j) {
|
|
242
|
-
return;
|
|
243
|
-
}
|
|
244
|
-
try {
|
|
245
|
-
const rtn = JSON.parse(j);
|
|
246
|
-
if (!verifyToken(rtn.token)) {
|
|
247
|
-
return;
|
|
248
|
-
}
|
|
249
|
-
if (!rtn.width || !rtn.height) {
|
|
250
|
-
return;
|
|
251
|
-
}
|
|
252
|
-
win.setSize(rtn.width, rtn.height);
|
|
253
|
-
win.center();
|
|
254
|
-
}
|
|
255
|
-
catch (e) {
|
|
256
|
-
console.log(e);
|
|
257
|
-
}
|
|
258
|
-
});
|
|
259
|
-
// --- 设置窗体最大化、最小化、还原 ---
|
|
260
|
-
on('cg-set-state', function(j) {
|
|
261
|
-
if (!win || !j) {
|
|
262
|
-
return;
|
|
263
|
-
}
|
|
264
|
-
try {
|
|
265
|
-
const rtn = JSON.parse(j);
|
|
266
|
-
if (!verifyToken(rtn.token)) {
|
|
267
|
-
return;
|
|
268
|
-
}
|
|
269
|
-
switch (rtn.state) {
|
|
270
|
-
case 'max': {
|
|
271
|
-
win.maximize();
|
|
272
|
-
break;
|
|
273
|
-
}
|
|
274
|
-
case 'min': {
|
|
275
|
-
win.minimize();
|
|
276
|
-
break;
|
|
277
|
-
}
|
|
278
|
-
default: {
|
|
279
|
-
win.restore();
|
|
280
|
-
}
|
|
281
|
-
}
|
|
282
|
-
}
|
|
283
|
-
catch (e) {
|
|
284
|
-
console.log(e);
|
|
285
|
-
}
|
|
286
|
-
});
|
|
287
|
-
// --- 响应主 task 被关闭的情况 ---
|
|
288
|
-
on('cg-main-close', function(j) {
|
|
289
|
-
if (!win || !j) {
|
|
290
|
-
return;
|
|
291
|
-
}
|
|
292
|
-
try {
|
|
293
|
-
const rtn = JSON.parse(j);
|
|
294
|
-
if (!verifyToken(rtn.token)) {
|
|
295
|
-
return;
|
|
296
|
-
}
|
|
297
|
-
if (isQuit) {
|
|
298
|
-
electron.app.quit();
|
|
299
|
-
}
|
|
300
|
-
else {
|
|
301
|
-
win.close();
|
|
302
|
-
}
|
|
303
|
-
}
|
|
304
|
-
catch (e) {
|
|
305
|
-
console.log(e);
|
|
306
|
-
}
|
|
307
|
-
});
|
|
308
|
-
// --- 设置 IgnoreMouseEvents,仅限 windows ---
|
|
309
|
-
on('cg-mouse-ignore', function(j) {
|
|
310
|
-
if (!win || !j) {
|
|
311
|
-
return;
|
|
312
|
-
}
|
|
313
|
-
try {
|
|
314
|
-
const rtn = JSON.parse(j);
|
|
315
|
-
if (!verifyToken(rtn.token)) {
|
|
316
|
-
return;
|
|
317
|
-
}
|
|
318
|
-
if (rtn.param) {
|
|
319
|
-
win.setIgnoreMouseEvents(true, { 'forward': true });
|
|
320
|
-
}
|
|
321
|
-
else {
|
|
322
|
-
win.setIgnoreMouseEvents(false);
|
|
323
|
-
}
|
|
324
|
-
}
|
|
325
|
-
catch (e) {
|
|
326
|
-
console.log(e);
|
|
327
|
-
}
|
|
328
|
-
});
|
|
329
247
|
electron.app.on('window-all-closed', function(): void {
|
|
330
248
|
/*
|
|
331
249
|
// --- MAC ---
|
|
@@ -338,28 +256,20 @@ export function run(path: string, opt: { 'frame'?: boolean; 'quit'?: boolean; }
|
|
|
338
256
|
}
|
|
339
257
|
});
|
|
340
258
|
electron.app.on('activate', function(): void {
|
|
341
|
-
if (electron.BrowserWindow.getAllWindows().length
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
259
|
+
if (electron.BrowserWindow.getAllWindows().length > 0) {
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
createForm(path);
|
|
263
|
+
// --- 成功运行一个 task 后 init ---
|
|
264
|
+
methods['cg-init'] = {
|
|
265
|
+
'once': true,
|
|
266
|
+
handler: function(t: string) {
|
|
345
267
|
if (!t || !win) {
|
|
346
268
|
return;
|
|
347
269
|
}
|
|
348
270
|
win.resizable = true;
|
|
349
271
|
token = t;
|
|
350
|
-
}
|
|
351
|
-
}
|
|
272
|
+
}
|
|
273
|
+
};
|
|
352
274
|
});
|
|
353
275
|
}
|
|
354
|
-
|
|
355
|
-
// --- listeners 监视 ---
|
|
356
|
-
setInterval(function() {
|
|
357
|
-
const keysCount: Record<string, number> = {};
|
|
358
|
-
let count: number = 0;
|
|
359
|
-
for (const key in listeners) {
|
|
360
|
-
keysCount[key] = listeners[key].length;
|
|
361
|
-
count += keysCount[key];
|
|
362
|
-
}
|
|
363
|
-
console.log('keysCount', keysCount);
|
|
364
|
-
console.log(`All count: ${count}`);
|
|
365
|
-
}, 5000);
|
package/dist/pre.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const electron = require("electron");
|
|
4
|
+
electron.contextBridge.exposeInMainWorld('clickgoNative', {
|
|
5
|
+
invoke: function (name, ...param) {
|
|
6
|
+
return electron.ipcRenderer.invoke('pre', name, ...param);
|
|
7
|
+
}
|
|
8
|
+
});
|
package/dist/pre.ts
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "clickgo-native",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.2-dev2",
|
|
4
4
|
"description": "The software developed with ClickGo will run in Windows, Mac OS, Linux.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"clickgo",
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
"@types/node": "^17.0.45",
|
|
19
19
|
"@typescript-eslint/eslint-plugin": "^5.30.5",
|
|
20
20
|
"@typescript-eslint/parser": "^5.30.5",
|
|
21
|
-
"clickgo": "^3.0.
|
|
21
|
+
"clickgo": "^3.0.7-dev8",
|
|
22
22
|
"eslint": "^8.19.0",
|
|
23
23
|
"typescript": "^4.7.4"
|
|
24
24
|
},
|