2

I'm attempting to create a Chrome extension which will add a parameter to the end of a URL if the URL matches a given pattern (*://*.mydomain.com/s/*). Below is the manifest file and background script I have, but I cannot get it working. What am I doing wrong?

manifest.json:

{
  "manifest_version": 2,
  "name": "Search Grid View",
  "version": "0.1",
  "description": "Changes MyDomain.com search to grid view by default",

  "background": {
     "scripts": ["background.js"]
  },

  "permissions": [
    "tabs",
    "webRequest",
    "*://*.mydomain.com/s/*",
    "webRequestBlocking"
  ]

}

background.js:

chrome.webRequest.onBeforeRequest.addListener(
  function(details) {         
    var currentUrl = tabs[0].url;
    var newUrl = currentUrl + "&style=gridview"
    return { redirectUrl: newUrl};
  },
  {
    urls: [
      '*://*.mydomain.com/s/*'
    ],
    types: ['main_frame']
  },
  ['blocking']);

Thanks in advance for any advice!

user994585
  • 661
  • 3
  • 13
  • 28
  • `browserAction` means an icon in the addressbar (omnibox), see [the API docs](https://developer.chrome.com/extensions/browserAction). Otherwise you'd need a background script and [webRequest](https://developer.chrome.com/extensions/webRequest) or [webNavigation](https://developer.chrome.com/extensions/webNavigation) API. Clarify the question, please. – wOxxOm Jul 22 '15 at 16:30
  • Try "lastFocusedWindow: true" instead of "currentWindow: true" –  Jul 22 '15 at 16:39
  • @wOxxOm Thanks! Very new to Chrome extensions, so I've been trying to hack together different examples, which obviously didn't work so well. I've updated the question above and rewritten the structure to use a background page with the webRequest API. That being said, it's still not working. Any idea what's wrong? – user994585 Jul 22 '15 at 17:16
  • Try using the code in [this question](http://stackoverflow.com/q/15359042/3959875) replacing the values accordingly. Thus you'll modify the url *before* it is sent to the server, eliminating the page refresh flicker. – wOxxOm Jul 22 '15 at 17:32
  • @wOxxOm I updated my background.js code, but still nothing happens. Take a look at the updated code above. I also added webRequestBlocking permission to my manifest.json, as I'm pretty sure that's necessary now. Any more ideas? – user994585 Jul 22 '15 at 17:48

1 Answers1

4
  1. Use debugger - click your extension's background page on chrome://extensions page and switch to the Sources panel.
  2. To obtain the url use onBeforeRequest's callback parameter
  3. Check if the url is already modified.

chrome.webRequest.onBeforeRequest.addListener(
    function(details) {
        return {
            redirectUrl: details.url + 
                (details.url.indexOf("?") == -1 ? "?" : "") +
                (details.url.indexOf("&style=gridview") == -1 ? "&style=gridview" : "")
        };
    },
    {urls: ['*://*.mydomain.com/s/*'], types: ['main_frame']},
    ['blocking']
);
wOxxOm
  • 65,848
  • 11
  • 132
  • 136
  • Thank you! The updated code you provided works perfectly. Unfortunately, I have run into one more issue... Now I've realized that sometimes the URL will already have a parameter and sometimes it will not. So, I need to check if there's a "?" in the URL. If there is, I need to append, "&style=gridview", but otherwise I need to append "?style=gridview". What's the best way to go about this? Thanks again for all your help! – user994585 Jul 22 '15 at 19:08
  • There are many ways to go about it, for example use `indexOf`, see the updated code. – wOxxOm Jul 22 '15 at 19:12