1

Good day everyone! I decided to try making an extension for Chrome for the first time and I have this idea but I don't know how to start. I hope you'd point me to the right direction.

I want to detect if a certain URL is opened then change the value of one of its query strings. For example:

  1. I open the page http://downloadpage.url/?page=info&id=123456
  2. The extension will detect this and change the query string "page" value from "info" to "download" and continue opening the page with the new values.

How do I do this? I think the permissions needed would be WebRequest and WebRequestBlocking but I'm not sure. I tried the steps here: Detect if URL is opened - Chrome extension but it doesn't seem to work even after changing background_page to background to account for my manifest's version (my Chrome version is 21.0.1180.89). I was hoping I could just modify the code there but unfortunately it didn't work for me and now I'm lost.

Community
  • 1
  • 1
DeVilFisCh
  • 267
  • 3
  • 9
  • 1
    The [WebRequest API](http://developer.chrome.com/extensions/webRequest.html) sounds like the best option in this case. Can you show what you've tried (because you allege that it doesn't work as intended). For another example, see [Chrome Redirect Extension](http://stackoverflow.com/questions/12065029/chrome-redirect-extension/12070823#12070823) on Stack Overflow. – Rob W Sep 23 '12 at 14:15
  • So far all I've tried is the link to an Stack Overflow example above and did some small modifications to it. I've been searching for other examples but it seems the one you gave is quite informative. I'll try that first and see how far I can go. Thanks! – DeVilFisCh Sep 23 '12 at 14:36
  • Rob, thanks to you I managed to do what I intended. I answered my own question but if you have a better solution for that I'm all ears! Thanks again. :) – DeVilFisCh Sep 24 '12 at 00:09

1 Answers1

1

Following Rob W's link in his comment above (Thanks Rob!) I came up with some code that works according to what I wanted. First I added the following to the manifest to run a content script when I'm in the intended URL:

  "content_scripts": [
     {
       "matches": [
          "http://downloadpage.url/*"
       ],
       "js": ["background.js"]
     }
  ]

Obviously the URL is fake...

Then created a file called background.js with the following function which makes it easy to grab the query strings. The original code came from here and I modified it a bit to make it reusable for other links or strings.:

function getQueryString(URL) {
  if (!URL) {
    URL = location.search;
  }
  else
  {
    var a = document.createElement('a');
    a.href = URL;
    URL = a.search;
  }

  var result = {}, queryString = URL.substring(1),
      re = /([^&=]+)=([^&]*)/g, m;

  while (m = re.exec(queryString)) {
    result[decodeURIComponent(m[1])] = decodeURIComponent(m[2]);
  }

  return result;
}

Then finally the code that does the redirect with the modified query string is below:

if (page == "info")
{
  var ID = getQueryString()["id"];

  location.replace('http://downloadpage.url/?page=download&id='+ID;);
}

I'm still open for better and more elegant solutions but this does what I want to do. If you have a better idea please let me know. Cheers!

Community
  • 1
  • 1
DeVilFisCh
  • 267
  • 3
  • 9
  • 1. Don't call your script `background.js` when it's not a background script. A more suitable name is `contentscript.js` 2. Since you only want to deal with URLs, add `"run_at": "document_start"` to the content script section in the manifest file. 3. The webRequest API is a better tool for this, what prevented you from writing the code using this API? – Rob W Sep 24 '12 at 08:24
  • I plan to change the filename. I just used the js file available at that time. – DeVilFisCh Sep 24 '12 at 09:28
  • Whoops! I posted the incomplete response by accident! I really wanted to use WebRequest but nothing happens when I tried the code on the link you provided - even without changing it which felt really weird. The same thing happened to the first code I tried. On the other hand, I was thinking of completely scrapping this idea and just make my extension modify the links on the referring page instead. It's going to be a lot more work, I think, but it won't need to do redirects anymore which makes it a lot more faster in my opinion. – DeVilFisCh Sep 24 '12 at 09:36
  • 1
    Replacing links is less reliable than redirecting. What Chrome version are you using? webRequest is only available since Chrome 17 (fyi, we're currently at Chrome 21 on the stable channel). Here's an example which should work: http://pastebin.com/Nf8Nvf3Z – Rob W Sep 24 '12 at 10:17
  • I'm also at version 21. The code you gave worked! Awesome! I'm using a different computer now though so maybe the installation of Chrome on the computer I used yesterday is the problem. I'll try reinstalling it later when I get home and hope it works there too. Also, kindly post your answer so I can mark it as such! Thanks a bunch! :D – DeVilFisCh Sep 24 '12 at 10:57
  • Just mark your own answer. My code example is almost identical to the one in the answer I originally referred (see comment at question), so I'm voting to close as a duplicate. – Rob W Sep 24 '12 at 11:15
  • Thanks. I'll just do that then. I actually have a new problem with my new code (the code I posted in my answer above doesn't actually contain everything such as referral exclusions which work on my code but I can't seem to get to work with webRequest) but it's beyond the concept of my question so I'll just ask a new one when I'm tired of trying by myself. Thanks again. – DeVilFisCh Sep 24 '12 at 13:56