从这个文档我得到了代码。我没有改变任何东西。但尽管如此,我还是无法运行 Metamask。错误在代码下。 我尝试启用 Metamask 并显示帐户,但出现问题...
<!DOCTYPE html>
<html>
<head>
<script>
document.addEventListener("DOMContentLoaded", function() {
const ethereumButton = document.querySelector(".enableEthereumButton");
const showAccount = document.querySelector(".showAccount");
if (!ethereumButton) {
console.error("The element with class 'enableEthereumButton' was not found.");
return;
}
ethereumButton.addEventListener("click", () => {
getAccount();
});
async function getAccount() {
// Check if MetaMask is installed
if (typeof window.ethereum !== 'undefined') {
try {
// Request account access if needed
const accounts = await window.ethereum.request({ method: 'eth_requestAccounts' });
// Get the first account
const account = accounts[0];
// Display the account
showAccount.innerHTML = account;
} catch (err) {
if (err.code === 4001) {
// EIP-1193 userRejectedRequest error
console.log('Please connect to MetaMask.');
} else {
console.error(err);
}
}
} else {
console.log('MetaMask is not installed!');
}
}
});
</script>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="generator" content="PSPad editor, www.pspad.com">
<title>Ethereum Account</title>
</head>
<body>
<!-- Display a connect button and the current account -->
<button class="enableEthereumButton">Enable Ethereum</button>
<h2>Account: <span class="showAccount"></span></h2>
</body>
</html>
但是在控制台中我看到(元掩码已安装,并且可以在真实托管的 web3 站点中使用元掩码)。
SyntaxError: An invalid or illegal string was specified contentscript.js:1
DOMException: An invalid or illegal string was specified
_postMessage file:///C:/Users/mirek/Downloads/pokus1/index.html:1
_write file:///C:/Users/mirek/Downloads/pokus1/index.html:1
_handshake file:///C:/Users/mirek/Downloads/pokus1/index.html:1
l file:///C:/Users/mirek/Downloads/pokus1/index.html:1
[1]</</< file:///C:/Users/mirek/Downloads/pokus1/index.html:1
[1]</< file:///C:/Users/mirek/Downloads/pokus1/index.html:1
[1]< file:///C:/Users/mirek/Downloads/pokus1/index.html:1
i file:///C:/Users/mirek/Downloads/pokus1/index.html:1
e file:///C:/Users/mirek/Downloads/pokus1/index.html:1
<anonymous> file:///C:/Users/mirek/Downloads/pokus1/index.html:1
MetaMask is not installed!
看起来这个方法不再起作用了:
if (typeof window.ethereum !== 'undefined'){}
通常如果安装了metamask,它会注入
window.ethereum
。在您共享的文档中查看这一点:
const accounts = await provider // Or window.ethereum if you don't support EIP-6963.
似乎新的浏览器支持EIP-6963。我首先检查了
windows.ethereum
并记录了它。然后我升级了浏览器,但现在它是未定义的。
现在从如何实现-eip-6963-support-in-your-web-3-dapp
2023年5月,一群以太坊钱包开发者聚集在一起 建立连接 dapp 的新行业标准。前 常见的做法是钱包公开 EIP-1193 通过网络浏览器中的 window.ethereum 全局对象提供程序。这 技术在早期运行良好,当时用户通常拥有和 连接到一个钱包。
随着生态系统的发展和用户开始使用多个钱包 一时间,dapp 开发者很难正确管理 检测用户连接到哪个钱包或向用户提供哪个钱包的过程 安装多个钱包时选择使用哪个钱包的选项。
2023 年 10 月,该标准现称为 EIP-6963 或 Multi-Wallet 注入的提供商发现已被接受。从此以后,钱包 开发人员已开始引入 EIP-6963 作为默认方式 发现钱包提供商。 MetaMask 也不例外,因为我们已经 确保 MetaMask 钱包 API 支持该标准,并且 MetaMask SDK。