Created
March 30, 2023 13:06
-
-
Save Brookinator/eec49e32e313e61a05e75eb43f8b8088 to your computer and use it in GitHub Desktop.
JjNaPEO
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
<button id="onboard">Loading...</button> |
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
import MetamaskOnboarding from "https://cdn.skypack.dev/@metamask/onboarding@1.0.1"; | |
window.addEventListener('DOMContentLoaded', () => { | |
const onboarding = new MetamaskOnboarding(); | |
const onboardButton = document.getElementById('onboard'); | |
let accounts; | |
const updateButton = () => { | |
if (!MetamaskOnboarding.isMetaMaskInstalled()) { | |
onboardButton.innerText = 'Click here to install MetaMask!'; | |
onboardButton.onclick = () => { | |
onboardButton.innerText = 'Onboarding in progress'; | |
onboardButton.disabled = true; | |
onboarding.startOnboarding(); | |
}; | |
} else if (accounts && accounts.length > 0) { | |
onboardButton.innerText = 'Connected'; | |
onboardButton.disabled = true; | |
onboarding.stopOnboarding(); | |
} else { | |
onboardButton.innerText = 'Connect'; | |
onboardButton.onclick = async () => { | |
await window.ethereum.request({ | |
method: 'eth_requestAccounts', | |
}); | |
}; | |
} | |
}; | |
updateButton(); | |
if (MetamaskOnboarding.isMetaMaskInstalled()) { | |
window.ethereum.on('accountsChanged', (newAccounts) => { | |
accounts = newAccounts; | |
updateButton(); | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment