Blogger Jeff = new Blogger

Programming and stuff in Western PA

Firefox and Greasemonkey saved my life, Part 1

I’m addicted to a particular football message board and have been an active participant on there for three years now. Recently, the board, which has a very large member base, has had an invasion of trolls (they’ve practically made the site unreadable). Like most message board software, this site does have an "ignore" feature. What this does is hide the user’s responses by replacing it with a message ("This user is on your ignore list"). However, you can see the ridiculous threads that they’ve created, which is actually the problem in the first place.

To my rescue came a Firefox add-on called Greasemonkey. What GreaseMonkey does is it allows you to interject java script into the html that is returned from a web request. In my case, I had an idea to "hide" from my view posts from these pond scum users.

What I did was save the main page of my message board as html, and then examine it in Visual Studio. I was then able to see the elements of the page, and determine the best manner, via java script and using the page’s DOM to do this. It would have been nice if each post on the page was encapsulated inside a div tag with an id, but I didn’t get off that easy. What I was able to do was look at each table row(<tr>) tag and search for the users name. If it was there, I changed the style for the row to hide it. My entire Greasemonkey script looked like this :

// ==UserScript==
// @name          Unreadability Script
// @namespace     Igglephans
// @description   This script will block out losers
// @include       http://igglephans.com/*
// ==/UserScript==

var users="jerrybim2,ericcoe,robbie76";
var ary = users.split(",");
var trHtml="";
var item;
var o=document.getElementsByTagName("tr");

for (var i = o.length - 1; i >= 0; i--){
	item=o.item(i);

	trHtml=item.innerHTML;

	for (var z = ary.length - 1; z >= 0; z--){
		if(trHtml.search(ary[z])!=-1)
			item.style.display="none";
	};
};

Tags: ,

December 8, 2007 Posted by | Firefox, Technology | 3 Comments

C# 3.0 Extension Methods

C# 3.0 introduces a concept known as extension methods.  What these allow you to do is to "extend" static methods onto an existing instance class.  Trust me, it’s a lot easier to understand when seeing an example.  In mine, I’ll extend a FormatSSN method onto the string object :

class Program
{
    static void Main(string[] args)
    {
        String val = "987654321";
        Console.WriteLine(val.FormatSSN());
        Console.ReadLine();
    }
}

public static class Utilities
{
    public static String FormatSSN(this String input)
    {
        return String.Format("{0}-{1}-{2}", input.Substring(0, 3),
            input.Substring(3, 2), input.Substring(5, 4));
    }
}

Microsoft says "Extension methods are less discoverable and more limited in functionality than instance methods. For those reasons, it is recommended that extension methods be used sparingly and only in situations where instance methods are not feasible or possible."

So why would you ever want to use an extension method?  I would say for convenience purposes, like in the example above, perhaps you already have a pre-existing class and rather than go back and add a "FormatSSN" method, you could simply extend one to the current code base.

Tags: ,

December 1, 2007 Posted by | .Net, C# | Leave a comment