Skip to content

Commit b1f2640

Browse files
authored
Merge pull request #290 from aselbie/handle-multiple-manifest
Handle scripts with packages from both manifests
2 parents 23fa988 + 43f903a commit b1f2640

File tree

2 files changed

+40
-17
lines changed

2 files changed

+40
-17
lines changed

src/js/contentUtils.ts

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import isPathnameExcluded from './content/isPathNameExcluded';
3333
import {doesWorkerUrlConformToCSP} from './content/doesWorkerUrlConformToCSP';
3434
import {checkWorkerEndpointCSP} from './content/checkWorkerEndpointCSP';
3535
import {MessagePayload} from './shared/MessageTypes';
36+
import {pushToOrCreateArrayInMap} from './shared/nestedDataHelpers';
3637

3738
type ContentScriptConfig = {
3839
checkLoggedInFromCookie: boolean;
@@ -228,25 +229,43 @@ function handleManifestNode(manifestNode: HTMLScriptElement): void {
228229
}
229230

230231
function 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);

src/js/shared/nestedDataHelpers.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,16 @@ export function setOrUpdateSetInMap<OuterKey, Value>(
3131
}
3232
return outerMap;
3333
}
34+
35+
export function pushToOrCreateArrayInMap<OuterKey, Value>(
36+
outerMap: Map<OuterKey, Array<Value>>,
37+
outerKey: OuterKey,
38+
value: Value,
39+
): Map<OuterKey, Array<Value>> {
40+
const innerArray = outerMap.get(outerKey) ?? [];
41+
innerArray.push(value);
42+
if (!outerMap.has(outerKey)) {
43+
outerMap.set(outerKey, innerArray);
44+
}
45+
return outerMap;
46+
}

0 commit comments

Comments
 (0)