title: Redirecting on page load with a Chrome extension
body: <p>I have a Chrome extension that I'm working on that redirects users of a non-HTTPS site to the HTTPS version automatically.</p>
  
  <p>However, the current problem with this is that the user must activate this redirection manually.</p>
  
  <p>This would be an easy feat to accomplish using content_scripts in manifest.json, however, according to the Chrome documentation, content scripts "Cannot...Use chrome.* APIs (except for parts of chrome.extension)".</p>
  
  <p>So, here's the manifest file for my extension:</p>
  
  <pre><code>{
    "name": "SSL Redirect",
    "version": "1.0",
    "manifest_version": 2,
    "description": "Redirects plain HTTP domain.com to the encrypted, HTTPS secured version.",
  
    "permissions": [ "tabs", "http://*/*", "https://*/*" ],
  
    "background" : {
    "page": "body.html"
    },
  
  "browser_action": {
            "default_icon": "icon.png"
  },
    "content_scripts": [
      {
        "matches": ["http://www.domain.com/*"],
        "js": ["redirect.js"]
      }
    ]
  }
  </code></pre>
  
  <p>And here's the js:</p>
  
  <pre><code>var domain = /domain.com\//;
  var ssldomain = "ssl.domain.com\/";
  
  function updateUrl(tab){
  
    if(tab.url.match(ssldomain)) {
      alert("You're already using the SSL site. :)")
      throw { name: 'Error', message: 'Stopped running, already in SSL mode.' };
    }
  
    if(tab.url.match(domain)) {
      var newurl = tab.url.replace(domain, ssldomain);
      newurl = newurl.replace(/^http:/, 'https:');
      newurl = newurl.replace("www.", "");
      chrome.tabs.update(tab.id, {url: newurl});
    }
  
    if(!(tab.url.match(domain))) {
      alert("This extension only works on domain.com.")
      throw { name: 'Error', message: 'Stopped running, not on domain.com.' };
    }
  
  
    }
  
  chrome.browserAction.onClicked.addListener(function(tab) {updateUrl(tab);});
  </code></pre>
  
  <p>My ultimate goal is to get this to run automatically on any page matching domain.com, without user interaction.</p>
  
  <p>I'm a little stuck. Any ideas?</p>