]> git.ardis.lu - alu.bz/commitdiff
Use the found wallet provider directly instead of overwriting window.ethereum
authorArdis Lu <ardisglu@gmail.com>
Thu, 15 Feb 2024 07:42:14 +0000 (23:42 -0800)
committerArdis Lu <ardisglu@gmail.com>
Thu, 15 Feb 2024 07:42:14 +0000 (23:42 -0800)
Some wallets hijack window.ethereum and prevent overwrite, e.g. Rainbow Wallet

script.js

index 7a33a5788fcb67feb30f136050d12a140ba79863..b9f6397b598be25a83ed0e1e4f736c8ea95a61ff 100644 (file)
--- a/script.js
+++ b/script.js
@@ -149,7 +149,7 @@ walletPicker.addEventListener('click', e => {
   if (e.target.dataset?.uuid === undefined) {
     return;
   }
-  globalThis.ethereum = wallets.find(wallet => wallet.info.uuid === e.target.dataset.uuid)?.provider ?? globalThis.ethereum;
-  ethereum?.addListener?.('accountsChanged', setAccount) ?? ethereum.on('accountsChanged', setAccount);
-  ethereum.request({ method: 'eth_requestAccounts' }).then(setAccount);
+  const provider = wallets.find(wallet => wallet.info.uuid === e.target.dataset.uuid)?.provider ?? globalThis.ethereum;
+  provider?.addListener?.('accountsChanged', setAccount) ?? provider.on('accountsChanged', setAccount);
+  provider.request({ method: 'eth_requestAccounts' }).then(setAccount);
 });
This page took 0.037191 seconds and 4 git commands to generate.