Skip to content

Commit

Permalink
fix: allow scriptlets being used as resources
Browse files Browse the repository at this point in the history
  • Loading branch information
seia-soto committed Oct 26, 2024
1 parent 1c2ad7a commit dafb57d
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion packages/adblocker/src/resources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,10 @@ export default class Resources {
}

public getResource(name: string): { body: string; contentType: string; dataUrl: string } {
const { body, contentType } = this.resourcesByName.get(name) || getResourceForMime(name);
const { body, contentType } =
this.resourcesByName.get(name) ||
this.getScriptletAsResource(name) ||
getResourceForMime(name);

let dataUrl;
if (contentType.indexOf(';') !== -1) {
Expand Down Expand Up @@ -346,6 +349,23 @@ export default class Resources {
return this.scriptletsByName.get(name.endsWith('.js') ? name : `${name}.js`);
}

private getScriptletAsResource(name: string): { body: string; contentType: string } | undefined {
// Do not skip `.js` when the scriptlet is used with `redirect=`, only when used in `+js(...)`.
if (name.endsWith('.js') === false) {
return undefined;
}

const scriptlet = this.scriptletsByName.get(name);
if (scriptlet === undefined) {
return undefined;
}

return {
body: scriptlet.body,
contentType: 'application/javascript',
};
}

private getScriptletDependencies(scriptlet: Scriptlet): string[] {
const dependencies: Map<string, string> = new Map();
const queue: string[] = [...scriptlet.dependencies];
Expand Down

0 comments on commit dafb57d

Please sign in to comment.