-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added some error handling for better safety
- Loading branch information
Showing
1 changed file
with
13 additions
and
12 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,28 @@ | ||
// Copied from https://github.com/WalletConnect/web3modal/pull/614/files | ||
// But updated to use newer packages | ||
import { WindowPostMessageStream } from '@metamask/post-message-stream'; | ||
import { initializeProvider } from '@metamask/providers'; | ||
|
||
// Firefox Metamask Hack | ||
// Due to https://github.com/MetaMask/metamask-extension/issues/3133 | ||
const isMetaMaskAvailable = () => { | ||
return typeof window !== 'undefined' && (window.ethereum || window.web3); | ||
}; | ||
|
||
(() => { | ||
if ( | ||
typeof window !== 'undefined' && | ||
!window.ethereum && | ||
!window.web3 && | ||
navigator.userAgent.includes('Firefox') | ||
) { | ||
if (!isMetaMaskAvailable() && navigator.userAgent.includes('Firefox')) { | ||
// setup background connection | ||
const metamaskStream = new WindowPostMessageStream({ | ||
name: 'metamask-inpage', | ||
target: 'metamask-contentscript', | ||
}); | ||
|
||
// this will initialize the provider and set it as window.ethereum | ||
initializeProvider({ | ||
connectionStream: metamaskStream, | ||
shouldShimWeb3: true, | ||
}); | ||
try { | ||
initializeProvider({ | ||
connectionStream: metamaskStream, | ||
shouldShimWeb3: true, | ||
}); | ||
} catch (error) { | ||
console.error('Failed to initialize MetaMask provider:', error); | ||
} | ||
} | ||
})(); |