File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -14,13 +14,20 @@ const { delay } = require('./utils');
1414function formatFilename ( pattern , nationalId , company ) {
1515 if ( ! pattern ) pattern = '{id}_{code}' ;
1616
17- // Sanitize company name for filesystem (remove \/:*?"<>| and control characters)
18- const safeName = ( company . name || '' ) . replace ( / [ \\ \\ / : * ? " < > | \\ x 0 0 -\\ x 1 F \\ x 7 F ] / g, '_' ) ;
17+ // Sanitize company name: replace forbidden chars and whitespace with underscores
18+ let safeName = ( company . name || '' )
19+ . trim ( )
20+ . replace ( / [ \\ / : * ? " < > | \t \n \r \f \v \x00 - \x1F \x7F ] / g, '_' ) ;
21+
22+ // Replace multiple internal underscores with a single one for cleaner names
23+ safeName = safeName . replace ( / _ + / g, '_' ) . replace ( / ^ _ + | _ + $ / g, '' ) ;
24+
25+ const nameToUse = safeName || 'noname' ;
1926
2027 return pattern
21- . replace ( / { i d } / g, nationalId )
22- . replace ( / { c o d e } / g, company . code )
23- . replace ( / { n a m e } / g, safeName ) ;
28+ . replace ( / { i d } / g, nationalId || 'unknown' )
29+ . replace ( / { c o d e } / g, company . code || 'unknown' )
30+ . replace ( / { n a m e } / g, nameToUse ) ;
2431}
2532
2633/**
Original file line number Diff line number Diff line change @@ -26,9 +26,27 @@ async function getCompanyList(webContents, sendLog) {
2626 const cells = row.querySelectorAll('td');
2727 if (cells.length < 1) return null;
2828
29- const companyInfo = cells[0].innerText.trim().split(/\\s+/);
30- const code = companyInfo[0];
31- const name = companyInfo.length > 1 ? companyInfo.slice(1).join(' ') : '未知公司';
29+ // Prefer hidden input for accuracy (handles English names better)
30+ // Search row-wide for the input
31+ const nameInput = row.querySelector('input[id^="stockName_"]');
32+ let code, name;
33+
34+ if (nameInput) {
35+ name = nameInput.value.trim();
36+ code = nameInput.id.replace('stockName_', '');
37+ } else {
38+ const text = cells[0].innerText.trim();
39+ // Match at least 4 digits at start, then optional separator, then name
40+ const match = text.match(/^(\\d{4,})(?:\\s+|(?=[^\\d]))(.*)$/s);
41+ if (match) {
42+ code = match[1];
43+ name = match[2].trim() || '未知公司';
44+ } else {
45+ const companyInfo = text.split(/\\s+/);
46+ code = companyInfo[0];
47+ name = companyInfo.length > 1 ? companyInfo.slice(1).join(' ') : '未知公司';
48+ }
49+ }
3250
3351 const links = Array.from(row.querySelectorAll('a.c-actLink, a.u-link'));
3452 const hasVote = links.some(a => ['投票', 'Vote'].some(kw => a.innerText.includes(kw)));
You can’t perform that action at this time.
0 commit comments