Skip to content

Commit 2d92ae1

Browse files
committed
refactor: minor refactor
1 parent 79d75b3 commit 2d92ae1

1 file changed

Lines changed: 35 additions & 38 deletions

File tree

src/automation/screenshot.js

Lines changed: 35 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,17 @@ const { delay } = require('./utils');
1414
async function execute(webContents, nationalId, company, outputDir, folderStructure = 'by_id', includeCompanyName = false) {
1515
const baseDir = outputDir || path.join(app.getPath('documents'), '投票證明');
1616
const dir = folderStructure === 'flat' ? baseDir : path.join(baseDir, nationalId);
17-
17+
1818
if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
19-
19+
2020
let filename = `${nationalId}_${company.code}`;
2121
if (includeCompanyName && company.name) {
2222
// Sanitize company name for filesystem
2323
const safeName = company.name.replace(/[\\\\/:*?"<>|]/g, '_');
2424
filename += `_${safeName}`;
2525
}
2626
filename += '.png';
27-
27+
2828
const filepath = path.join(dir, filename);
2929

3030
// Use executeJavaScript to scroll the barcode block into view before capturing
@@ -47,48 +47,45 @@ async function execute(webContents, nationalId, company, outputDir, folderStruct
4747
try {
4848
image = await webContents.capturePage();
4949
if (image.isEmpty()) throw new Error('Screenshot empty');
50-
fs.writeFileSync(filepath, image.toPNG());
5150
} catch (err) {
5251
const { BrowserWindow } = require('electron');
5352
const win = BrowserWindow.getAllWindows()[0];
54-
if (win) {
55-
const isMinimized = win.isMinimized();
56-
const isVisible = win.isVisible();
57-
58-
let originalOpacity, originalFocusable;
53+
if (!win) throw err;
54+
55+
const isMinimized = win.isMinimized();
56+
const isVisible = win.isVisible();
57+
58+
let originalOpacity, originalFocusable;
59+
if (isMinimized || !isVisible) {
60+
originalOpacity = win.getOpacity();
61+
originalFocusable = win.isFocusable();
62+
63+
// Prevent stealing focus and make it invisible
64+
win.setOpacity(0);
65+
win.setFocusable(false);
66+
}
67+
68+
if (isMinimized) win.restore();
69+
if (!isVisible) win.showInactive();
70+
71+
// Wait for rendering surface to be allocated
72+
await delay(500);
73+
74+
try {
75+
image = await webContents.capturePage();
76+
if (image.isEmpty()) throw new Error('Still empty');
77+
} finally {
78+
if (isMinimized) win.minimize();
79+
else if (!isVisible) win.hide();
80+
5981
if (isMinimized || !isVisible) {
60-
originalOpacity = win.getOpacity();
61-
originalFocusable = win.isFocusable();
62-
63-
// Prevent stealing focus and make it invisible
64-
win.setOpacity(0);
65-
win.setFocusable(false);
66-
}
67-
68-
if (isMinimized) win.restore();
69-
if (!isVisible) win.showInactive();
70-
71-
// Wait for rendering surface to be allocated
72-
await delay(500);
73-
74-
try {
75-
image = await webContents.capturePage();
76-
if (image.isEmpty()) throw new Error('Still empty');
77-
fs.writeFileSync(filepath, image.toPNG());
78-
} finally {
79-
if (isMinimized) win.minimize();
80-
else if (!isVisible) win.hide();
81-
82-
if (isMinimized || !isVisible) {
83-
win.setOpacity(originalOpacity);
84-
win.setFocusable(originalFocusable);
85-
}
82+
win.setOpacity(originalOpacity);
83+
win.setFocusable(originalFocusable);
8684
}
87-
} else {
88-
throw err;
8985
}
9086
}
91-
87+
88+
fs.writeFileSync(filepath, image.toPNG());
9289
return filepath;
9390
}
9491

0 commit comments

Comments
 (0)