title: Google Chrome extension - How to execute a code once page is loaded?
body: <p>I'm trying to create an extension to automate some tasks. I'm trying to load a page and then execute a code once the page has been loaded but I couldn't find the right way to do this.</p>
  
  <p>custom.js:</p>
  
  <pre><code>var func=function(){
  
  var actualCode = '(' + function() {
      //load the new page
      window.location='http://page.com/'
  } + ')();';
      // inserts the code into the current page tab
      chrome.tabs.executeScript(null, { code:actualCode})
  
      //--Execute this code once the page is loaded--
  
  }
  
  document.getElementById('btn').addEventListener("click",func);
  </code></pre>
  
  <p>index.html:</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
   &lt;head&gt;  
   &lt;/head&gt;
   &lt;body&gt;
    &lt;button id="btn"&gt; BTN &lt;/button&gt;
    &lt;script src="custom.js"&gt;
    &lt;/script&gt;
   &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>