title: Chrome Extension: Blocking content from external domain being loaded into pages from specific domain
body: <p>I am building an extension for use on a social website that I do not control. That website allows users to insert content (images, flash etc...) from other domains. I want to block this.</p>
  
  <p>I initially thought that firing jquery document.ready and removing content from html at that point would be fine because document ready fires when the dom is loaded but content has not been loaded. I just empty the necessary div elements in the hope that this would stop http requests from occurring. Unfortunately this technique does not work.</p>
  
  <p>Then I discovered chrome.webRequest which seems like it might work. The difficulty I am having is making the extension specific only to the domain I am on. Chrome.webRequest can only be used in the background page and not in content scripts.</p>
  
  <p>My initial manifest permissions looked liked this:</p>
  
  <pre><code> "permissions": [
      "tabs",
      "http://www.example.com/*",
    ],
  </code></pre>
  
  <p>But then I logged the requests using chrome.webRequest.onBeforeRequest.addListener I noticed only the image requests for the site the extension is for were being logged. So I changed the permissions to this:</p>
  
  <pre><code>"permissions": [
      "tabs",
      "&lt;all_urls&gt;",
      "webRequest",
      "webRequestBlocking"
    ],
  </code></pre>
  
  <p>I can now see all the requests using chrome.webRequest.onBeforeRequest.addListener. However because this event is being fired in the background page it is firing for all and domains I visit in the browser. When I block the images from loading it is thus blocking all images on all sites loading. </p>
  
  <p>The problem is that the object returned by chrome.webRequest.onBeforeRequest.addListener doesn't contain the url of the tab the user is currently on. Only the tabid. There is nothing in the request that I can see that I can detect which domain the current tab is on. I can obviously get the tab.url with the tab id using chrome.tabs.query but this is asynchronous as is the event onBeforeRequest listener.</p>
  
  <p>This is the event listener code at the moment:</p>
  
  <pre><code>chrome.webRequest.onBeforeSendHeaders.addListener(
  
      function(details) {
  
      if(details.type == "image" &amp;&amp; (details.url.indexOf("://www.example.com") == -1)){
      console.log(details.url);       
              //THIS LOGS ALL IMAGES LOADING EXCEPT THE SITE THE EXTENSION IS FOR 
              //BUT THE CANCEL ALSO CANCELS IMAGES LOADING ON ALL OTHER SITES.
              return {cancel: true};
      }
  
      },
      {urls: ["&lt;all_urls&gt;"]},
      ["blocking"]); 
  </code></pre>
  
  <p>So in a nutshell how do I only run the cancel code when the images that are loading (which can be an image from anywhere) are being loaded in to the site the extension is for?</p>
  
  <p>And finally (if I can get this to work) does Firefox have a similar option to chrome.webrequest as I would like to build a Firefox addon equivalent.</p>