title: Dynamic loading of content script (chrome extension)
body: <p>I have an chrome extension, with 2 content script injected by manifest and one background script.</p>
  
  <pre><code>{
      "manifest_version": 2,
      "name": "Test",
      "permissions": [
          "tabs", "&lt;all_urls&gt;", "activeTab", "storage" 
      ],
  
      "content_scripts": [
          {
              "matches": ["http://*/*", "https://*/*"],
              "js": [
                     "content/autofill/lib_generic.js",
                     "content/autofill/lib.js"],
              "run_at": "document_end"
          }
      ],
  
    "web_accessible_resources": [
          "content/specific_scripts/*"
      ],
  
      "background": {
          "scripts": ["background.js"],
          "persistent": false
      }
  
  }
  </code></pre>
  
  <p><code>lib_generic.js</code> contains one function named <code>apply_forms(...)</code> (its description is not important). The function is called from <code>lib.js</code> file. But this procedure doesn't work with several pages, so for each such page a I have a special script - also with only one function named <code>apply_forms(...)</code>. </p>
  
  <p>I have a function, which takes current domain as input and returns name of desired specific script or <code>false</code> if generic should be used.</p>
  
  <p>There is too many files and it's logic is more complicated, so I can't just list all <code>(url, script)</code> pairs in <code>"content_scripts"</code> directive (I also don't want to inject all specific files as content script).</p>
  
  <p>I've tried something like this in background (note that it's only for demonstration):</p>
  
  <pre><code>var url = ""; //url of current tab
  
  chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
      if(changeInfo.status == "complete") {
          var filename = getSpecificFilename(url);
          chrome.tabs.executeScript(tabId, {file: filename}, function() {
              //script injected
          });
      }
  });
  </code></pre>
  
  <p>NOTE: getSpecificFilename(...) will always return a name </p>
  
  <p>But I get <code>Unchecked runtime.lastError while running tabs.executeScript: Cannot access a chrome:// URL</code> on the 5th line.</p>
  
  <p>Can anyone help me with this? Is it the good way to "override` function definition dynamically, or should I go different way (which one, then).</p>
  
  <p>Thanks.</p>