简体   繁体   中英

Web3 event listener. Check metamask login

So I have this default metamask event listener that checks is a user is connected. I have added an extra function(line 4) that will display the wallet address in a container. However, if I try to append the container with a different message when metamask is not detected, I get a empty container. What am I missing?

window.addEventListener('load', function() {
if (typeof web3 !== 'undefined') {
    web3js = new Web3(web3.currentProvider);
    $('#eth-address').append(web3.eth.accounts[0]) // display address
} else {
    console.log('No web3? You should consider trying MetaMask!')
    web3js = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
    $('#eth-address').append('please connect using metamask')}
startApp() })    

Is this a case where metamask is installed; so there is a provider but the account is locked?

web3 is defined, but then the accounts objects is empty, maybe do a console.log to check what is happening.

If the account is locked then one thing you can do a set an interval to keep checking for the accounts.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM