5

Is there any way to detect if a certain URL is opened in chrome and redirect to another page. I need it to make a site blocker.

Nathan Piercy
  • 348
  • 3
  • 9

1 Answers1

9

Yes, you can it now with Chrome 17.

Add the background page and webRequest permissions to manifest.json:

{
  "background_page": "background.html",
  "permissions": [
    "webRequest", "webRequestBlocking",
    "http://www.mozilla.org/*"
  ]
}

and a redirect logic to background.html:

<html><body>
<script>
chrome.webRequest.onBeforeRequest.addListener(
  function(details) {
    //console.log('before', details);
    if (details.url == "http://www.mozilla.org/") {
      return {redirectUrl: "https://www.google.com/chrome/"};
    };
  },
  {
    urls: ["http://www.mozilla.org/*"],
    types: ["main_frame"]
  },
  ["blocking"]
);
</script>
</body></html>
Omer M.
  • 630
  • 7
  • 17
anfilat
  • 706
  • 4
  • 4
  • Wait... How do I put that in my chrome extension. Does it go in manifest.json or the main html script. Sorry but I know nothing about the Chrome.* API. – Nathan Piercy Feb 17 '12 at 13:00