我正在制作一个需要 MetaMask 身份验证的 Chrome 扩展。
我开始将其开发为 Web 应用程序,但作为 chrome 扩展,它无法检测 MetaMask...
这是我现在的网络应用程序代码:
function toggleButton() {
const loginButton = document.getElementById("login-button");
if (!window.ethereum) {
loginButton.innerText = "Install MetaMask";
loginButton.addEventListener("click", () => {
window.open("https://metamask.io");
});
return false;
}
loginButton.addEventListener("click", loginWithMetaMask);
}
async function loginWithMetaMask() {
const accounts = await window.ethereum.request({ method: "eth_requestAccounts" }).catch((e) => {
console.error(e.message);
return;
});
if (!accounts) {
return;
}
const userWallet = document.getElementById("user-wallet");
userWallet.innerText = accounts[0];
}
window.addEventListener("DOMContentLoaded", toggleButton);
Run Code Online (Sandbox Code Playgroud)
有没有办法将 Web 应用程序迁移到 Chrome 扩展程序?