-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
448 lines (423 loc) · 15 KB
/
index.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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
const { app, BrowserWindow,
Tray, Menu, nativeImage,
ipcMain, globalShortcut,
dialog, clipboard,
shell, nativeTheme,
} = require('electron');
const fs = require('original-fs');
const path = require('path');
const robot = require('robotjs');
const { PNG } = require('pngjs');
const { exec } = require('child_process');
// Tray
let tray = null;
function create_tray() {
tray = new Tray(nativeImage.createFromPath(path.join(path.dirname(process.execPath), 'resources','app.asar.unpacked','src' ,'icon.png')));
const contextMenu = Menu.buildFromTemplate([
{ label: config.app_settings.Chinese ? '切换大小锁定状态' : 'Toggle Caps Lock Status', click: toggle_caps_status },
{ label: config.app_settings.Chinese ? '设置' : 'Settings', click: show_settings },
{ label: config.app_settings.Chinese ? '退出' : 'Exit', click: app.quit }
]);
tray.setToolTip('CapsTools');
tray.setContextMenu(contextMenu);
};
// Main Window
let main_window = null;
function create_main_window(){
main_window = new BrowserWindow({
frame: false,
transparent: true,
alwaysOnTop: true,
resizable: false,
show: false,
fullscreen: true,
disableAutoHideCursor: true,
webPreferences: {
nodeIntegration: true,
enableRemoteModule: true,
contextIsolation: false,
//sandbox: false
}
});
main_window.loadFile('renderer.html');
main_window.setMenu(null);
main_window.setSkipTaskbar(true);
main_window.setVisibleOnAllWorkspaces(false);
//main_window.webContents.openDevTools();//
};
// Caps Lock Listener
var caps_status = false;
function create_caps_listener(){
globalShortcut.register('CapsLock', async()=>{
caps_status = !caps_status;
if(caps_status){
main_window.webContents.send('reflash', {config: config, cjs_plugin: cjs_plugins_list});
main_window.show();
}else{
main_window.webContents.send('hide');
main_window.hide();
};
});
};
async function toggle_caps_status(){
caps_status = !caps_status;
if(caps_status){
main_window.webContents.send('reflash', {config: config, cjs_plugin: cjs_plugins_list});
main_window.show();
}else{
main_window.webContents.send('hide');
main_window.hide();
};
};
// Screenshot
ipcMain.on('screenshot',(event)=>{
let wait_interval = setInterval(()=>{
if (!caps_status) {
clearInterval(wait_interval);
let shot = get_screenshot().buffer;
if (config.default_plugin_settings.screenshot_save_path && config.default_plugin_settings.screenshot_save_path != ''){
let filePath = config.default_plugin_settings.screenshot_save_path;
if (!filePath.endsWith(path.sep)){filePath = filePath + path.sep};
filePath = filePath + `screenshot_${Date.now()}.png`;
fs.writeFile(filePath, shot, (err) => {
if (err) {
dialog.showErrorBox('Error', err.toString());
};
});
} else {
dialog.showSaveDialog({
title: config.app_settings.Chinese ? '保存截图' : 'Save Screenshot',
defaultPath: `screenshot_${Date.now()}.png`,
filters: [
{ name: 'PNG', extensions: ['png'] }
]
}).then((res) => {
if (res.filePath) {
fs.writeFile(res.filePath, shot, (err) => {
if (err) {
dialog.showErrorBox('Error', err);
};
});
};
});
};
};
},100);
});
function get_screenshot(){
let screenshot = robot.screen.capture();
shot = screenshot.image; // buffer
let size = robot.getScreenSize();
let pngdata = new PNG({width: size.width, height: size.height});
// robotjs返回的图片是bgra的
for (let i = 0; i < shot.length; i += 4) {
[shot[i], shot[i+2]] = [shot[i+2], shot[i]];
};
pngdata.data = Buffer.from(shot);
let bufferdata = PNG.sync.write(pngdata);
return {origin: shot, buffer: bufferdata};
// Claude 3.5 Sonnet 给的,buffer->png->buffer
};
// translate
ipcMain.on('translate',(event,message)=>{
let translate_window = new BrowserWindow({
title: config.app_settings.Chinese ? '翻译' : 'Translate',
show: false,
frame: true,
autoHideMenuBar: true,
icon: path.join(path.dirname(process.execPath), 'resources','app.asar.unpacked','src' ,'icon.png'),
webPreferences: {
nodeIntegration: true,
enableRemoteModule: true,
contextIsolation: false,
webviewTag: true
}
});
translate_window.loadFile('default_plugins/translate/tl.html');
//translate_window.webContents.openDevTools();
translate_window.setMenu(null);
translate_window.webContents.on('did-finish-load', () => {
translate_window.show();
translate_window.webContents.send('translate', {message: message, config: config});
});
});
// cliprecog & clipboard
var clip_history = [];
function is_obj_equal(obj1, obj2){
if(obj1.text === obj2.text && obj1.html === obj2.html){
if (obj1.image === null && obj2.image === null){
return true;
} else if (obj1.image.toDataURL() === obj2.image.toDataURL()){
return true;
};
return false;
}else{
return false;
};
};
var add_clip_interval = setInterval(()=>{
if ('default_tools' in config){
clearInterval(add_clip_interval);
if (config.default_tools.includes('cliprecog')) {
setInterval(()=>{
let now_clip = {text: null, html: null, image: null};
now_clip.text = clipboard.readText();
now_clip.html = clipboard.readHTML();
let now_clip_img = clipboard.readImage();
if (!now_clip_img.isEmpty()){
now_clip.image = now_clip_img;
};
if (clip_history.length === 0){
clip_history.push(now_clip);
} else if (! is_obj_equal(now_clip, clip_history[0])){
for (let i = 0; i < clip_history.length; i++){
if (is_obj_equal(now_clip, clip_history[i])){
clip_history.splice(i, 1);
break;
};
};
clip_history.unshift(now_clip);
//console.log(clip_history);
};
},1000);
};
} else {
clearInterval(add_clip_interval);
};
}, 100);
ipcMain.on('cliprecog',(event)=>{
let clip_window = new BrowserWindow({
title: config.app_settings.Chinese ? '剪贴板' : 'Clipboard',
show: false,
frame: true,
autoHideMenuBar: true,
fullscreenable: false,
maximizable: false,
icon: path.join(path.dirname(process.execPath), 'resources','app.asar.unpacked','src' ,'icon.png'),
webPreferences: {
nodeIntegration: true,
enableRemoteModule: true,
contextIsolation: false,
},
width: 290,
height: 580,
x: 100,
y: 100,
});
clip_window.loadFile('default_plugins/clip/clip.html');
clip_window.setMenu(null);
//clip_window.webContents.openDevTools();//
clip_window.webContents.on('did-finish-load', () => {
clip_window.show();
clip_window.webContents.send('open_clip', {clip_history: clip_history, config: config});
});
});
ipcMain.handle('get_clip',async(e)=>{
return clip_history;
});
ipcMain.on('changed_clip', (e,new_history) => {
clip_history = new_history;
});
ipcMain.on('save_text',(event,obj)=>{
dialog.showSaveDialog({
title: config.app_settings.Chinese ? '保存文本' : 'Save Text',
defaultPath: `text_${Date.now()}.txt`,
filters: [
{ name: 'Text', extensions: ['txt'] },
{ name: 'HTML', extensions: ['html'] },
]
}).then((res)=>{
if(res.filePath){
if(path.extname(res.filePath) === '.txt'){
fs.writeFile(res.filePath, obj.text, 'utf-8', (err)=>{console.error(err)});
} else if(path.extname(res.filePath) === '.html'){
fs.writeFile(res.filePath, obj.html, 'utf-8', (err)=>{console.error(err)});
};
}
});
});
ipcMain.on('translate_text',(event,text)=>{
let translate_window = new BrowserWindow({title: config.app_settings.Chinese ? '翻译' : 'Translate',show: false,frame: true,autoHideMenuBar: true,icon: path.join(path.dirname(process.execPath), 'resources','app.asar.unpacked','src' ,'icon.png'), webPreferences: {nodeIntegration: true,enableRemoteModule: true,contextIsolation: false,webviewTag: true}});translate_window.loadFile('default_plugins/translate/tl.html');
translate_window.setMenu(null);
translate_window.webContents.on('did-finish-load', () => {
translate_window.show();
translate_window.webContents.send('translate', {message: {from: 'cn', to: 'en', text: text}, config: config});
});
});
ipcMain.on('save_image',(e,nimg)=>{
dialog.showSaveDialog({
title: config.app_settings.Chinese ? '保存图片' : 'Save Image',
defaultPath: `image_${Date.now()}.png`,
filters: [
{ name: 'PNG', extensions: ['png'] }
]
}).then((res)=>{
if(res.filePath){
let img_data = nimg.toPNG();
fs.writeFile(res.filePath, img_data, (err)=>{console.error(err)});
}
});
});
function check_file_ability(file_path){
try{
fs.accessSync(file_path, fs.constants.R_OK);
return true;
} catch (error) {
return false;
};
};
ipcMain.handle('check_svg',(e,svg_path)=>{
if(check_file_ability(svg_path)){
let content = fs.readFileSync(svg_path, 'utf-8');
if(content.startsWith('<svg')){
return [true,content];
} else {
return false;
};
} else {
return false;
};
});
ipcMain.handle('check_img',(e,img_path)=>{
if(check_file_ability(img_path)){
return true;
} else {
return false;
};
});
ipcMain.on('run_cmd',(e,cmd_str)=>{
exec(cmd_str, (error, stdout, stderr) => {
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
if (error) {
console.error(`exec error: ${error}`);
};
});
});
ipcMain.on('open_pwa',(e,url)=>{
open_pwa(url);
});
function open_pwa(url){
let pwa_window = new BrowserWindow({
autoHideMenuBar: true,
show: true,
});
pwa_window.loadURL(url);
pwa_window.webContents.setWindowOpenHandler((details)=>{
if (details.url){
open_pwa(details.url);
};
return {action: 'deny'};
});
Menu.setApplicationMenu(Menu.buildFromTemplate([
{ label: config.app_settings.Chinese ? '操作' : 'Actions', submenu: [
{ label: config.app_settings.Chinese ? '刷新' : 'Refresh', click: (item,focusedWindow) => { focusedWindow.reload(); } },
{ label: config.app_settings.Chinese ? '浏览器打开' : 'Open in Browser', click: (item,focusedWindow) => { shell.openExternal(focusedWindow.getURL()); } },
{ label: config.app_settings.Chinese ? '关闭' : 'Close', click: (item,focusedWindow) => { focusedWindow.close(); } }
] }
]));
};
var cjs_plugins_list = [];
var cjs_plugins_func = [];
function process_cjsplugins(){
cjs_plugins_list = [];
cjs_plugins_func = [];
let plugin_config = config.plugin_tools;
for (let i = 0; i < plugin_config.length; i++){
let plugin_path = plugin_config[i];
try{
let plugin = require(plugin_path).default;
cjs_plugins_list.push({
"index": i,
"name": plugin.name,
"icon": plugin.icon,
});
cjs_plugins_func.push(plugin.func);
} catch (error) {
console.log(error);
};
};
};
ipcMain.on('run_cjs_plugin',(e,index)=>{
cjs_plugins_func[index]();
});
// Settings
function show_settings(){
let settings_window = new BrowserWindow({
title: config.app_settings.Chinese ? '设置' : 'Settings',
show: false,
frame: true,
autoHideMenuBar: true,
icon: path.join(path.dirname(process.execPath), 'resources','app.asar.unpacked','src' ,'icon.png'),
webPreferences: {
nodeIntegration: true,
enableRemoteModule: true,
contextIsolation: false,
},
});
settings_window.setMenu(null);
settings_window.loadFile('setting_page/set.html');
//settings_window.webContents.openDevTools();//
settings_window.webContents.on('did-finish-load', () => {
settings_window.show();
settings_window.webContents.send('open_set', {config: config});
});
};
// no repeat
const sil = app.requestSingleInstanceLock();
if (!sil) {
app.quit();
} else {
app.on('second-instance', (event, commandLine, workingDirectory) => {
if (main_window) {
if (main_window.isMinimized()) main_window.restore();
main_window.focus();
}
});
};
// Config
var config = {};
function read_config(){
let config_file_path = path.join(path.dirname(process.execPath), 'config.json');
try{
if(fs.existsSync(config_file_path)){
config = JSON.parse(fs.readFileSync(config_file_path, 'utf-8'));
}else{
write_config();
};
} catch (error) {
console.log(error);
write_config();
};
};
function write_config(config){
let config_file_path = path.join(path.dirname(process.execPath), 'config.json');
if(config){
fs.writeFileSync(config_file_path, JSON.stringify(config), 'utf-8');
}else{
config = {"custom_tools": [],"plugin_tools": [],"default_tools": ["screenshot","translate","cliprecog"],"default_plugin_settings": {"google_translate_mirror": "https://translate.yunkuerp.cn/","clip_quickinput": []},"app_settings": {"panel_place": "bottom","Chinese": false,"light_dark": "auto"}};
fs.writeFileSync(config_file_path, JSON.stringify(config), 'utf-8');
};
};
read_config();
if(config.app_settings.light_dark != 'light' && config.app_settings.light_dark != 'dark'){
config.app_settings.light_dark = nativeTheme.shouldUseDarkColors ? 'dark' : 'light';
};
process_cjsplugins();
ipcMain.on('save_settings',(e,new_config)=>{
//config = new_config;
write_config(new_config);
});
// App
app.on('ready',()=>{
create_tray();
create_main_window();
create_caps_listener();
if(process.argv.includes('--pwa')){
let pwa_url = process.argv[process.argv.indexOf('--pwa')+1];
open_pwa(pwa_url);
};
});
app.on('will-quit',()=>{
globalShortcut.unregisterAll();
});