metamask-extension
metamask-extension copied to clipboard
Fix #14846 - Inject provider for MV3 via app-init
Supercedes https://github.com/MetaMask/metamask-extension/pull/15262
Explanation
If we use the src of <script> to inject our inpage provider, the code is async and won't be initialized immediately in the page. By using the new world: MAIN property for content scripts, we can ensure the script is injected at the correct time.
More Information
- Fixes #14846
Manual Testing Steps
- Start via
yarn start:mv3 - Refresh the extension in the Extension Manager, which ensures flushing cache
- Open the test dapp
- Type
window.ethereumin the console, ensure it's there - Use the test dapp buttons, ensure the popup appears
Pre-Merge Checklist
- [ ] PR template is filled out
- [ ] IF this PR fixes a bug, a test that would have caught the bug has been added
- [ ] PR is linked to the appropriate GitHub issue
- [ ] PR has been added to the appropriate release Milestone
+ If there are functional changes:
- [ ] Manual testing complete & passed
- [ ] "Extension QA Board" label has been applied
CLA Signature Action: All authors have signed the CLA. You may need to manually re-run the blocking PR check if it doesn't pass in a few minutes.
One difference with this method is that we don't use shouldInjectProvider which does various checks for Doctype, host, etc. Not sure if we'll find that perfectly acceptable.
One difference with this method is that we don't use shouldInjectProvider which does various checks for Doctype, host, etc. Not sure if we'll find that perfectly acceptable.
We can move that check within inpage.js itself. If it's run first-thing, even before the imports, it should let us stop execution early at least.
I'm not sure if that covers all the reasons we had that check though. I know the hard-coded domains listed there had incompatibilities with our inpage script (probably relying on globals or doing some sloppy message handling), so moving the check to inpage.js should do the trick there. But for the suffix check I'm less sure. I think that was to prevent injecting into non-web contexts like PDFs, but... maybe the contentscript just ignores those so we don't need that check anymore.
Builds ready [053cd67]
- builds: chrome, firefox, opera
- builds (beta): chrome, firefox, opera
- builds (flask): chrome, firefox, opera
- build viz: Build System
- mv3: Background Module Init Stats
- mv3: UI Init Stats
- mv3: Module Load Stats
- mv3: Bundle Size Stats
- code coverage: Report
- storybook: Storybook
- all artifacts
Page Load Metrics (1672 ± 68 ms)
| Platform | Page | Metric | Min (ms) | Max (ms) | Average (ms) | StandardDeviation (ms) | MarginOfError (ms) |
|---|---|---|---|---|---|---|---|
| Chrome | Home | firstPaint | 81 | 160 | 107 | 24 | 11 |
| domContentLoaded | 1515 | 2069 | 1651 | 147 | 71 | ||
| load | 1516 | 2069 | 1672 | 141 | 68 | ||
| domInteractive | 1515 | 2069 | 1651 | 147 | 71 |
Builds ready [f6bf5de]
- builds: chrome, firefox, opera
- builds (beta): chrome, firefox, opera
- builds (flask): chrome, firefox, opera
- build viz: Build System
- mv3: Background Module Init Stats
- mv3: UI Init Stats
- mv3: Module Load Stats
- mv3: Bundle Size Stats
- code coverage: Report
- storybook: Storybook
- all artifacts
Page Load Metrics (1693 ± 49 ms)
| Platform | Page | Metric | Min (ms) | Max (ms) | Average (ms) | StandardDeviation (ms) | MarginOfError (ms) |
|---|---|---|---|---|---|---|---|
| Chrome | Home | firstPaint | 92 | 169 | 109 | 18 | 9 |
| domContentLoaded | 1541 | 1923 | 1691 | 99 | 47 | ||
| load | 1541 | 1943 | 1693 | 101 | 49 | ||
| domInteractive | 1541 | 1923 | 1691 | 99 | 47 |
Builds ready [c5afdfa]
- builds: chrome, firefox, opera
- builds (beta): chrome, firefox, opera
- builds (flask): chrome, firefox, opera
- build viz: Build System
- mv3: Background Module Init Stats
- mv3: UI Init Stats
- mv3: Module Load Stats
- mv3: Bundle Size Stats
- code coverage: Report
- storybook: Storybook
- all artifacts
Page Load Metrics (2205 ± 121 ms)
| Platform | Page | Metric | Min (ms) | Max (ms) | Average (ms) | StandardDeviation (ms) | MarginOfError (ms) |
|---|---|---|---|---|---|---|---|
| Chrome | Home | firstPaint | 100 | 2519 | 258 | 519 | 249 |
| domContentLoaded | 1738 | 2563 | 2184 | 237 | 114 | ||
| load | 1738 | 2682 | 2205 | 252 | 121 | ||
| domInteractive | 1738 | 2563 | 2184 | 237 | 114 |
Builds ready [5ed15fc]
- builds: chrome, firefox, opera
- builds (beta): chrome, firefox, opera
- builds (flask): chrome, firefox, opera
- build viz: Build System
- mv3: Background Module Init Stats
- mv3: UI Init Stats
- mv3: Module Load Stats
- mv3: Bundle Size Stats
- code coverage: Report
- storybook: Storybook
- all artifacts
Page Load Metrics (1789 ± 79 ms)
| Platform | Page | Metric | Min (ms) | Max (ms) | Average (ms) | StandardDeviation (ms) | MarginOfError (ms) |
|---|---|---|---|---|---|---|---|
| Chrome | Home | firstPaint | 93 | 171 | 128 | 20 | 10 |
| domContentLoaded | 1605 | 2409 | 1767 | 161 | 78 | ||
| load | 1621 | 2409 | 1789 | 164 | 79 | ||
| domInteractive | 1605 | 2408 | 1767 | 161 | 78 |
Builds ready [76c22ec]
- builds: chrome, firefox, opera
- builds (beta): chrome, firefox, opera
- builds (flask): chrome, firefox, opera
- build viz: Build System
- mv3: Background Module Init Stats
- mv3: UI Init Stats
- mv3: Module Load Stats
- mv3: Bundle Size Stats
- code coverage: Report
- storybook: Storybook
- all artifacts
Page Load Metrics (1831 ± 95 ms)
| Platform | Page | Metric | Min (ms) | Max (ms) | Average (ms) | StandardDeviation (ms) | MarginOfError (ms) |
|---|---|---|---|---|---|---|---|
| Chrome | Home | firstPaint | 95 | 142 | 110 | 13 | 6 |
| domContentLoaded | 1606 | 2384 | 1796 | 180 | 86 | ||
| load | 1606 | 2384 | 1831 | 197 | 95 | ||
| domInteractive | 1606 | 2384 | 1796 | 180 | 86 |
Builds ready [61a7ecf]
- builds: chrome, firefox, opera
- builds (beta): chrome, firefox, opera
- builds (flask): chrome, firefox, opera
- build viz: Build System
- mv3: Background Module Init Stats
- mv3: UI Init Stats
- mv3: Module Load Stats
- mv3: Bundle Size Stats
- code coverage: Report
- storybook: Storybook
- all artifacts
Page Load Metrics (1756 ± 46 ms)
| Platform | Page | Metric | Min (ms) | Max (ms) | Average (ms) | StandardDeviation (ms) | MarginOfError (ms) |
|---|---|---|---|---|---|---|---|
| Chrome | Home | firstPaint | 91 | 241 | 124 | 35 | 17 |
| domContentLoaded | 1604 | 1966 | 1740 | 89 | 43 | ||
| load | 1622 | 1989 | 1756 | 97 | 46 | ||
| domInteractive | 1604 | 1966 | 1740 | 89 | 43 |
Builds ready [f0a4e3a]
- builds: chrome, firefox, opera
- builds (beta): chrome, firefox, opera
- builds (flask): chrome, firefox, opera
- build viz: Build System
- mv3: Background Module Init Stats
- mv3: UI Init Stats
- mv3: Module Load Stats
- mv3: Bundle Size Stats
- code coverage: Report
- storybook: Storybook
- all artifacts
Page Load Metrics (1738 ± 59 ms)
| Platform | Page | Metric | Min (ms) | Max (ms) | Average (ms) | StandardDeviation (ms) | MarginOfError (ms) |
|---|---|---|---|---|---|---|---|
| Chrome | Home | firstPaint | 87 | 159 | 114 | 18 | 9 |
| domContentLoaded | 1591 | 2097 | 1732 | 126 | 60 | ||
| load | 1591 | 2097 | 1738 | 123 | 59 | ||
| domInteractive | 1591 | 2097 | 1732 | 126 | 60 |
Builds ready [61a7ecf]
- builds: chrome, firefox, opera
- builds (beta): chrome, firefox, opera
- builds (flask): chrome, firefox, opera
- build viz: Build System
- mv3: Background Module Init Stats
- mv3: UI Init Stats
- mv3: Module Load Stats
- mv3: Bundle Size Stats
- code coverage: Report
- storybook: Storybook
- all artifacts
Page Load Metrics (1655 ± 46 ms)
| Platform | Page | Metric | Min (ms) | Max (ms) | Average (ms) | StandardDeviation (ms) | MarginOfError (ms) |
|---|---|---|---|---|---|---|---|
| Chrome | Home | firstPaint | 90 | 123 | 104 | 11 | 5 |
| domContentLoaded | 1523 | 1947 | 1651 | 99 | 47 | ||
| load | 1541 | 1947 | 1655 | 96 | 46 | ||
| domInteractive | 1523 | 1947 | 1651 | 99 | 47 |
Amazing! I've tested and overall looks great! :100: Just one thing, I noticed a slightly strange behaviour, that I am not sure if might be on the Dapp side (meaning Dapps should adapts something on their code?) or on our side. Basically, when I stop service worker and re-start again, after unlocking MM I can see both on MM and on the Dapp that I am still Connected to the Dapp. Even though, when I continue triggering actions on the Dapp nothing happens. It is not until I refresh the Dapp that I can continue operating normally. Is this the expected behaviour? @darkwing @naugtur
https://user-images.githubusercontent.com/54408225/183867630-4abdd2a3-357b-4b14-9f54-1c990f756f5b.mp4
It's not expected. Something didn't reconnect. But I'm not familiar with that logic, I'll leave figuring it out to someone more qualified 😉 Thanks for finding it!
@seaona @naugtur This is expected behavior at the moment because we need to reconnect the streams when the Service Worker restarts.
This is the issue related here: https://github.com/MetaMask/metamask-extension/issues/14845 I have a draft PR attempting to fix this: https://github.com/MetaMask/metamask-extension/pull/15494
great! so for QA side this PR is looking good! @darkwing I will check the fix on the other PR once ready @digiwand Thank you all!!
Builds ready [c46fc39]
- builds: chrome, firefox, opera
- builds (beta): chrome, firefox, opera
- builds (flask): chrome, firefox, opera
- build viz: Build System
- mv3: Background Module Init Stats
- mv3: UI Init Stats
- mv3: Module Load Stats
- mv3: Bundle Size Stats
- mv2: E2e Actions Stats
- code coverage: Report
- storybook: Storybook
- typescript migration: Dashboard
- all artifacts
Page Load Metrics (1866 ± 72 ms)
| Platform | Page | Metric | Min (ms) | Max (ms) | Average (ms) | StandardDeviation (ms) | MarginOfError (ms) |
|---|---|---|---|---|---|---|---|
| Chrome | Home | firstPaint | 88 | 2219 | 299 | 582 | 279 |
| domContentLoaded | 1680 | 2228 | 1837 | 137 | 66 | ||
| load | 1703 | 2355 | 1866 | 150 | 72 | ||
| domInteractive | 1680 | 2228 | 1837 | 137 | 66 |
Builds ready [c46fc39]
- builds: chrome, firefox, opera
- builds (beta): chrome, firefox, opera
- builds (flask): chrome, firefox, opera
- build viz: Build System
- mv3: Background Module Init Stats
- mv3: UI Init Stats
- mv3: Module Load Stats
- mv3: Bundle Size Stats
- mv2: E2e Actions Stats
- code coverage: Report
- storybook: Storybook
- typescript migration: Dashboard
- all artifacts
Page Load Metrics (1795 ± 39 ms)
| Platform | Page | Metric | Min (ms) | Max (ms) | Average (ms) | StandardDeviation (ms) | MarginOfError (ms) |
|---|---|---|---|---|---|---|---|
| Chrome | Home | firstPaint | 90 | 189 | 113 | 25 | 12 |
| domContentLoaded | 1595 | 1957 | 1765 | 92 | 44 | ||
| load | 1631 | 1957 | 1795 | 82 | 39 | ||
| domInteractive | 1595 | 1957 | 1765 | 92 | 44 |