title: Chrome extension, how to delay script until page is fully loaded
body: <p>I am trying to get a DOM element by class name just after opening new website address in the tab. The problem is that I have no idea how to make it wait until the page is fully loaded. I tried alarms, setTimeOut, setTimeDelay and nothing works.</p>
  
  <pre><code>chrome.runtime.onMessage.addListener(message);
  
  function message(msg) {
    chrome.tabs.update({url: 'https://www.website.com/'});
    chrome.tabs.executeScript({code:"var theBtn = document.getElementsByClassName('btn1');theBtn[0].click();"});
  }
  </code></pre>
  
  <p>I am using chrome.runtime.sendMessage in the popup because I want to send web addresses from input, then I press a button and the code above is triggered. I simplified this because everything else works. The (msg) is just a website address</p>