Jul
09
2008
0

Something for the brain

My layman solution to Dustin’s Programming Brain Teaser.

    var arr = ['a', 'b', 'c', 'c', 'd','e', 'e', 'e', 'e', 'e', 'f', 'e', 'f', 'e', 'f', 'a', 'a', 'a', 'f', 'f', 'f'];
 
    var result = '';
 
    arr.forEach(function(value, index){
        if(index > 1 && (arr[index] == arr[index - 1]) && (arr[index - 1] == arr[index - 2])){
            if(index == (arr.length - 1) || (arr[index + 1] != arr[index])){
                result += (index < 3 || (arr[index - 2] != arr[index - 3])) ? ' &lt;span&gt;' + value + '&lt;/span&gt;' : ' ' + value +  '&lt;/span&gt;' ;
            } else {
                result += (index < 3 || (arr[index - 2] != arr[index - 3])) ? ' &lt;span&gt;' + value : ' ' + value;                
            }
        } else {
            result += ' ' + value;   
        }
    })

Remi Sharp and other guys who used regular expressions came up with much ‘cooler’ solutions. Regular Expression is really powerful. Need to work on that.

Written by Bhabishya Kumar in: JavaScript |
Sep
13
2007
2

Google Gadget for fellow Immigrants

I have created a Google Gadget for all the Immigrants of India/China who were finally able to proceed with the last stage of their Green Card process after I-485 Fiasco of 2007.

This displays the Date as well as the Countdown to 180 days from I-485 Receipt Date of Green Card. I call that day “H1B Independence Day”. They have to add their own I-485 Receipt Date under Settings. The default is last date of filing - August 17, 2007. This Gadget can be added to iGoogle, Blog or Website. They can look at this Gadget every morning and feel good.

Its hosted in Google Directory. I have posted about this at ImmigrationPortal Forum.

Written by Bhabishya Kumar in: JavaScript |
Aug
25
2007
0

More Monkey Business

Gasoline requested me if I could develop a Greasemonkey script similar to my first script for Picsearch.com. Initially I thought it should be fairly simple. So, I looked into Picsearch. I was wrong. The good thing with Google Image Search is that all the search results have the url of actual image and actual page as part of their href. So, you just have to parse the href and replace it with image url.

Picsearch on the other hand just points to some ID and when you click on the search results to go to next page, then only you see those urls. So, it became a 2-step process compared to one for Google. First, I had to load the actual href in an invisible Iframe and then read the document in that Iframe to look for the desired urls.

And so, something which was supposed to be a mundane task turned out to be exciting. I had tried this Iframe busines earlier too but had to give up because of cross-domain security restriction (silly me!).

Also, I guess I have to learn more about Greasemonkey quirks. Some things which work fine in real world stop working in Greasemonkey script. Like my onload event handler for Iframe started giving some strange errors in Firebug though it worked perfectly fine in my standalone testing. So, I had to do it the eventListener way. I guess it doesn’t like level 4 JavaScript:).

Another weird thing was that the popup-positioning JavaScript which was working perfectly fine for my Google script stopped working. I have no idea why. Maybe, it was conflicting with something in the main body of Picsearch. Anyway, this forced me to try something else and I came across something new. You can manipulate the loaded stylesheets through JavaScript using document.stylesheets. That was fun.

So, finally the script is ready. I even added [Origin Image | Origin Page] to it like CustomizeGoogle. And its also an indicator that the script has done its business and popups should appear now (helpful, since its slower compared to google version because of above mentioned reason). And I assume everybody is going to open images in new tabs(if at all) because if you open in the same tab and come back it will obviously reload all the Iframes and images again.

You can download it from its UserScripts page or directly from here.

Update - 08/28/2007 - I came across an irritating problem with some of the search results. Some of the origin pages don’t want them to be shown in frames in some other site. So, they use the following hack at the top of their pages to break out of frames.

if (window!= top) top.location.href=location.href

This was loading their pages in Picsearch results page when I was trying to load them in a IFrame, spoiling the whole thing. So, instead I am using Ajax now. I had thought of Ajax earlier too but gave it up thinking invisible IFrame should work the same way. But, I was wrong(again?). The new script is MUCH FASTER!!!

Written by Bhabishya Kumar in: JavaScript |
Aug
11
2007
0

JavaScript Knowledge Test

I came across a very interesting test of JavaScript Knowledge. I agree with most of the comments for that article, the gap between level 5 & 6 is much more than any of the other levels.

I have yet to come across a Java project with JavaScript code of level 6. Even level 4 and 5 are rare. And the reason being that in traditional Java projects, JavaScript is used only for basic UI validations, for which anything over level 3 is an overkill.

I would love to try out level 6 JavaScript code in my projects but I guess that would scare all the other Java Developers away:)

Written by Bhabishya Kumar in: JavaScript |
May
28
2007
4

My First Greasemonkey Script

Today, I wrote my first Greasemonkey Script! Woo-Hoo!

I always wanted to try out my hand at Greasemonkeyness and today turned out to be that day. I was doing a Google Image Search for Hot Lyla from Friday Night Lights and as always I had to go through a number of clicks to open the photos that looked “promising” in new tabs before starting to view them.

So, being a long weekend, I thought why not do a NetFlix kind of Popup for Google Image Search Results so that I don’t have to go through this clicking routine everytime I am searching for a hot girl!

My Script is a mashup of the original script by Patrick Cavit and a Dynamic Drive CSS Library.

Although, Eric Hamiter & CustomizeGoogle have already fixed the most irritating issue of having to click twice on search result to get to original image, I wanted to take it a step further by not having to click at all (atleast for small/medium images). Instead of opening images in separate tabs, you can view it in a CSS Popup on Mouseover.

Unlike NetFlix, who have fixed size Popup for Movie Info, I had to figure out the least ugly way to position Popups since the images are of random sizes.. The default CSS Library was always positioning the Popup below the search result with a little left offset. This was looking very ugly for the search results on the right & bottom portion of the page. I was not able to fix this issue through CSS because of my limited knowledge of it. So, I had to write some ugly JavaScript to take care of this.

You can find the Script here & here.

I can’t help checking the Install Count at UserScripts.org every now and then to boost my ego.

Update - 08/06/2008 - On megamorphg’s request, added the resize feature. The large images are now resized to fit the available screen area, no need to scroll any more. This feature is dynamic in the sense that the same image will open in different sizes up to its full size depending on the position of its thumbnail on the screen when mouse is hovered over it.

Written by Bhabishya Kumar in: JavaScript |

Powered by WordPress | Aeros Theme | TheBuckmaker.com WordPress Themes