@@ -33,6 +33,7 @@ import isPathnameExcluded from './content/isPathNameExcluded';
3333import { doesWorkerUrlConformToCSP } from './content/doesWorkerUrlConformToCSP' ;
3434import { checkWorkerEndpointCSP } from './content/checkWorkerEndpointCSP' ;
3535import { MessagePayload } from './shared/MessageTypes' ;
36+ import { pushToOrCreateArrayInMap } from './shared/nestedDataHelpers' ;
3637
3738type ContentScriptConfig = {
3839 checkLoggedInFromCookie : boolean ;
@@ -228,25 +229,43 @@ function handleManifestNode(manifestNode: HTMLScriptElement): void {
228229}
229230
230231function handleScriptNode ( scriptNode : HTMLScriptElement ) : void {
231- // Need to get the src of the JS
232- let scriptDetails : ScriptDetails ;
233- let version = UNINITIALIZED ;
234-
235232 if ( originConfig . scriptsShouldHaveManifestProp ) {
236233 const dataBtManifest = scriptNode . getAttribute ( 'data-btmanifest' ) ;
237234 if ( dataBtManifest == null ) {
238235 invalidateAndThrow (
239236 `No data-btmanifest attribute found on script ${ scriptNode . src } ` ,
240237 ) ;
241238 }
242- version = dataBtManifest . split ( '_' ) [ 0 ] ;
243- const otherType = dataBtManifest . split ( '_' ) [ 1 ] ;
244- scriptDetails = {
239+
240+ // Scripts may contain packages from both main and longtail manifests,
241+ // e.g. "1009592080_main,1009592080_longtail"
242+ const [ manifest1 , manifest2 ] = dataBtManifest . split ( ',' ) ;
243+
244+ // If this scripts contains packages from both main and longtail manifests
245+ // then require both manifests to be loaded before processing this script,
246+ // otherwise use the single type specified.
247+ const otherType = manifest2 ? BOTH : manifest1 . split ( '_' ) [ 1 ] ;
248+
249+ // It is safe to assume a script will not contain packages from different
250+ // versions, so we can use the first manifest version as the script version.
251+ const version = manifest1 . split ( '_' ) [ 0 ] ;
252+
253+ if ( ! version ) {
254+ invalidateAndThrow (
255+ `Unable to parse a valid version from the data-btmanifest property of ${ scriptNode . src } ` ,
256+ ) ;
257+ }
258+
259+ const scriptDetails = {
245260 src : scriptNode . src ,
246261 otherType,
247262 } ;
263+
248264 ALL_FOUND_SCRIPT_TAGS . add ( scriptNode . src ) ;
265+ pushToOrCreateArrayInMap ( FOUND_SCRIPTS , version , scriptDetails ) ;
249266 } else {
267+ let scriptDetails : ScriptDetails ;
268+
250269 if ( scriptNode . src !== '' ) {
251270 scriptDetails = {
252271 src : scriptNode . src ,
@@ -266,17 +285,8 @@ function handleScriptNode(scriptNode: HTMLScriptElement): void {
266285 otherType : currentFilterType ,
267286 } ;
268287 }
269- }
270288
271- const scriptsForVersion = FOUND_SCRIPTS . get ( version ) ;
272- if ( scriptsForVersion ) {
273- scriptsForVersion . push ( scriptDetails ) ;
274- } else {
275- if ( version != UNINITIALIZED ) {
276- FOUND_SCRIPTS . set ( version , [ scriptDetails ] ) ;
277- } else {
278- FOUND_SCRIPTS . get ( FOUND_SCRIPTS . keys ( ) . next ( ) . value ) ?. push ( scriptDetails ) ;
279- }
289+ FOUND_SCRIPTS . get ( FOUND_SCRIPTS . keys ( ) . next ( ) . value ) ?. push ( scriptDetails ) ;
280290 }
281291
282292 updateCurrentState ( STATES . PROCESSING ) ;
0 commit comments