Skip to content

Commit

Permalink
fix: inject polyfill earlier when world: MAIN is supported (#606)
Browse files Browse the repository at this point in the history
  • Loading branch information
sidvishnoi authored Sep 17, 2024
1 parent 5df489f commit 2d811da
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 8 deletions.
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
30 changes: 30 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,35 @@ export class Background {
this.sendToPopup.start();
}

// TODO: When Firefox 128 is old enough, inject directly via manifest.
// Also see: injectPolyfill in contentScript
// See: https://github.com/interledger/web-monetization-extension/issues/607
async injectPolyfill() {
try {
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(
`Content script execution world \`MAIN\` not supported by your browser.\n` +
`Check https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/scripting/ExecutionWorld#browser_compatibility for browser compatibility.`,
error,
);
}
}
}

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')) {
// already patched
return;
}

const handlers = new WeakMap();
const attributes: PropertyDescriptor & ThisType<EventTarget> = {
enumerable: true,
Expand Down
4 changes: 3 additions & 1 deletion src/content/services/contentScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ 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
// See: https://github.com/interledger/web-monetization-extension/issues/607
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

0 comments on commit 2d811da

Please sign in to comment.