Websense redirect – My First Chrome Extension

At my (current) workplace we have a webfiltering mechanism powered by WebSense. I’ve ranted enough number of times on twitter about websense’s stupid filtering mechanisms & the bizarreclassification that it uses to classify websites.
Recently, I found that fetching the mobile(aka text-only) versions of webpages using instapaper/google web transcoder would result in me being able to read the content – which was a very nice alternative because majority of the time, I’d be reading up on posts which are text heavy in nature. It was getting irritating to open to a webpage, encounter a blocked page, open another tab & copy-paste the URL to instapaper to grab the text version. I asked my peers are Super User:

is there a ModRewrite-like extension for chrome ? :-s

TomWij pointed me towards Privoxy but that was not what I needed. So I decided to build a Chrome Extension. The first version of the extension I built fairly quickly, but required manual intervention by way of clicking on the Extension icon to trigger the redirect – clearly not an ideal solution. Taking a deeper look at Chrome Extension API docs, I realized that I needed to build a content script.

Building up on this – I created a simple extension which checks for websense blocking pattern and if so – redirects it to instapaper which then fetches the text-only version. While this extension is not really the best-thing-since-sliced-bread, it’s of immense use to me. (And of course, I learnt to build a Chrome Extension, which I always wanted to do).

There are certain bugs, but I can live with those for the moment. If you need a similar thing – do try out my extension — it’s hosted on my servers & can be downloaded from over here. Let me know if you run into problems.

Share this:

Like this:

Related

Comments ( 9 )

So how exactly did you do it? I'm writing a Chrome extension using a page action and a content script. When I tried redirecting by simply reassigning location.href, it only redirected the pop-up containing information about the extension... not the main page. I must be missing something; thanks for any code samples you can contribute!

Hi Rachel,
Content scripts are sandboxed, so you cannot do stuff like redirects within a content scripts. You will need to send a redirect request from the content script to the background page, using message passing API.
Code snippet:
In your content script:
chrome.extension.sendRequest({redirect: newurl});
where newurl would be the URL you wish to redirect to .
You will need a handler in your background page like so:
chrome.extension.onRequest.addListener(function(request, sender) {
chrome.tabs.update(sender.tab.id, {url: request.redirect});
});

@Sathya, this is belated, but thanks so much! I can't believe it took me more than a month to try out your suggestion -- it works beautifully, presumably because scripts on the extension's background page are *not* sandboxed. I hadn't realized that. Thanks for taking the time to type out how you did it. I'm sure that others have also been helped in the meantime. :-)
Now that I'm past this particular sticking point, I'm planning to study these extensions quite a bit more!

Hey Sathya, your site here and your posts over at super users both came up in my search for an answer. I hope I can tap your brain for a minute to see if what I'm trying to do is even possible. I'm hoping that in your research you may have come across this as it is somewhat related to your project.
What I'm looking to do is automatically rewrite not the URL in the initial request, but rather a URL within an HTML anchor tag on the fly. Specifically a reference to an image file. I can do this with the developer tools by inspecting the page element and modifying the URL of the image to whatever I want and it goes ahead and displays the new image on my rendered page.
However, this needs to be somewhat of an automated process to intercept and rewrite this image every time I view the page. Do you know if this is possible and if so where can I look to get up to speed quickly on writing something like this?
I appreciate your time!

@BH
It should be possible, you'll need to analyze the DOM ( using Javascript - jQuery will make it lot easier), search for the required class and change it suitably. Have a look at jQuery tutorial ( http://docs.jquery.com/Tutorials ) and search/ask your questions on Stack Overflow ( http://stackoverflow.com )
Hope that helps!

No lender will check your credit status before approving
the funds as he only considers your repaying potential.
Even people living in Melbourne and coming from overseas
have applied for car loans and got approved through our website.
When looking from the car loan lenders perspective the market is
now flooded with competition and recession has broken the financial back bone of many prospective
car buyers. Apply for guaranteed Missouri car loan approval without worrying about
your bad credit history. Long-term car loans are also available in the
market with a repayment period of 10 to 25 years. Car
is the first choice for campus students primarily because car is safe and secured and can accommodate many things.