forked from qinyongliang/suspension-utools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreload.js
321 lines (314 loc) · 11.2 KB
/
preload.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
const { ipcRenderer } = require("electron");
const fs = require("fs");
const utils = require('./utils.js')
const mineMap = {
bmp: "image/bmp",
gif: "image/gif",
heic: "image/heic",
jpeg: "image/jpeg",
jpg: "image/jpeg",
jpe: "image/jpeg",
png: "image/png",
svg: "image/svg+xml",
webp: "image/webp",
ico: "image/x-icon",
html: "html",
htm: "html",
};
function uuidv4() {
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) {
var r = (Math.random() * 16) | 0,
v = c == "x" ? r : (r & 0x3) | 0x8;
return v.toString(16);
});
}
function show(payload, filePath) {
let params = {};
let paramsIndex = payload.lastIndexOf("#");
// 解析参数到 params 对象
if (paramsIndex > 0) {
payload
.substring(paramsIndex + 1)
.split("&")
.forEach((item) => {
params[item.split("=")[0]] = item.split("=")[1];
});
}
utils.log(payload ? payload.substring(0, 30): null, filePath);
// 网页的情况
let isPage = filePath && (filePath.endsWith('htm') || filePath.endsWith('html'));
isPage ||= payload.startsWith("http://") || payload.startsWith("https://");
if (!isPage) {
let img = new Image();
img.src = payload;
img.onload = function () {
let width = img.width / (utools.isMacOs() ? 2 : 1);
let height = img.height / (utools.isMacOs() ? 2 : 1);
utils.log(`图片宽高: width: ${width}, height: ${height}`);
loadWindow(width, height, payload, filePath, params);
};
} else {
let width = utils.config.defaultWidth / (utools.isMacOs() ? 2 : 1);
let height = utils.config.defaultHeight / (utools.isMacOs() ? 2 : 1);
loadWindow(width, height, payload, filePath, params);
}
}
function loadWindow(width, height, payload, filePath, params) {
let scale = width / (height * 1.0);
//图片大小不能超过当前显示器80%,否则缩放
let cursorPoint = utools.getCursorScreenPoint();
let display = utools.getDisplayNearestPoint(cursorPoint);
if (display) {
width = Math.min(width, display.size.width * 0.8);
height = width / scale;
height = Math.min(height, display.size.height * 0.8);
width = height * scale;
}
let eleKey = uuidv4();
//通过localStorage传参,解决url传参的大小限制问题
localStorage.setItem(eleKey, payload);
localStorage.setItem(eleKey + "_file", filePath);
let windowOptions = {
title: "悬浮",
x: params.x ? parseInt(params.x) : cursorPoint.x,
y: params.y ? parseInt(params.y) : cursorPoint.y,
width: parseInt(width),
height: parseInt(height),
useContentSize: true,
//不能最大最小化
minimizable: false,
maximizable: false,
fullscreenable: false,
//背景透明,防止放大缩小时出现白框
transparent: true,
backgroundColor: "#00000000",
frame: false,
alwaysOnTop: true,
webPreferences: {
preload: "suspend.js",
// devTools: true,
},
}
utils.log(windowOptions);
let browserWindow = utools.createBrowserWindow("suspend.html?#" + eleKey, windowOptions, () => {
// browserWindow.webContents.openDevTools();
ipcRenderer.sendTo(browserWindow.webContents.id, "init");
for (var i = 1; i <= 5; i++) {
setTimeout(
() => ipcRenderer.sendTo(browserWindow.webContents.id, "init"),
i * 200
);
}
ipcRenderer.on("resize", (event, changed, proportion) => {
if (event.senderId == browserWindow.webContents.id) {
let nowBounds = browserWindow.getBounds();
let widthChanged = nowBounds.width + changed;
browserWindow.setSize(
Math.ceil(widthChanged),
Math.ceil(widthChanged * proportion)
);
}
});
ipcRenderer.on("moveBounds", (event, x, y, width, height) => {
if (event.senderId == browserWindow.webContents.id) {
let bound = browserWindow.getBounds();
let newBounds = {
x: parseInt(bound.x + x),
y: parseInt(bound.y + y),
width: parseInt(width || bound.width),
height: parseInt(height || bound.height),
};
browserWindow.setBounds(newBounds);
}
});
ipcRenderer.on("toEdit", (event) => {
if (event.senderId == browserWindow.webContents.id) {
let bound = browserWindow.getBounds();
browserWindow.capturePage().then((img) => {
utools.redirect("截图工具", {
type: "img",
data: `data:image/png;base64,${_arrayBufferToBase64(img)}#x=${bound.x}&y=${bound.y}`,
});
browserWindow.close();
});
}
});
ipcRenderer.on("copyNowImage", (event) => {
if (event.senderId == browserWindow.webContents.id) {
browserWindow.capturePage().then((img) => {
utools.copyImage(
`data:image/png;base64,${_arrayBufferToBase64(img)}`
);
utools.showNotification("图片已经拷贝至剪切板");
ipcRenderer.sendTo(browserWindow.webContents.id, "reduction");
});
}
});
ipcRenderer.on("saveNowImage", (event) => {
if (event.senderId == browserWindow.webContents.id) {
browserWindow.capturePage().then((img) => {
let defaultPath =
utools.getPath("downloads") +
"/suspend_" +
new Date().getTime() +
".png";
let savePath = utools.showSaveDialog({
title: "保存图片",
defaultPath: defaultPath,
buttonLabel: "保存",
});
if (savePath) {
fs.writeFileSync(savePath, img);
utools.showNotification("保存成功");
}
ipcRenderer.sendTo(browserWindow.webContents.id, "reduction");
});
}
});
browserWindow.on("will-resize", (event, newBounds) => {
event.preventDefault();
ipcRenderer.sendTo(
browserWindow.webContents.id,
"will-resize",
newBounds
);
});
});
if (utils.config.debug && utils.config.devTool) {
browserWindow.webContents.openDevTools();
}
}
function fileUrlData(path) {
return new Promise((resolve, reject) => {
let postfix = path.substring(path.lastIndexOf(".") + 1);
let supportType = mineMap[postfix.toLowerCase()];
if (supportType) {
fs.readFile(path, function (err, data) {
if (err) {
reject(err);
} else if ("html" === supportType) {
resolve(data.toString("utf-8"));
} else {
resolve(`data:${supportType};base64,${data.toString("base64")}`);
}
});
} else {
reject("不支持的文件格式!");
}
});
}
function _arrayBufferToBase64(buffer) {
var binary = "";
var bytes = new Uint8Array(buffer);
var len = bytes.byteLength;
for (var i = 0; i < len; i++) {
binary += String.fromCharCode(bytes[i]);
}
return window.btoa(binary);
}
window.exports = {
pic_sus: {
mode: "none",
args: {
enter: (action) => {
utils.log(action)
window.utools.hideMainWindow();
if (action.type === "files") {
for (i in action.payload) {
fileUrlData(action.payload[i].path)
.then((payload) => {
show(payload, action.payload[i].path);
})
.catch((err) => {
utools.showNotification(err);
})
.finally(() => {
window.utools.outPlugin();
});
}
} else if (action.type === "img") {
show(action.payload);
window.utools.outPlugin();
}
},
},
},
shot_sus: {
mode: "none",
args: {
enter: (action) => {
//解决用户反馈的截图并识别容易截取到utools黑屏的问题
utools.hideMainWindow();
utools.screenCapture((base64Str) => {
utools.copyImage(base64Str);
show(base64Str);
utools.outPlugin();
});
},
},
},
base_sus: {
mode: "none",
args: {
enter: (action) => {
show(action.payload);
window.utools.outPlugin();
},
},
},
svg_sus: {
mode: "none",
args: {
enter: (action) => {
var base64 = btoa(action.payload);
show(`data:image/svg+xml;base64,${base64}`);
window.utools.outPlugin();
},
},
},
htm_sus: {
mode: "none",
args: {
enter: (action) => {
window.utools.hideMainWindow();
utils.log(action);
switch (action.type) {
case "files":
for (i in action.payload) {
fileUrlData(action.payload[i].path)
.then((payload) => {
show(payload, action.payload[i].path);
})
.catch((err) => {
utools.showNotification(err);
})
.finally(() => {
window.utools.outPlugin();
});
}
break;
case "img":
show(action.payload);
window.utools.outPlugin();
default:
}
},
},
},
web_sus: {
mode: "none",
args: {
enter: (action) => {
window.utools.hideMainWindow();
utils.log(action);
if (action.type === "regex") {
try{
show(action.payload, action.payload);
} finally {
window.utools.outPlugin();
}
}
}
},
},
};