Repository navigation
Conversation
Chromium exposes a native `browser` namespace in extension contexts that is a separate object from `chrome`. The preload only patched `chrome`, so extensions reading APIs through `browser` (uBlock Origin, Plasmo-based extensions) bypassed every shim. Point `browser` at the patched `chrome` when it exists. Fixes samuelmaddock#189
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Chromium exposes a native
browsernamespace in extension contexts, and it's a separate object fromchrome(browser === chromeisfalseon Electron 44.1.0, 44.9.3 and 45.0.0).chrome-extension-api.preloadonly patcheschrome, so any extension that reads APIs throughbrowserskips every shim. uBlock Origin doesself.chrome = self.browserinvapi.js, which leaves it withoutchrome.browserActionand with atabs.querythat returns[]. Plasmo-based extensions readbrowser.storage.sync.Change
At the end of
mainWorldScript(), afterchromeis patched and frozen,globalThis.browseris redefined to the same patchedchromeobject. It only does this ifbrowseralready exists, so contexts without it behave as before.mainWorldScript()is the single place that patcheschromefor MV2 background pages (no context isolation), MV3 service workers andchrome-extension://frames, so one alias covers all three.I also added a spec that checks
browser === chromeandbrowser.browserActionin therpcfixture's background page.Verification
yarn buildandyarn test: 65 passing. The new spec fails without the change (expected false to equal true) and passes with it.Ran the sample shell (Electron 44.1.0) with two throwaway extensions that only use
browser.*:runtime.id,tabs.query,storage.localandaction/browserAction. I opened the popups by clicking their toolbar buttons.browser !== chrome,tabs.query→[], nobrowserActionbrowser === chrome, active tab returned,browserActionpresentbrowser !== chrome,tabs.query→[]browser === chrome, active tab returnedbrowser === chrome, all calls succeeduBlock Origin 1.75.0 (chromium zip): before the change, the background page had no
chrome.browserActionand the popup stayed onloading offwith an empty hostname. After the change,chrome.browserActionis present and the popup shows the active tab's hostname (example.com).Not tested: Raycast Companion / other Plasmo extensions, and Electron 45.
Fixes #189