Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to rewrite url (with extra parameters) with a Chrome extension

I am trying to append few extra parameters to the url that user typed (before the page gets loaded). Is it possible to do?

For example, if user types www.google.com, I would like to append ?q=query to url (final: www.google.com?q=query.

Thanks

like image 701
user846895 Avatar asked Apr 21 '12 06:04

user846895


1 Answers

The webRequest API might be what you need. This code goes in your background page:

chrome.webRequest.onBeforeRequest.addListener(
    function(details) {
        if( details.url == "http://www.google.com/" )
            return {redirectUrl: "http://www.google.com/?q=defaultquery" };
    },
    {urls: ["http://www.google.com/*"]},
    ["blocking"]);

This is an extremely specific rule that redirects visits to http://www.google.com/ with http://www.google.com/?q=defaultquery, but I think you can see how to expand it to include more functionality.

Note that this will reroute all attempts to reach http://www.google.com/, including Ajax requests and iframes.

Per the documentation, you will need to add the webRequest and webRequestBlocking permissions, along with host permissions for every host you plan to intercept:

"permissions": [
    "webRequest",
    "webRequestBlocking",
    "*://*.google.com/",
    ...
],
like image 113
apsillers Avatar answered Nov 08 '22 06:11

apsillers