I'm trying to get the current wallet address of MetaMask on my Firefox 67 but I keep getting nothing (empty array), except it says my MetMask is locked. But on Chrome 74 it works. FYI, my MetaMask is NOT locked at all.
window.addEventListener('load', function() {
// Check if Web3 has been injected by the browser:
if (typeof web3 !== 'undefined') {
// You have a web3 browser! Continue below!
web3 = new Web3(web3.currentProvider);
var version = web3.version;
//Get wallet info in the form of Javascript object
var account = web3.eth.accounts;
//Get the current MetaMask selected/active wallet
walletAddress = account.givenProvider.selectedAddress;
//Check if Metamask is locked
if (!empty(walletAddress)){
//Detect if the user changes the account on MetaMask
window.ethereum.on('accountsChanged', function (accounts) {
console.log("MetaMask account change. Reloading...");
window.location.reload();
})
//If not locked, continue to run the app
startApp(web3);
} else {
//If locked, alert user to unlock their MetaMask
if (alert("<?php _e('Your MetaMask is locked. Please unlock with your password and then press OK below.', 'dc-woocommerce-multi-vendor'); ?>")){
} else {
window.location.reload();
}
}
} else {
if (alert("<?php _e('No MetaMask plugin detected. Please install MetaMask digital wallet at www.metamask.io', 'dc-woocommerce-multi-vendor'); ?>")){
} else {
window.location.reload();
}
}
});
I also tried the two options below but failed.
var accounts = web3.eth.getAccounts(function(error, result) {
if (error) {
console.log(error);
} else {
console.log(result);
}
});
var account = web3.eth.accounts[0];
var accountInterval = setInterval(function() {
if (web3.eth.accounts[0] !== account) {
account = web3.eth.accounts[0];
window.location.reload();
}
}, 100);