Search code examples
google-chromegoogle-chrome-extensionwebrequestforwarding

Simple forwarding using Google Chrome webRequest API


I am attempting to create a Chrome extension to help me understand Chrome's webRequest API by replicating what is done with this answer but without the experimental portion of the namespace, which is no longer needed since webRequest is now in Chrome's trunk.

In my background page I have the code:

<!DOCTYPE html>
<html>
  <head>
  <script>
function interceptRequest(request) {
    return { redirectUrl: 'http://www.yahoo.com' }
}
chrome.webRequest.onBeforeRequest.addListener(interceptRequest, { urls: ['*://*.google.com/*'] }, ['blocking']);
  </script>
  </head><body></body>
</html>

In my manifest file I have:

{
  "name": "My Extension",
  "version": "0.1",
  "background_page": "background.html",
  "permissions" : [
     "webRequest",
     "webRequestBlocking",
     "*://*/*"
  ]
}

Based on the previously asked question, I should be forwarded to http://www.yahoo.com when I access https://www.google.com and my extension is loaded, however, https://www.google.com loads without any forwarding effect. What do I need to do in order to simply forward a user from one URI to another using the webRequest API in Chrome?


Solution

  • Your url matching pattern isn't correct. You simply need to add a trailing slash:

    {urls: ['https://www.google.com/']}
    

    Alternatively, you could use: '*://www.google.com/' to match all protocols, '*://www.google.com/*' to match beyond just the domain, and '*://*.google.com/*' to match all subdomains, protocols and paths, etc.

    See: http://code.google.com/chrome/extensions/trunk/match_patterns.html