通过web3.js 1.x访问metamask中的帐户
我想通过web3.js在我的metamask插件中检索当前选定的帐户。而且我想动态地做,所以当切换到另一个帐户时,它应该被打印到UI上。
我正在通过:<script src="https://cdn.jsdelivr.net/gh/ethereum/web3.[email protected]/dist/web3.min.js"></script>"
导入库(beta.37)
让事情变得更复杂: 1)在勇敢的web3.eth.accounts[0]
中,除了我的dApp之外,它会将我的当前地址记录在任何其他站点上,但在这里,它返回“未定义”。2)在Chrome (相同的版本)中,它总是返回undefined。
对于我来说,当其他基于Web3.js0.x构建dApps使用完全相同的代码时,它怎么会返回undefined
呢?
因此,我不能使用以下函数来动态打印当前地址:
var accountInterval = setInterval(function() {
if (web3.eth.accounts[0] !== userAccount) {
userAccount = web3.eth.accounts[0];
}
}, 100);
转载请注明出处:http://www.intsu.net/article/20230518/1918021.html