@@ -29,6 +29,9 @@ class CardExtractorService {
2929 { pattern : / ^ c o n t e n t - c a r d $ / , weight : 1.0 } ,
3030 { pattern : / ^ c o v e r - c a r d $ / , weight : 1.0 } ,
3131 { pattern : / ^ c a r d - c o n t a i n e r $ / , weight : 0.9 } ,
32+ // 添加Pod2Post常用的卡片样式
33+ { pattern : / \b s t y l e [ 1 - 5 ] \b / i, weight : 0.8 } ,
34+ { pattern : / \b s t y l e \d + - \w + / i, weight : 0.8 } ,
3235 { pattern : / \b c a r d \b / i, weight : 0.7 } ,
3336 { pattern : / \b p a n e l \b / i, weight : 0.5 } ,
3437 { pattern : / \b t i l e \b / i, weight : 0.5 } ,
@@ -83,11 +86,11 @@ class CardExtractorService {
8386 browser = await this . launchBrowser ( ) ;
8487 const page = await browser . newPage ( ) ;
8588
86- // 设置视口
89+ // 设置视口(降低像素比以减少资源消耗)
8790 await page . setViewport ( {
8891 width : 1920 ,
8992 height : 1080 ,
90- deviceScaleFactor : 2
93+ deviceScaleFactor : 1 // 降低像素比
9194 } ) ;
9295
9396 // 注入字体样式以确保中文显示
@@ -101,8 +104,17 @@ class CardExtractorService {
101104 } ) ;
102105
103106 // 设置更长的默认导航超时(适应慢速OSS访问)
104- page . setDefaultNavigationTimeout ( 180000 ) ; // 3分钟
105- page . setDefaultTimeout ( 180000 ) ; // 3分钟
107+ page . setDefaultNavigationTimeout ( 300000 ) ; // 5分钟
108+ page . setDefaultTimeout ( 300000 ) ; // 5分钟
109+
110+ // 设置页面的额外HTTP头,可能有助于OSS资源加载
111+ await page . setExtraHTTPHeaders ( {
112+ 'Accept-Language' : 'zh-CN,zh;q=0.9,en;q=0.8' ,
113+ 'Accept' : 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8'
114+ } ) ;
115+
116+ // 增加页面的资源加载设置
117+ await page . setBypassCSP ( true ) ; // 绕过CSP限制
106118
107119 // 监听页面的console输出
108120 page . on ( 'console' , msg => console . log ( '[Browser Console]:' , msg . text ( ) ) ) ;
@@ -140,7 +152,7 @@ class CardExtractorService {
140152 }
141153
142154 let checkCount = 0 ;
143- const maxChecks = 60 ; // 最多检查60次(60秒)
155+ const maxChecks = 120 ; // 增加到120次(120秒),给予充足的图片加载时间
144156
145157 const checkStatus = ( ) => {
146158 checkCount ++ ;
@@ -149,9 +161,10 @@ class CardExtractorService {
149161
150162 console . log ( `Check ${ checkCount } : ${ currentLoaded } loaded, ${ currentErrors } failed, ${ totalImages - currentLoaded - currentErrors } pending` ) ;
151163
152- // 如果所有图片都处理完,或已加载50%,或检查次数达到上限
164+ // 如果所有图片都处理完,或已加载80%(提高要求确保完整加载),或检查次数达到上限
165+ // 对于OSS图片,等待大部分加载完成
153166 if ( currentLoaded + currentErrors >= totalImages ||
154- currentLoaded >= totalImages * 0.5 ||
167+ currentLoaded >= totalImages * 0.8 ||
155168 checkCount >= maxChecks ) {
156169 resolve ( { total : totalImages , loaded : currentLoaded , errors : currentErrors } ) ;
157170 } else {
@@ -163,12 +176,12 @@ class CardExtractorService {
163176 checkStatus ( ) ;
164177 } ) ;
165178 } ) ,
166- // 强制超时保护(考虑到OSS图片加载慢,延长到60秒 )
179+ // 强制超时保护(增加到120秒,确保图片有足够时间加载 )
167180 new Promise ( ( resolve ) => {
168181 setTimeout ( ( ) => {
169- console . log ( '[CardExtractor] Force timeout after 60 seconds' ) ;
182+ console . log ( '[CardExtractor] Force timeout after 120 seconds - proceeding with card detection ' ) ;
170183 resolve ( { total : - 1 , loaded : - 1 , errors : - 1 , timeout : true } ) ;
171- } , 60000 ) ;
184+ } , 120000 ) ;
172185 } )
173186 ] ) ;
174187
@@ -178,9 +191,9 @@ class CardExtractorService {
178191 console . log ( '[CardExtractor] Page load error:' , err . message ) ;
179192 }
180193
181- // 等待一下确保渲染完成
194+ // 等待更长时间确保渲染完成(特别是对于包含大量图片的页面)
182195 console . log ( '[CardExtractor] Waiting for rendering...' ) ;
183- await new Promise ( resolve => setTimeout ( resolve , 2000 ) ) ;
196+ await new Promise ( resolve => setTimeout ( resolve , 5000 ) ) ; // 增加到5秒
184197 console . log ( '[CardExtractor] Rendering wait complete, proceeding to card detection...' ) ;
185198
186199 // 识别卡片
@@ -194,9 +207,9 @@ class CardExtractorService {
194207 console . log ( '[Card Detection] Starting simplified evaluation...' ) ;
195208 const candidates = [ ] ;
196209
197- // 只查找最具体的选择器
198- const cardElements = document . querySelectorAll ( '.card-container, .tutorial-card, .content-card' ) ;
199- console . log ( `[Card Detection] Found ${ cardElements . length } card elements` ) ;
210+ // 只查找最具体的选择器(包含Pod2Post的卡片类型)
211+ const cardElements = document . querySelectorAll ( '.card-container, .tutorial-card, .content-card, .cover-card ' ) ;
212+ console . log ( `[Card Detection] Found ${ cardElements . length } card elements with primary selectors ` ) ;
200213
201214 // 如果没找到,尝试更通用的选择器
202215 let elements = Array . from ( cardElements ) ;
@@ -221,9 +234,12 @@ class CardExtractorService {
221234
222235 const rect = element . getBoundingClientRect ( ) ;
223236
224- // 跳过太小的元素
237+ // 跳过太小的元素(但保留合理的卡片尺寸)
225238 if ( rect . width < 100 || rect . height < 100 ) {
226- return ;
239+ // 特殊处理:如果类名明确包含card,降低尺寸要求
240+ if ( ! / c a r d | c o v e r | c o n t e n t / i. test ( element . className ) ) {
241+ return ;
242+ }
227243 }
228244
229245 // 计算得分
@@ -252,7 +268,7 @@ class CardExtractorService {
252268 if ( element . querySelector ( 'img' ) ) score += 5 ;
253269 if ( element . querySelector ( 'h1, h2, h3, h4, h5, h6' ) ) score += 5 ;
254270
255- if ( score >= 30 ) { // 阈值
271+ if ( score >= 25 ) { // 降低阈值,提高检测灵敏度
256272 candidates . push ( {
257273 score : score ,
258274 className : className ,
@@ -299,12 +315,12 @@ class CardExtractorService {
299315 console . log ( `[Card Detection] Found ${ filtered . length } cards after filtering` ) ;
300316 return filtered ;
301317 } , this . cardPatterns ) ,
302- // 30秒超时保护(给卡片检测更多时间 )
318+ // 120秒超时保护(给予充足时间处理复杂页面和大量图片 )
303319 new Promise ( ( resolve ) => {
304320 setTimeout ( ( ) => {
305- console . log ( '[CardExtractor] Card detection timeout after 30 seconds' ) ;
321+ console . log ( '[CardExtractor] Card detection timeout after 120 seconds' ) ;
306322 resolve ( [ ] ) ;
307- } , 30000 ) ;
323+ } , 120000 ) ;
308324 } )
309325 ] ) ;
310326 } catch ( err ) {
@@ -367,7 +383,7 @@ class CardExtractorService {
367383 async launchBrowser ( ) {
368384 const options = {
369385 headless : 'new' , // 使用新的 headless 模式
370- protocolTimeout : 180000 , // 增加协议超时到3分钟
386+ protocolTimeout : 300000 , // 增加协议超时到5分钟
371387 args : [
372388 '--no-sandbox' ,
373389 '--disable-setuid-sandbox' ,
@@ -398,7 +414,19 @@ class CardExtractorService {
398414 '--memory-pressure-off' ,
399415 '--max_old_space_size=4096' ,
400416 '--max-http-connections=100' ,
401- '--max-http-connections-per-host=20'
417+ '--max-http-connections-per-host=32' , // 增加每个主机的连接数
418+ // 增加资源限制和网络优化,解决ERR_INSUFFICIENT_RESOURCES
419+ '--max-old-space-size=4096' , // 增加V8堆内存到4GB
420+ '--js-flags=--max-old-space-size=4096' ,
421+ '--disable-features=IsolateOrigins' , // 允许跨域
422+ '--disable-site-isolation-trials' ,
423+ '--disable-web-security' , // 允许跨域资源
424+ '--allow-running-insecure-content' ,
425+ '--ignore-certificate-errors' ,
426+ '--ignore-certificate-errors-spki-list' ,
427+ '--enable-features=NetworkService,NetworkServiceInProcess' ,
428+ '--aggressive-cache-discard' ,
429+ '--disable-blink-features=AutomationControlled' // 避免被检测为自动化
402430 ] ,
403431 // 添加更多配置以确保在容器中正常运行
404432 ignoreDefaultArgs : [ '--enable-automation' ] ,
0 commit comments