Code

You are currently browsing articles tagged Code.

Twitter sidebar hack

So by default the official Twitter HTML/Javascript badge doesn’t actually let you click-through hyperlinks, they’re just displayed as normal text the same as everything else.

The main JavaScript function from that badge is:

function twitterCallback2(obj) {
	var twitters = obj;
	var statusHTML = "";
	var username = "";
	for (var i=0; i<twitters.length; i++){
		username = twitters[i].user.screen_name
		statusHTML += ('<li><span>'+twitters[i].text+'</span> <a style="font-size:85%" href="http://twitter.com/'+username+'/statuses/'+twitters[i].id+'">'+relative_time(twitters[i].created_at)+'</a></li>')
	}
	document.getElementById('twitter_update_list').innerHTML = statusHTML;
}

So basically it’s just inserting into the twitter_update_list  (the list in the sidebar)  the text of each tweet, and then a link to that particular tweet with the “relative time” as the link text, with a separate relative_time(datetime) function that just converts an actual timestamp to an ordinal name.

So because there’s no formatting inside the twitters[i].text, links are displayed in the same formatting as any other text.

So even though this is kind of a major design flaw (imho), especially coming from Twitter themselves as the official blog badge, it’s just them being simple with their function – low frills.

BUT, we can be a bit fancy with regular expressions. So, if we add another script that replaces the twitter_update_list with a formatted version (that we subject to a search-and-format for links using regexes), then we can force the Twitter sidebar to have true links.

document.getElementById('twitter_update_list').innerHTML = document.getElementById('twitter_update_list').innerHTML.replace(/https?:\/\/([-\w\.]+)+(:\d+)?(\/([\w/_\.]*(\?\S+)?)?)?/g,"<a href="$&">$&</a>");

Overall, this shows that regexes can indeed, save the day.

this way is a waterslide-away-from-me-to-chase-her-fuller-everyday

Tags: ,

Weak typing in VB

So I scribbled this post down on a piece of paper back in November or so, and going through my old stuff found it again today. At the time I had grandiose ideas of regularly blogging on random programming stuff, but I suppose that idea was abandoned in favour of irregularly blogging on random introspective stuff :P

Anyhow, this was back when I actually had to use VB (2005) for mech engg in Edmonchuck (dark days). I noticed this interesting thing:

Say you’ve got two combo boxes, A and B, and the value of A is “1″ and B is “2″.

Redim myArray(cboA.Text + cboB.Text - 1)
myArray.GetLength(0)

And you’ll get 12 for the array length.

But, like this:

Redim myArray(cboA.Text - 1 + cboB.Text)
myArray.GetLength(0)

You’ll get 3 for the array length.

Which totally violates the possibly-expected commutative property of add/subtract, because f(a + b – c) != f(a – c + b).

But, I guess what’s actually happening is that the “+” operator, when given String + String, simply appends the two strings together, thus “1″ + “2″ = “12″. Then, the String – Integer ends up being implicitly converted to Integer – Integer, so we get 12 – 1 = 11, and an array length of 12.

In the second case, the String – Integer ends up doing the same implicit type conversion on the String to make it Integer – Integer, so we get 1 – 1 + 2 = 2, and an array length of 3.

I guess it all boils down to VB being rather weakly typed. It’s progammer-friendly in that it doesn’t require a lot of typecasting, but also dangerous in that it gives you zero feedback on what it’s implicitly converting. In this case, I think I spent at least a few hours being super confused about some calculations messing up, before I realized my mistake.

Anyhow, that’s my random VB tale for today. C# ftw!

it’s not me, it’s not my family

Tags: