interledger / web-monetization-extension

An open-source browser extension that enables Web Monetization.
Apache License 2.0
77 stars 6 forks source link

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

Closed sidvishnoi closed 2 months ago

sidvishnoi commented 2 months ago

Context

Fixes https://github.com/interledger/web-monetization-extension/issues/590 Without needing to sacrifice Firefox support!

Changes proposed in this pull request

If browser supports world: MAIN for content script, we inject directly using scripting.registerContentScripts API (can detect WM support immediately on page load). If not, we inject like before from contentScript (slow, requires timeout for WM support detection).

We inject via scripting.registerContentScripts API and not directly via manifest.json, as we can catch the lack of support of world: MAIN via this API. With this detection, we can still support Firefox versions older than 128.

Update polyfill to support injecting twice - only the first injection works - as we detect if already injected.

Note: In content script, we inject polyfill from contentScript and not using scripting.executeScript in background as we'd otherwise need to inject it manually to each tab.

What next?

When Firefox 128 is old enough, we can remove injectPolyfill from background.ts as well as contentScript.ts and register it directly in manifest.json with world: MAIN. When we do that, we'd update browser_specific_settings.gecko.strict_min_version in manifest.json from 110 to 128, and also remove polyfill/* from web_accessible_resources. (https://github.com/interledger/web-monetization-extension/issues/607)

Test results:

Browser WM detection on page load WM detection later Other JS Events
Firefox 126
Firefox 130
Chrome 110

https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/scripting/ExecutionWorld#browser_compatibility

github-actions[bot] commented 2 months ago

Extension builds preview

Name Link
Latest commit 5196937ab20512250e45d0522bca57394bd1d2c3
Latest job logs Run #10904420369
BadgeDownload
BadgeDownload
sidvishnoi commented 2 months ago

The timing issue for Firefox < 128 still exists right?

Yes, but it'll at least continue to work like before. We just won't need to set min supported version to 128 this way.