diff --git a/packages/adblocker/src/resources.ts b/packages/adblocker/src/resources.ts index 2a6b15b1c3..e7700b54bf 100644 --- a/packages/adblocker/src/resources.ts +++ b/packages/adblocker/src/resources.ts @@ -314,7 +314,7 @@ export default class Resources { const scriptlet = this.getRawScriptlet(name); if (scriptlet === undefined) { - return undefined; + return this.getSurrogate(name); } let script = this.scriptletsCache.get(scriptlet.name); @@ -333,6 +333,16 @@ export default class Resources { return script; } + private getSurrogate(name: string): string | undefined { + const resource = this.resourcesByName.get(name.endsWith('.js') ? name : `${name}.js`); + + if (resource === undefined || resource.contentType !== 'application/javascript') { + return undefined; + } + + return resource.body; + } + public getScriptletCanonicalName(name: string): string | undefined { return this.getRawScriptlet(name)?.name; } diff --git a/packages/adblocker/test/resources.test.ts b/packages/adblocker/test/resources.test.ts index 0e2af9017d..4e995de10e 100644 --- a/packages/adblocker/test/resources.test.ts +++ b/packages/adblocker/test/resources.test.ts @@ -227,6 +227,14 @@ describe('#Resources', function () { requiresTrust: false, }, ], + resources: [ + { + name: 'surrogate.js', + aliases: [], + body: '(function resource() {})()', + contentType: 'application/javascript', + }, + ], }); }); @@ -272,6 +280,13 @@ describe('#Resources', function () { it('includes setup for scritplet globals', function () { expect(resources.getScriptlet('a')).to.include('var scriptletGlobals = {};'); }); + + it('allows resources surrogate', function () { + // Also, they don't need to be wrapped with scriptlet suppliments like `scriptletGlobals` to avoid possible conflicts. + expect(resources.getScriptlet('surrogate')).to.be.equal('(function resource() {})()'); + // Allow calling surrogate with `.js`. + expect(resources.getScriptlet('surrogate.js')).to.be.equal('(function resource() {})()'); + }); }); context('#getResource', function () {