Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect if URL is opened - Chrome extension

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.

like image 666
Nathan Piercy Avatar asked Feb 21 '23 10:02

Nathan Piercy


1 Answers

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>
like image 146
anfilat Avatar answered May 18 '23 22:05

anfilat