title: JavaScript works if injected manually into the console but not automatically
body: <p>I made a script to check if the number of orders has changed by refreshing the page every few seconds so I can email my self if there's a new order .
  I used emailjs for the emailing part so I injected the <a href="https://www.emailjs.com/" rel="nofollow noreferrer">emailjs</a> library then sent the email , in the same code.
  when I enter the code in the console it works as intended but
  the code checks once then refreshes the page so I used <a href="https://www.tampermonkey.net/" rel="nofollow noreferrer">tempermonkey</a> to get it to run onload  I used <a href="https://chrome.google.com/webstore/detail/onload-scripts/gddicpebdonjdnkhonkkfkibnjpaclok" rel="nofollow noreferrer">onload scripts</a> too they had the same result , the library is injected but none if it commands work.
  here is the script :</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>// ==UserScript==
  // @name         autoRuns
  // @include      https://daleel.com/wp-admin/edit.php?post_type=shop_order*
  // @grant        all
  // ==/UserScript==
  new Promise((resolve) =&gt; setTimeout(resolve, 4000));
  var alll = document.getElementsByClassName("count")[0].innerText;
  if (location.href.includes("#") === false) {
    location.href = location.href + "#" + alll;
  }
  if (location.href.includes(alll.replace(/\(..|\)/g, "")) === false) {
  
   var s = document.createElement("script");
    s.type = "text/javascript";
    s.src="https://cdn.jsdelivr.net/npm/emailjs-com@2/dist/email.min.js";
      document.head.appendChild(s);
      var ss = document.createElement("script");
    ss.type = "text/javascript";
    ss.innerHTML ='(function() {emailjs.init("my_user_id");})();'
        document.head.appendChild(ss);
  emailjs.init("my_user_id");
    emailjs.send("my_template_id", "template_auojv9r").then(
      function (response) {
        console.log("SUCCESS!", response.status, response.text);
      },
      function (error) {
        console.log("FAILED...", error);
      }
    );
    new Audio(
      "https://assets.mixkit.co/sfx/download/mixkit-modern-classic-door-bell-sound-113.wav"
    ).play();
    new Promise((resolve) =&gt; setTimeout(resolve, 2500));
    location.href = location.href.replace(/#.*/, "#" + alll);
  }
  
  window.location.reload(1);</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>https://stackoverflow.com/posts/66420617/edit#</code></pre>
  </div>
  </div>
  </p>
  <p>there is a lot of pauses because the server will ban me if I refresh it to fast and because I thought it will help some how.
  and I used the URL to store the previous number of orders.
  and I changed the emailjs id to a sample.
  here are the errors when I run it using tempermonkey:
  <a href="https://i.stack.imgur.com/AxaR1.png" rel="nofollow noreferrer">the error image</a>
  but when I run the exact same code in the console manually it works fine.
  I'm new to JavaScript , any kind of comment will help and will be appreciated .</p>