My World A Blog On All The Things That Happen In My Life

Operation Upgrade Envy RAM successful, the Envy now features 8GB RAMOperation Upgrade Envy RAM successful, the Envy now features 8GB RAMOperation Upgrade Envy RAM successful, the Envy now features 8GB RAMOperation Upgrade Envy RAM successful, the Envy now features 8GB RAMOperation Upgrade Envy RAM successful, the Envy now features 8GB RAMOperation Upgrade Envy RAM successful, the Envy now features 8GB RAMA look at how @CREAMnFUDGE_IN prepare the ice creams ;) /cc @9_6A look at how @CREAMnFUDGE_IN prepare the ice creams ;) /cc @9_6
Status » RT @foobarthik: Story of the hour !! RT @newsycombinator: Indian Government to intercept phone calls without operator knowledge http://t ...

Websense redirect – My First Chrome Extension
about Programming on 04/02/2011 - like it?

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 bizarre classification 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.

No related posts.

Tags: , , , , , ,


8 comments - really? way cool!

#brainysathya

Lovingly written by ashwinsid on 04/02/2011

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!

Lovingly written by Rachel on 22/02/2011

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});
});

Lovingly written by Sathya on 23/02/2011

@shankargan that's the one. My Chrome Extension ( http://u.sbhat.me/ibc1iF )broke too :/ shifted to GWT for time being @shivaranjan

Lovingly written by Sathya on 12/03/2011

@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!

Lovingly written by Rachel on 04/04/2011

@Rachel you’re welcome, do let me know if you’ve authored some extensions, would love to take them for a spin.

Lovingly written by Sathya on 05/04/2011

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!

Lovingly written by BH on 18/05/2011

@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!

Lovingly written by Sathya on 18/05/2011

Leave some comment love