Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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() })    
like image 986
oContis Studio Avatar asked Jan 23 '26 21:01

oContis Studio


1 Answers

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.

like image 90
Phillip Gibb Avatar answered Jan 26 '26 11:01

Phillip Gibb