16

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

Mark van Lent
  • 12,641
  • 4
  • 30
  • 52
user846895
  • 171
  • 1
  • 1
  • 5

2 Answers2

13

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/",
    ...
],
apsillers
  • 112,806
  • 17
  • 235
  • 239
  • Which file should i write this code , how we add permission for this. – anwerj Dec 08 '15 at 17:39
  • 1
    @anwerjunaid From the text above: "The code goes in your background page." The necessary permissions are described in the [first paragraph in the webRequest documentation](http://code.google.com/chrome/extensions/webRequest.html), which I already linked to above. You need `webRequest`, `webRequestBlocking`, and host permissions for each site you want to intercept. – apsillers Dec 08 '15 at 17:43
6

This is an old question still I am answering it for future readers.

Modification of query parameters is a little tricky because you can endup in an infinite loop and Chrome/Firefox may detect it and process whatever is the current state of the request URL.

I have faced this situation in my chrome extension Requestly where Users used Replace Rule and replaced www.google.com with www.google.com?q=query or did something similar.

The problem with this approach is browsers intercept the request URL after adding query parameter so the parameter will be added multiple times and corrupt the URL. So you have to ensure either of the following:-

  1. Do not intercept a request once it has been redirected.
  2. Check if the parameter already exists, then do not redirect it.

As correctly pointed out by @apsillers in his answer, you have to use webRequest API to perform any modifications to the URL. Please have a look at his answer and write your code accordingly.

Just in case, you are looking for an already available solution, consider trying Requestly's Query Parameter Rule. Here is a screenshot of how it looks like:-

enter image description here

For Firefox, you can download Requestly from its home page.

Sachin Jain
  • 21,353
  • 33
  • 103
  • 168