MetaMask / metamask-extension

:globe_with_meridians: :electric_plug: The MetaMask browser extension enables browsing Ethereum blockchain enabled websites

Home Page:https://metamask.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Bug]: Chromium browser extension is not injecting/initializing window.ethereum on clean start, related to security permissions?

vlrevolution opened this issue · comments

Describe the bug

Are you aware that on Chromium browsers when a clean start is performed and a dapp site opened straight away, the MetaMask addon does not work at all and requires a refresh to that tab to finally be registered by any Web3 site. Basically you can test/reproduce this yourself on for example the Doodles homepage: https://doodles.app/ Make sure you do a complete fresh start of a Chromium browser and open that url right away (this is important, you have to open dapp as quickly as possible after browser startup). When you then try to go and click on the Connect > MetaMask, absolutely nothing happens. If you then still sign into MetaMask, site still doesn't register it. You have to refresh the page to get it to show up on the web3 site.

Steps to reproduce

  1. you can test/reproduce this yourself on for example the Doodles homepage: https://doodles.app/ Make sure you do a complete fresh start (force close it from task manager first) of the Chromium browser (use edge for example). When you then try to go and click on the Connect > MetaMask, absolutely nothing happens. If you then still sign into MetaMask, site still doesn't register it. You have to refresh the page to get it to show up on the web3 site.

Error messages or log output

No response

Version

10.11.3

Build type

No response

Browser

Chrome, Microsoft Edge

Operating system

Windows

Hardware wallet

No response

Additional context

No response

Looks like it depends on how fast you open the dapp website after browser start.. if you wait after the start of the browser and then open the site then it works but if you open dapp site right away, it is not working until a window refresh..

People who open a dapp site from a link in Discord for example, without prior having the browser loaded, will be greeted with a broken dapp until they refresh that browser tab - and no notification about the plugin being the culprit here. Will seem like the website itself is broken

Looks like it depends on how fast you open the dapp website after browser start.. if you wait after the start of the browser and then open the site then it works but if you open dapp site right away, it is not working until a window refresh..

People who open a dapp site from a link in Discord for example, without prior having the, will be greeted with a broken dapp until they restart - and no notification about the plugin being the culprit here. Will seem like the website itself is broken

I can confirm this. It happens to me too. If I start chromium and open a metamask-enabled dapp right away it will stop working until I refresh the page.

Using svelte-web3 this is how you can fix it for now:

  // Use this function on:click of connect button
  async function connectMetmask() {
    if (window.ethereum) {
      // Check that MetaMask is present & not glitched
      if (!window.ethereum._state.isConnected && browser) {
        // Page was opened too fast after start-up, MetaMask is glitched, setting marker & then reloading..
        sessionStorage.setItem('shouldTriggerMetamask', JSON.stringify(true));
        // Marker set so let's refresh page and then key in sessionStorage will be read as soon as defaultEvmStores
        // is ready and then reactive function below will re-trigger MetaMask sign-in

        // We have to wait a little bit here (1000 ms seems enough from initial testing) for MetaMask to
        // initialize and inject itself into the chromium.. otherwise it will still be glitched after the
        // forced reload as all of this (the reload and re-trigger) happens really fast
        // I would recommend to show modal with a loader here telling them to:
        // 'Please wait while MetaMask is initializing..'
        setTimeout(() => {
          window.location.reload();
        }, 1000);
        return;
      }
      // If all is good & MetaMask not glitched, we can trigger MetaMask popup to sign-in
      defaultEvmStores.setBrowserProvider();
      // I would show modal with a loader here tellin them to:
      // 'Please authorize the MetaMask sign in to continue..'
    } else {
      // I would advise to show modal with an error saying:
      // 'No wallet plugin detected'
    }
  }

  // Reactive function that will run when defaultEvmStores is ready
  $: if (defaultEvmStores && browser) {
    // Read the sessionStorage value to check if we should trigger MetaMask popup for sign-in
    const shouldTriggerMetamask = JSON.parse(sessionStorage.getItem('shouldTriggerMetamask'));
    if (shouldTriggerMetamask) {
      // Set the sessionStorage key to false so next reload doesn't trigger again
      sessionStorage.setItem('shouldTriggerMetamask', JSON.stringify(false));
      // Trigger MetaMask popup to sign-in
      defaultEvmStores.setBrowserProvider();
      // I would show modal with a loader here tellin them to:
      // 'Please authorize the MetaMask sign in to continue..'
    }
  }

I'm also seeing this. Although, this is happening to me also. If I'm not already logged into my account I need to refresh my browser. Although, this doesn't happen all the time. Seems to be more intermittent.

It happens 100% of the time if you don't have browser opened and click a link from Discord.. so it depends on the time passed since browser was started - seems metamask will not be ready if dApp starts as soon as browser starts and it will bug out until you refresh..

Same issue with a lot of users complaining about this, has there been an update?

Happy new year, any fix for this yet?

This issue has been automatically marked as stale because it has not had recent activity in the last 90 days. It will be closed in 45 days if there is no further activity.\n\nThe MetaMask team intends on reviewing this issue before close, and removing the stale label if it is still a bug. We welcome new comments on this issue. We do not intend on closing issues if they report bugs that are still reproducible.\n\nThank you for your contributions.

This issue was closed because there has been no follow up activity in the last 45 days. If you feel this was closed in error, please reopen and provide evidence on the latest release of the extension. Thank you for your contributions.