Skip to content

Commit

Permalink
Update inpage-metamask.js
Browse files Browse the repository at this point in the history
Added some error handling for better safety
  • Loading branch information
nikbenesh authored Dec 20, 2024
1 parent ea5f808 commit 4cb2a9a
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions src/vendor/inpage-metamask.js
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);
}
}
})();

0 comments on commit 4cb2a9a

Please sign in to comment.