@@ -2,11 +2,11 @@ import { buildPopupCacheRecord } from './popup-cache'
22import { buildEffectivePageRules , loadDetectorSettings , loadTechRules } from './detector-settings'
33import { mergeTechnologyRecords , shortHeaderUrl } from './merge'
44import { getTabData , getTabSnapshot , updateBadgeForTab , writeTabData } from './tab-store'
5- import { matchesCompiledRulePatterns , matchesRuleTextHints , passesRulePrefilter } from './rule-matcher'
5+ import { matchesCompiledRulePatterns , matchesRuleTextHints } from './rule-matcher'
66import { isDetectablePageUrl } from '@/utils/page-support'
77import { cleanTechnologyUrl } from '@/utils/url'
88
9- const BUNDLE_LICENSE_SCHEMA_VERSION = 2
9+ const BUNDLE_LICENSE_SCHEMA_VERSION = 3
1010const BUNDLE_LICENSE_SOURCE = 'JS 版权注释'
1111const MAX_CANDIDATE_SCRIPTS = 5
1212const MAX_FETCH_BYTES = 384 * 1024
@@ -260,6 +260,8 @@ const trimLicenseText = (text: string): string => {
260260 return text . slice ( 0 , MAX_LICENSE_TEXT_CHARS )
261261}
262262
263+ const looksLikeHtmlDocument = ( text : string ) : boolean => / ^ \s * (?: < ! d o c t y p e \s + h t m l | < h t m l [ \s > ] ) / i. test ( text )
264+
263265const extractLicenseComments = ( source : string ) : string [ ] => {
264266 const comments : string [ ] = [ ]
265267 let commentChars = 0
@@ -298,12 +300,17 @@ const buildSidecarLicenseUrl = (scriptUrl: string): string => {
298300 }
299301}
300302
303+ const fetchSidecarLicenseText = async ( sidecarUrl : string , budget : ScanBudget ) : Promise < string > => {
304+ const text = await fetchLimitedText ( sidecarUrl , MAX_SIDECAR_BYTES , budget )
305+ if ( ! text || looksLikeHtmlDocument ( text ) ) return ''
306+ return text
307+ }
308+
301309const scanScriptLicense = async ( scriptUrl : string , budget : ScanBudget ) : Promise < ScriptLicenseObservation | null > => {
302310 const source = await fetchSampledScriptText ( scriptUrl , budget )
303311 const comments = unique ( source ? extractLicenseComments ( source ) : [ ] )
304312 const sidecarUrl = buildSidecarLicenseUrl ( scriptUrl )
305- const sidecarText =
306- sidecarUrl && comments . length < 12 && hasScanBudget ( budget ) ? await fetchLimitedText ( sidecarUrl , MAX_SIDECAR_BYTES , budget ) : ''
313+ const sidecarText = sidecarUrl && comments . length < 12 && hasScanBudget ( budget ) ? await fetchSidecarLicenseText ( sidecarUrl , budget ) : ''
307314 const text = trimLicenseText ( [ ...comments , sidecarText ] . filter ( Boolean ) . join ( '\n\n' ) )
308315
309316 if ( ! text ) return null
@@ -322,7 +329,7 @@ const detectTechnologiesFromLicenseText = (observations: ScriptLicenseObservatio
322329 for ( const observation of observations ) {
323330 const lowerText = observation . text . toLowerCase ( )
324331 for ( const rule of rules ) {
325- if ( ! rule ?. name || ! passesRulePrefilter ( rule , lowerText ) || ! matchesRuleTextHints ( rule , lowerText ) ) continue
332+ if ( ! rule ?. name || ! matchesRuleTextHints ( rule , lowerText ) ) continue
326333 if ( ! matchesCompiledRulePatterns ( rule , observation . text ) ) continue
327334 technologies . push ( {
328335 category : rule . category || '前端库' ,
0 commit comments