0

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 emailjs 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 tempermonkey to get it to run onload I used onload scripts too they had the same result , the library is injected but none if it commands work. here is the script :

// ==UserScript==
// @name         autoRuns
// @include      https://daleel.com/wp-admin/edit.php?post_type=shop_order*
// @grant        all
// ==/UserScript==
new Promise((resolve) => 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) => setTimeout(resolve, 2500));
  location.href = location.href.replace(/#.*/, "#" + alll);
}

window.location.reload(1);
https://stackoverflow.com/posts/66420617/edit#

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: the error image 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 .

jweaker
  • 1
  • 1
  • 1
    You're injecting the email library, but you aren't waiting for it to load. After `document.head.appendChild(s);` you need `s.onload = function() { ... };`, then move all the remaining code inside that function (i.e. replace `...` with the rest of your code) –  Mar 01 '21 at 10:48
  • yup that was it .. thank you !! – jweaker Mar 01 '21 at 11:33

0 Answers0