title: Most effective way to listen for URL and run Function
body: <p>This question has been asked in a similar way before, however none of their answers manage to solve my particular issue. I'm not sure if certain parts of the API have since been deprecated but here goes:</p>
  
  <p>Using the background.js with tabs permissions in the manifest, what is the most effective way to detect the page URL for the current tab? I understand you can the tab URL using:</p>
  
  <pre><code>chrome.tabs.getSelected(null, function(tab) {
      var url = tab.url;
  });
  </code></pre>
  
  <p>Of course the above method must be triggered by a particular event. I need this to be triggered whenever the tab is changed, <strong>and</strong> when a new page is fully loaded in a current tab.</p>
  
  <p>I was playing with it for an hour or so, and came up with the idea of having two listeners that execute a function. But the onUpdated listener has this strange habit of being called multiple times.</p>
  
  <pre><code>chrome.tabs.onActivated.addListener(function() {
      // This works perfect.
      chrome.tabs.getSelected(null, function(tab) {
          console.debug("Tab is: " + tab.url);
      });
  });
  
  chrome.tabs.onUpdated.addListener(function() {
      // This somehow runs multiple times depending on the page.
      chrome.tabs.getSelected(null, function(tab) {
          console.debug("Tab changed to: " + tab.url);
      });
  });
  </code></pre>
  
  <p>I know I could probably hack this by ignoring the duplicates, but surely there's a more effective way of doing this? Just thought I'd mention that when using the onUpdated listener it's the same URL that's outputted.</p>
  
  <p>Instead of passing it over to the console, I'll be handing the URL over to a function which then checks to see if it matches valid variations of two accepted domains. I'll be running further validation on the path and parameters of the URL, which isn't my question.</p>
  
  <p>It would also need to detect when it is no longer an approved URL, as my icon will be changing colour depending if it's on a valid page or not.</p>
  
  <p>Thanks!</p>