up vote 8 down vote favorite
1
share [g+] share [fb]

Is there any extension to Chrome that will let me force a URL from a particular domain to be redirected to another domain?

(E.g. Redirect http://www.google.com to https://encrypted.google.com.)

Note: I'm looking for an arbitrary redirector, not KB SSL Enforcer, which only works for the specific task of redirecting to HTTPS.

link|improve this question

70% accept rate
Not that I know of - I built a Chrome extension which does this - will be happy to post the code as an answer – Sathya May 16 '11 at 8:35
@Sathya: Cool, I'm definitely interested! :D – Mehrdad May 16 '11 at 8:37
You could also consider using the HOSTS file as another method. – Synetech May 23 '11 at 20:33
@Synetech: Would that redirect just the root page, or anything with a particular domain? – Mehrdad May 23 '11 at 20:39
It would redirect the whole domain (or subdomain as the case may be). – Synetech May 24 '11 at 2:53
feedback

1 Answer

up vote 11 down vote accepted

I had built a Chrome extension which does this.

Note: I built this for just 2 sites - just for the heck of it - by no means it's professional quality™. Please don't flame me for crappy code :)


manifest.json

{
  "name": "URL Redirect",
  "version": "0.1",
  "description": "Checks URL and redirects as required.",
  "background_page": "bg.html",
   "content_scripts": [
   {
     "matches": ["http://*/*"],
     "js": ["content.js"]
   }
   ],
  "permissions": ["tabs"]
}

bg.html

<html>
  <script>

chrome.extension.onRequest.addListener(function(request, sender) {
        chrome.tabs.update(sender.tab.id, {url: request.redirect});
    });

  </script>
</html>

content.js

var pattern=/\bIWSS/
var hnold = /news.ycombinator.com/
var imgur = /imgur.com/
var new_image = "http://sbhat.me/u/fetch_images.php?ImgUrl="
var newurl

if (pattern.test(window.document.title)) // if it matches pattern defined above
{

    if (window.location.href.match(hnold))  // redirect from news.ycombinator to hackerne.ws
    {
      newurl = window.location.href.replace(hnold,"hackerne.ws");
    }
    else if (window.location.href.match(imgur))
     {
      newurl = new_image + window.location.href;
     }


    chrome.extension.sendRequest({redirect: newurl}); // send message to redirect

}

To install this, create files with filenames as mentioned above the codeblock.

enter image description here

Once all 3 files are created, Click on Wrench -> Tools -> Extensions. Click the "+" on Developer Mode. Click on Load Unpacked extension and point to the directory where the files are stored.

enter image description here

Edit the files are required, and uninstall and reinstall the extension as mentioned above

link|improve this answer
Omg cool! How do I install it? (Sorry I have no experience with developing Chrome extensions, haha.) – Mehrdad May 16 '11 at 9:05
@Mehrdad expanded the answer – Sathya May 16 '11 at 9:19
ofcourse, if you aren't looking at editing the files, I could've packed the files in to a Chrome Extension.... – Sathya May 16 '11 at 9:21
Beautiful, thank you so much! It teaches me some JavaScript too! :D – Mehrdad May 16 '11 at 9:25
welcome! I had developed this to learn Chrome extension development. (and to get around cough*websense*cough). – Sathya May 16 '11 at 11:26
show 7 more comments
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.