electron-screenshots 0.5.22 → 0.5.24

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.
@@ -1,458 +0,0 @@
1
- "use strict";
2
- var __extends = (this && this.__extends) || (function () {
3
- var extendStatics = function (d, b) {
4
- extendStatics = Object.setPrototypeOf ||
5
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
- function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
- return extendStatics(d, b);
8
- };
9
- return function (d, b) {
10
- if (typeof b !== "function" && b !== null)
11
- throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
- extendStatics(d, b);
13
- function __() { this.constructor = d; }
14
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
- };
16
- })();
17
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
18
- if (k2 === undefined) k2 = k;
19
- var desc = Object.getOwnPropertyDescriptor(m, k);
20
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
21
- desc = { enumerable: true, get: function() { return m[k]; } };
22
- }
23
- Object.defineProperty(o, k2, desc);
24
- }) : (function(o, m, k, k2) {
25
- if (k2 === undefined) k2 = k;
26
- o[k2] = m[k];
27
- }));
28
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
29
- Object.defineProperty(o, "default", { enumerable: true, value: v });
30
- }) : function(o, v) {
31
- o["default"] = v;
32
- });
33
- var __importStar = (this && this.__importStar) || function (mod) {
34
- if (mod && mod.__esModule) return mod;
35
- var result = {};
36
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
37
- __setModuleDefault(result, mod);
38
- return result;
39
- };
40
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
41
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
42
- return new (P || (P = Promise))(function (resolve, reject) {
43
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
44
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
45
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
46
- step((generator = generator.apply(thisArg, _arguments || [])).next());
47
- });
48
- };
49
- var __generator = (this && this.__generator) || function (thisArg, body) {
50
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
51
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
52
- function verb(n) { return function (v) { return step([n, v]); }; }
53
- function step(op) {
54
- if (f) throw new TypeError("Generator is already executing.");
55
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
56
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
57
- if (y = 0, t) op = [op[0] & 2, t.value];
58
- switch (op[0]) {
59
- case 0: case 1: t = op; break;
60
- case 4: _.label++; return { value: op[1], done: false };
61
- case 5: _.label++; y = op[1]; op = [0]; continue;
62
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
63
- default:
64
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
65
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
66
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
67
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
68
- if (t[2]) _.ops.pop();
69
- _.trys.pop(); continue;
70
- }
71
- op = body.call(thisArg, _);
72
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
73
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
74
- }
75
- };
76
- var __importDefault = (this && this.__importDefault) || function (mod) {
77
- return (mod && mod.__esModule) ? mod : { "default": mod };
78
- };
79
- Object.defineProperty(exports, "__esModule", { value: true });
80
- var debug_1 = __importDefault(require("debug"));
81
- var electron_1 = require("electron");
82
- var events_1 = __importDefault(require("events"));
83
- var fs_extra_1 = __importDefault(require("fs-extra"));
84
- var event_1 = __importDefault(require("./event"));
85
- var getDisplay_1 = __importDefault(require("./getDisplay"));
86
- var padStart_1 = __importDefault(require("./padStart"));
87
- var Screenshots = /** @class */ (function (_super) {
88
- __extends(Screenshots, _super);
89
- function Screenshots(opts) {
90
- var _this = _super.call(this) || this;
91
- // 截图窗口对象
92
- _this.$win = null;
93
- _this.$view = new electron_1.BrowserView({
94
- webPreferences: {
95
- preload: require.resolve('./preload.js'),
96
- nodeIntegration: false,
97
- contextIsolation: true,
98
- },
99
- });
100
- _this.isReady = new Promise(function (resolve) {
101
- electron_1.ipcMain.once('SCREENSHOTS:ready', function () {
102
- _this.logger('SCREENSHOTS:ready');
103
- resolve();
104
- });
105
- });
106
- _this.logger = (opts === null || opts === void 0 ? void 0 : opts.logger) || (0, debug_1.default)('electron-screenshots');
107
- _this.singleWindow = (opts === null || opts === void 0 ? void 0 : opts.singleWindow) || false;
108
- _this.listenIpc();
109
- _this.$view.webContents.loadURL("file://".concat(require.resolve('react-screenshots/electron/electron.html')));
110
- if (opts === null || opts === void 0 ? void 0 : opts.lang) {
111
- _this.setLang(opts.lang);
112
- }
113
- return _this;
114
- }
115
- /**
116
- * 开始截图
117
- */
118
- Screenshots.prototype.startCapture = function () {
119
- return __awaiter(this, void 0, void 0, function () {
120
- var display, imageUrl;
121
- return __generator(this, function (_a) {
122
- switch (_a.label) {
123
- case 0:
124
- this.logger('startCapture');
125
- display = (0, getDisplay_1.default)();
126
- return [4 /*yield*/, Promise.all([this.capture(display), this.isReady])];
127
- case 1:
128
- imageUrl = (_a.sent())[0];
129
- return [4 /*yield*/, this.createWindow(display)];
130
- case 2:
131
- _a.sent();
132
- this.$view.webContents.send('SCREENSHOTS:capture', display, imageUrl);
133
- return [2 /*return*/];
134
- }
135
- });
136
- });
137
- };
138
- /**
139
- * 结束截图
140
- */
141
- Screenshots.prototype.endCapture = function () {
142
- return __awaiter(this, void 0, void 0, function () {
143
- return __generator(this, function (_a) {
144
- switch (_a.label) {
145
- case 0:
146
- this.logger('endCapture');
147
- return [4 /*yield*/, this.reset()];
148
- case 1:
149
- _a.sent();
150
- if (!this.$win) {
151
- return [2 /*return*/];
152
- }
153
- // 先清除 Kiosk 模式,然后取消全屏才有效
154
- this.$win.setKiosk(false);
155
- this.$win.blur();
156
- this.$win.blurWebView();
157
- this.$win.unmaximize();
158
- this.$win.removeBrowserView(this.$view);
159
- if (this.singleWindow) {
160
- this.$win.hide();
161
- }
162
- else {
163
- this.$win.destroy();
164
- }
165
- return [2 /*return*/];
166
- }
167
- });
168
- });
169
- };
170
- /**
171
- * 设置语言
172
- */
173
- Screenshots.prototype.setLang = function (lang) {
174
- return __awaiter(this, void 0, void 0, function () {
175
- return __generator(this, function (_a) {
176
- switch (_a.label) {
177
- case 0:
178
- this.logger('setLang', lang);
179
- return [4 /*yield*/, this.isReady];
180
- case 1:
181
- _a.sent();
182
- this.$view.webContents.send('SCREENSHOTS:setLang', lang);
183
- return [2 /*return*/];
184
- }
185
- });
186
- });
187
- };
188
- Screenshots.prototype.reset = function () {
189
- return __awaiter(this, void 0, void 0, function () {
190
- return __generator(this, function (_a) {
191
- switch (_a.label) {
192
- case 0:
193
- // 重置截图区域
194
- this.$view.webContents.send('SCREENSHOTS:reset');
195
- // 保证 UI 有足够的时间渲染
196
- return [4 /*yield*/, Promise.race([
197
- new Promise(function (resolve) {
198
- setTimeout(function () { return resolve(); }, 500);
199
- }),
200
- new Promise(function (resolve) {
201
- electron_1.ipcMain.once('SCREENSHOTS:reset', function () { return resolve(); });
202
- }),
203
- ])];
204
- case 1:
205
- // 保证 UI 有足够的时间渲染
206
- _a.sent();
207
- return [2 /*return*/];
208
- }
209
- });
210
- });
211
- };
212
- /**
213
- * 初始化窗口
214
- */
215
- Screenshots.prototype.createWindow = function (display) {
216
- var _a, _b;
217
- return __awaiter(this, void 0, void 0, function () {
218
- var windowTypes;
219
- var _this = this;
220
- return __generator(this, function (_c) {
221
- switch (_c.label) {
222
- case 0:
223
- // 重置截图区域
224
- return [4 /*yield*/, this.reset()];
225
- case 1:
226
- // 重置截图区域
227
- _c.sent();
228
- // 复用未销毁的窗口
229
- if (!this.$win || ((_b = (_a = this.$win) === null || _a === void 0 ? void 0 : _a.isDestroyed) === null || _b === void 0 ? void 0 : _b.call(_a))) {
230
- windowTypes = {
231
- darwin: 'panel',
232
- // linux 必须设置为 undefined,否则会在部分系统上不能触发focus 事件
233
- // https://github.com/nashaofu/screenshots/issues/203#issuecomment-1518923486
234
- linux: undefined,
235
- win32: 'toolbar',
236
- };
237
- this.$win = new electron_1.BrowserWindow({
238
- title: 'screenshots',
239
- x: display.x,
240
- y: display.y,
241
- width: display.width,
242
- height: display.height,
243
- useContentSize: true,
244
- type: windowTypes[process.platform],
245
- frame: false,
246
- show: false,
247
- autoHideMenuBar: true,
248
- transparent: true,
249
- resizable: false,
250
- movable: false,
251
- minimizable: false,
252
- maximizable: false,
253
- // focusable 必须设置为 true, 否则窗口不能及时响应esc按键,输入框也不能输入
254
- focusable: true,
255
- skipTaskbar: true,
256
- alwaysOnTop: true,
257
- /**
258
- * linux 下必须设置为false,否则不能全屏显示在最上层
259
- * mac 下设置为false,否则可能会导致程序坞不恢复问题,且与 kiosk 模式冲突
260
- */
261
- fullscreen: false,
262
- // mac fullscreenable 设置为 true 会导致应用崩溃
263
- fullscreenable: false,
264
- kiosk: true,
265
- backgroundColor: '#00000000',
266
- titleBarStyle: 'hidden',
267
- hasShadow: false,
268
- paintWhenInitiallyHidden: false,
269
- // mac 特有的属性
270
- roundedCorners: false,
271
- enableLargerThanScreen: false,
272
- acceptFirstMouse: true,
273
- });
274
- this.emit('windowCreated', this.$win);
275
- this.$win.on('show', function () {
276
- var _a, _b;
277
- (_a = _this.$win) === null || _a === void 0 ? void 0 : _a.focus();
278
- (_b = _this.$win) === null || _b === void 0 ? void 0 : _b.setKiosk(true);
279
- });
280
- this.$win.on('closed', function () {
281
- _this.emit('windowClosed', _this.$win);
282
- _this.$win = null;
283
- });
284
- }
285
- this.$win.setBrowserView(this.$view);
286
- // 适定平台
287
- if (process.platform === 'darwin') {
288
- this.$win.setWindowButtonVisibility(false);
289
- }
290
- if (process.platform !== 'win32') {
291
- this.$win.setVisibleOnAllWorkspaces(true, {
292
- visibleOnFullScreen: true,
293
- skipTransformProcessType: true,
294
- });
295
- }
296
- this.$win.blur();
297
- this.$win.setBounds(display);
298
- this.$view.setBounds({
299
- x: 0,
300
- y: 0,
301
- width: display.width,
302
- height: display.height,
303
- });
304
- this.$win.setAlwaysOnTop(true);
305
- this.$win.show();
306
- return [2 /*return*/];
307
- }
308
- });
309
- });
310
- };
311
- Screenshots.prototype.capture = function (display) {
312
- return __awaiter(this, void 0, void 0, function () {
313
- var NodeScreenshots, capturer, image, err_1, sources, source;
314
- return __generator(this, function (_a) {
315
- switch (_a.label) {
316
- case 0:
317
- this.logger('SCREENSHOTS:capture');
318
- _a.label = 1;
319
- case 1:
320
- _a.trys.push([1, 4, , 6]);
321
- return [4 /*yield*/, Promise.resolve().then(function () { return __importStar(require('node-screenshots')); })];
322
- case 2:
323
- NodeScreenshots = (_a.sent()).Screenshots;
324
- capturer = NodeScreenshots.fromPoint(display.x + display.width / 2, display.y + display.height / 2);
325
- this.logger('SCREENSHOTS:capture NodeScreenshots.fromPoint arguments %o', display);
326
- this.logger('SCREENSHOTS:capture NodeScreenshots.fromPoint return %o', capturer
327
- ? {
328
- id: capturer.id,
329
- x: capturer.x,
330
- y: capturer.y,
331
- width: capturer.width,
332
- height: capturer.height,
333
- rotation: capturer.rotation,
334
- scaleFactor: capturer.scaleFactor,
335
- isPrimary: capturer.isPrimary,
336
- }
337
- : null);
338
- if (!capturer) {
339
- throw new Error("NodeScreenshots.fromDisplay(".concat(display.id, ") get null"));
340
- }
341
- return [4 /*yield*/, capturer.capture()];
342
- case 3:
343
- image = _a.sent();
344
- return [2 /*return*/, "data:image/png;base64,".concat(image.toString('base64'))];
345
- case 4:
346
- err_1 = _a.sent();
347
- this.logger('SCREENSHOTS:capture NodeScreenshots capture() error %o', err_1);
348
- return [4 /*yield*/, electron_1.desktopCapturer.getSources({
349
- types: ['screen'],
350
- thumbnailSize: {
351
- width: display.width * display.scaleFactor,
352
- height: display.height * display.scaleFactor,
353
- },
354
- })];
355
- case 5:
356
- sources = _a.sent();
357
- source = void 0;
358
- // Linux系统上,screen.getDisplayNearestPoint 返回的 Display 对象的 id
359
- // 和这里 source 对象上的 display_id(Linux上,这个值是空字符串) 或 id 的中间部分,都不一致
360
- // 但是,如果只有一个显示器的话,其实不用判断,直接返回就行
361
- if (sources.length === 1) {
362
- source = sources[0];
363
- }
364
- else {
365
- source = sources.find(function (item) { return item.display_id === display.id.toString()
366
- || item.id.startsWith("screen:".concat(display.id, ":")); });
367
- }
368
- if (!source) {
369
- this.logger("SCREENSHOTS:capture Can't find screen source. sources: %o, display: %o", sources, display);
370
- throw new Error("Can't find screen source");
371
- }
372
- return [2 /*return*/, source.thumbnail.toDataURL()];
373
- case 6: return [2 /*return*/];
374
- }
375
- });
376
- });
377
- };
378
- /**
379
- * 绑定ipc时间处理
380
- */
381
- Screenshots.prototype.listenIpc = function () {
382
- var _this = this;
383
- /**
384
- * OK事件
385
- */
386
- electron_1.ipcMain.on('SCREENSHOTS:ok', function (e, buffer, data) {
387
- _this.logger('SCREENSHOTS:ok buffer.length %d, data: %o', buffer.length, data);
388
- var event = new event_1.default();
389
- _this.emit('ok', event, buffer, data);
390
- if (event.defaultPrevented) {
391
- return;
392
- }
393
- electron_1.clipboard.writeImage(electron_1.nativeImage.createFromBuffer(buffer));
394
- _this.endCapture();
395
- });
396
- /**
397
- * CANCEL事件
398
- */
399
- electron_1.ipcMain.on('SCREENSHOTS:cancel', function () {
400
- _this.logger('SCREENSHOTS:cancel');
401
- var event = new event_1.default();
402
- _this.emit('cancel', event);
403
- if (event.defaultPrevented) {
404
- return;
405
- }
406
- _this.endCapture();
407
- });
408
- /**
409
- * SAVE事件
410
- */
411
- electron_1.ipcMain.on('SCREENSHOTS:save', function (e, buffer, data) { return __awaiter(_this, void 0, void 0, function () {
412
- var event, time, year, month, date, hours, minutes, seconds, milliseconds, _a, canceled, filePath;
413
- return __generator(this, function (_b) {
414
- switch (_b.label) {
415
- case 0:
416
- this.logger('SCREENSHOTS:save buffer.length %d, data: %o', buffer.length, data);
417
- event = new event_1.default();
418
- this.emit('save', event, buffer, data);
419
- if (event.defaultPrevented || !this.$win) {
420
- return [2 /*return*/];
421
- }
422
- time = new Date();
423
- year = time.getFullYear();
424
- month = (0, padStart_1.default)(time.getMonth() + 1, 2, '0');
425
- date = (0, padStart_1.default)(time.getDate(), 2, '0');
426
- hours = (0, padStart_1.default)(time.getHours(), 2, '0');
427
- minutes = (0, padStart_1.default)(time.getMinutes(), 2, '0');
428
- seconds = (0, padStart_1.default)(time.getSeconds(), 2, '0');
429
- milliseconds = (0, padStart_1.default)(time.getMilliseconds(), 3, '0');
430
- this.$win.setAlwaysOnTop(false);
431
- return [4 /*yield*/, electron_1.dialog.showSaveDialog(this.$win, {
432
- defaultPath: "".concat(year).concat(month).concat(date).concat(hours).concat(minutes).concat(seconds).concat(milliseconds, ".png"),
433
- filters: [
434
- { name: 'Image (png)', extensions: ['png'] },
435
- { name: 'All Files', extensions: ['*'] },
436
- ],
437
- })];
438
- case 1:
439
- _a = _b.sent(), canceled = _a.canceled, filePath = _a.filePath;
440
- if (!this.$win) {
441
- return [2 /*return*/];
442
- }
443
- this.$win.setAlwaysOnTop(true);
444
- if (canceled || !filePath) {
445
- return [2 /*return*/];
446
- }
447
- return [4 /*yield*/, fs_extra_1.default.writeFile(filePath, buffer)];
448
- case 2:
449
- _b.sent();
450
- this.endCapture();
451
- return [2 /*return*/];
452
- }
453
- });
454
- }); });
455
- };
456
- return Screenshots;
457
- }(events_1.default));
458
- exports.default = Screenshots;