Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: inject polyfill earlier when world: MAIN is supported #606

Merged
merged 4 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions esbuild/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,6 @@ function processManifestPlugin({
json.background = {
scripts: [json.background.service_worker],
};
json.content_scripts?.forEach((contentScript) => {
// TODO: Remove this when Firefox supports `world` - at least last 10
// versions
contentScript.world = undefined;
});
delete json.minimum_chrome_version;
} else {
delete json['browser_specific_settings'];
Expand Down
25 changes: 25 additions & 0 deletions src/background/services/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export class Background {

async start() {
this.bindOnInstalled();
await this.injectPolyfill();
await this.onStart();
this.heartbeat.start();
this.bindMessageHandler();
Expand All @@ -61,6 +62,30 @@ export class Background {
this.sendToPopup.start();
}

async injectPolyfill() {
try {
// TODO: When Firefox 128 is old enough, inject directly via manifest.
// Also see: injectPolyfill in contentScript
raducristianpopa marked this conversation as resolved.
Show resolved Hide resolved
await this.browser.scripting.registerContentScripts([
{
world: 'MAIN',
id: 'polyfill',
allFrames: true,
js: ['polyfill/polyfill.js'],
matches: PERMISSION_HOSTS.origins,
runAt: 'document_start',
},
]);
} catch (error) {
// Firefox <128 will throw saying world: MAIN isn't supported. So, we'll
// inject via contentScript later. Injection via contentScript is slow,
// but apart from WM detection on page-load, everything else works fine.
if (!error.message.includes(`world`)) {
this.logger.error(error);
raducristianpopa marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

async onStart() {
await this.storage.populate();
await this.checkPermissions();
Expand Down
5 changes: 5 additions & 0 deletions src/content/polyfill.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import type { MonetizationEventPayload } from '@/shared/messages';
(function () {
if (document.createElement('link').relList.supports('monetization')) {
// console.log('already patched');
raducristianpopa marked this conversation as resolved.
Show resolved Hide resolved
return;
}

const handlers = new WeakMap();
const attributes: PropertyDescriptor & ThisType<EventTarget> = {
enumerable: true,
Expand Down
3 changes: 2 additions & 1 deletion src/content/services/contentScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ export class ContentScript {
}

// TODO: When Firefox has good support for `world: MAIN`, inject this directly
// via manifest.json https://bugzilla.mozilla.org/show_bug.cgi?id=1736575
// via manifest.json https://bugzilla.mozilla.org/show_bug.cgi?id=1736575 and
// remove this, along with injectPolyfill from background
async injectPolyfill() {
const document = this.window.document;
const script = document.createElement('script');
Expand Down
4 changes: 2 additions & 2 deletions src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@
"background": {
"service_worker": "background/background.js"
},
"permissions": ["tabs", "storage", "alarms"],
"permissions": ["tabs", "storage", "alarms", "scripting"],
"action": {
"default_title": "Web Monetization",
"default_popup": "popup/index.html"
},
"web_accessible_resources": [
{
"resources": ["assets/*", "polyfill/*"],
"resources": ["polyfill/*"],
"matches": ["<all_urls>"]
}
],
Expand Down