6

I am making a chrome extension that requires MetaMask authentication.
I started developing it as a web application, but as a chrome extension, it doesn't detect MetaMask...

This is my web application code right now:

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);

Is there a way to migrate the web application to a chrome extension?

TylerH
  • 20,799
  • 66
  • 75
  • 101
RonKon
  • 133
  • 8
  • Did you solve this? – emorling Jan 26 '22 at 23:33
  • No, I don't think it is possible for an extension to integrate with other extensions. – RonKon Jan 30 '22 at 11:56
  • It must be possible as there are cases when it is working I think. One of those cases is ERC Sniper extension for NFTs. But I've been struggling to do the same thing. Will add answer if I figure this out anytime soon. – D.Weltrowski Feb 05 '22 at 19:26

2 Answers2

1

This is how to do it.

  1. Install metamask-extension-provider in your project https://github.com/MetaMask/extension-provider You will need to use browserify to compile your plugin.

  2. Create a custom connector like this

    class customConnector extends Moralis.AbstractWeb3Connector {
        async activate() {
        const provider = createMetaMaskProvider();
        if (!provider) {
          console.error("MetaMask provider not detected.");
          throw new Error("MetaMask provider not detected.");
        }
        const [accounts, chainId] = await Promise.all([
          provider.request({
            method: 'eth_requestAccounts',
          }),
          provider.request({ method: 'eth_chainId' }),
        ]);
        const account = accounts[0] ? accounts[0].toLowerCase() : null;
    
        this.chainId = provider.chainId;
        this.account = provider.selectedAddress;
        this.provider = provider;
    
        this.subscribeToEvents(provider);
    
        return { provider, chainId, account };
      }
    }
    
  3. Now you can authenticate using MetaMask like this

    Moralis.authenticate({ connector: customConnector })
    
TylerH
  • 20,799
  • 66
  • 75
  • 101
emorling
  • 147
  • 1
  • 2
  • 11
-1

I have created a small boilerplate of chrome extension with react and added metamask login and web3 support.

https://github.com/shaheem-khanzada/chrome-extension-react-metamask-boilerplate

so the problem is we cannot access window.eth... directly in the extension that is why metamask created a module called metamask-extension-provider but in order to run this we need to use browserify compiler so in simple words, we need to write our own compiler that's why I created this boilerplate to help others it uses gulp to create task and then bundle files using browserify and babelify

Muhammad Shaheem
  • 607
  • 5
  • 14
  • A link to a solution is welcome, but please ensure your answer is useful without it: [add context around the link](//meta.stackexchange.com/a/8259) so your fellow users will have some idea what it is and why it is there, then quote the most relevant part of the page you are linking to in case the target page is unavailable. [Answers that are little more than a link may be deleted.](/help/deleted-answers) – jmoerdyk Apr 20 '22 at 17:15