先通过 ethereum?.providerMap 尝试找 MetaMask,如果找不到,返回默认的 ethereum

export default function getEthereum() {
  let ethereum = (window as any).ethereum?.providerMap?.get('MetaMask') || (window as any).ethereum?.providerMap?.get('metamask') || (window as any).ethereum?.providerMap?.get('Metamask')
  if (!ethereum) { 
    ethereum = (window as any).ethereum
  }
  return ethereum
}

使用方法:

const ethereum = getEthereum()

已有 2 条评论

  1. ysh ysh

    请问一下 providerMap undifind咋办呢

    1. Admin Admin

      说明没安装 metamask,如果刚安装,网页需要刷新一下

添加新评论