Why code from from Metamask documentation does not works - *SyntaxError: An invalid or illegal string was specified contentscript.js:1 DOMException..?

From this documentation I got code. I did not changed anything. But despite that I am not able to run MetaMask. Error is under the code. I try to enable MetaMask and show account but something get wrong…

<!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>

But in the console i see (MetaMask is installed and it is possible to eanble MetaMask in real hosted web3 sites).

SyntaxError: An invalid or illegal string was specified contentscript.js:1
DOMException: An invalid or illegal string was specified
_postMessage file:///C:/Users/david/Downloads/pokus1/index.html:1
_write file:///C:/Users/david/Downloads/pokus1/index.html:1
_handshake file:///C:/Users/david/Downloads/pokus1/index.html:1
l file:///C:/Users/david/Downloads/pokus1/index.html:1
[1]</</< file:///C:/Users/david/Downloads/pokus1/index.html:1
[1]</< file:///C:/Users/david/Downloads/pokus1/index.html:1
[1]< file:///C:/Users/david/Downloads/pokus1/index.html:1
i file:///C:/Users/david/Downloads/pokus1/index.html:1
e file:///C:/Users/david/Downloads/pokus1/index.html:1
<anonymous> file:///C:/Users/mirek/Downloads/pokus1/index.html:1
MetaMask is not installed!

I see memamask is not installed, but is is. Where could be problem?

Hi which browser are you using and what version of MetaMask?

I tried Mozilla, Chrome and Edge too. Nothing worked. How to see MetaMask version?

Hi you can see your version under Settings > About :
Screenshot 2024-07-17 at 15.56.24 please double check if your MetaMask is installed: Download MetaMask: The Premier Blockchain Wallet App and Browser Extension

This topic was automatically closed after 30 days. New replies are no longer allowed.