Skip to content

Commit c9bc2bb

Browse files
committed
fix(automation): standardize waitForNavigation timeout to 30s for better stability
1 parent 825f0da commit c9bc2bb

4 files changed

Lines changed: 15 additions & 15 deletions

File tree

src/automation/login.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ async function execute(webContents, nationalId, sendLog) {
8181
sendLog('[警告] 填寫異常。', 'warning');
8282
}
8383

84-
await waitForNavigation(webContents, 3000);
84+
await waitForNavigation(webContents);
8585

8686
const handleLoginDialog = `
8787
(async () => {
@@ -124,7 +124,7 @@ async function execute(webContents, nationalId, sendLog) {
124124

125125
if (resultStr.startsWith("DOM_MODAL_CLICKED") || resultStr.startsWith("NATIVE_DIALOG_CAPTURED")) {
126126
sendLog('[登入] 偵測提示,已點選。');
127-
await waitForNavigation(webContents, 3000);
127+
await waitForNavigation(webContents);
128128
} else if (resultStr !== "NO_DIALOG_FOUND" && !resultStr.startsWith("ERROR: TIMEOUT") && !resultStr.includes('destroyed')) {
129129
sendLog('[警告] 登入對話框異常', 'warning');
130130
}
@@ -139,7 +139,7 @@ async function execute(webContents, nationalId, sendLog) {
139139
if (currentUrl.includes('login') && !currentUrl.includes('index')) {
140140
sendLog('[警告] 未跳轉,手動導航...', 'warning');
141141
await webContents.loadURL(CONSTANTS.URLS.INDEX);
142-
await delay(3000);
142+
await delay(30000);
143143
currentUrl = webContents.getURL();
144144
if (currentUrl.includes('login') && !currentUrl.includes('index')) return false;
145145
}

src/automation/logout.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,12 @@ async function execute(webContents, sendLog) {
6363

6464
if (result === "SYS_MSG_CLICKED") {
6565
sendLog('[登出] 完成。');
66-
await waitForNavigation(webContents, 3000);
66+
await waitForNavigation(webContents);
6767
return;
6868
}
6969

7070
sendLog('[登出] 已觸發,待跳轉...');
71-
await waitForNavigation(webContents, 5000);
71+
await waitForNavigation(webContents);
7272

7373
const checkFinalScript = `
7474
(() => {
@@ -83,7 +83,7 @@ async function execute(webContents, sendLog) {
8383
const isFinalClicked = await safeExecute(webContents, checkFinalScript, 2000);
8484
if (isFinalClicked === true) {
8585
sendLog('[登出] 確認完成。');
86-
await waitForNavigation(webContents, 3000);
86+
await waitForNavigation(webContents);
8787
await randomDelay(200, 400);
8888
}
8989
}

src/automation/utils.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ function randomDelay(min = 400, max = 800) {
3535
* bottleneck the script.
3636
*
3737
* @param {object} webContents The Electron WebContents instance
38-
* @param {number} timeoutMs Maximum time to wait before timing out (defaults to 10s)
38+
* @param {number} timeoutMs Maximum time to wait before timing out (defaults to 30s)
3939
* @returns {Promise<boolean>} True if loaded successfully, false if timed out
4040
*/
41-
function waitForNavigation(webContents, timeoutMs = 10000) {
41+
function waitForNavigation(webContents, timeoutMs = 30000) {
4242
return new Promise((resolve) => {
4343
let resolved = false;
4444

@@ -102,16 +102,16 @@ function isScreenshotExists(nationalId, company, outputDir, folderStructure = 'b
102102
if (!fs.existsSync(dir)) return false;
103103

104104
const code = typeof company === 'string' ? company : company.code;
105-
105+
106106
// Performance: cache directory file list per execution loop
107107
if (!dirCache.has(dir)) {
108108
dirCache.set(dir, fs.readdirSync(dir));
109109
// Clear cache after a short delay to ensure fresh data for next run but fast for current loop
110110
setTimeout(() => dirCache.delete(dir), 5000);
111111
}
112-
112+
113113
const files = dirCache.get(dir);
114-
114+
115115
// Fuzzy match: check if segments (split by _) match both nationalId AND company code exactly
116116
return files.some(file => {
117117
if (!file.endsWith('.png')) return false;

src/automation/voting.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ async function getCompanyList(webContents, sendLog) {
9393
hasNextPage = pageData.hasNext;
9494
if (hasNextPage) {
9595
pageNum++;
96-
await waitForNavigation(webContents, 6000);
96+
await waitForNavigation(webContents);
9797
await randomDelay(1500, 2500);
9898
}
9999
}
@@ -194,7 +194,7 @@ async function voteForCompany(webContents, company, sendLog, skipClick = false,
194194
})()
195195
`;
196196

197-
const waitNext = waitForNavigation(webContents, 10000);
197+
const waitNext = waitForNavigation(webContents);
198198
const result = await webContents.executeJavaScript(pageScript);
199199
if (!result.success) throw new Error(result.reason || '頁面處理失敗');
200200

@@ -255,7 +255,7 @@ async function searchAndNavigate(webContents, stockCode, sendLog) {
255255

256256
for (let i = 0; i < 20; i++) {
257257
await delay(500);
258-
const waitSearchNav = waitForNavigation(webContents, 8000);
258+
const waitSearchNav = waitForNavigation(webContents);
259259
const linkResult = await webContents.executeJavaScript(`
260260
(() => {
261261
const rows = Array.from(document.querySelectorAll('tr')).filter(row => row.innerText.includes('${stockCode}'));
@@ -311,7 +311,7 @@ async function navigateBackToList(webContents, sendLog) {
311311
})()
312312
`;
313313

314-
const waitP = waitForNavigation(webContents, 15000);
314+
const waitP = waitForNavigation(webContents);
315315
try {
316316
const clickedBack = await webContents.executeJavaScript(returnListScript);
317317
if (!clickedBack) {

0 commit comments

Comments
 (0)