NBA game recaps, not game info!

With reference to this post, I decided to write my first Greasemonkey script.

I don’t subscribe to cable TV, so I get my sports news fix mostly by reading. Now that the NBA playoffs are in progress, I use NBA.com on a daily basis to read recaps like this one. I’m upset enough that the Lakers — one of the eight teams I’m rooting for — lost, without having to right-click “Game Scoreboard”, copy the link, and replace “gameinfo.html” with “recap.html” — which is what I’m most interested in, since I didn’t watch the match.

This is a pain, and since I can’t control NBA.com, I’ve written nba_recap_not_gameinfo, a Greasemonkey userscript to do it automatically for me.

This uses the excellent jQuery, of course, and since this blog does programming-related posts, we’ll take at the important (super-simple) part consists of just 3 lines of Javascript.

So we’ll look for a descendants of span with class “gamelinks”, and then we regex-replace “gameinfo” with “recap”.

jQuery("span.gamelinks a").each(function() {
var $this = $(this);
$this.attr("href", $this.attr("href").replace(/gameinfo/, "recap"));
});
Advertisements

jQuery: $ namespace conflict with TinyMCE

I wanted to add a rich text editor one day, so I was looking at TinyMCE. This is a nicely done editor written in JavaScript. Now I’ve used it before, basically you include the main script, and it changes the textarea elements into a nice editor. I downloaded the 2.1.2 version, and it didn’t work.

Somehow TinyMCE conflicts with jQuery v1.1.3.1 — I used the pack-ed version. Or vice-versa. Anyway, to solve the problem:

jQuery.noConflict(); // use jQuery instead $ from here on

jQuery(document).ready(function() {
// TODO
});

Read more about it here.

CodeIgniter: jQuery $.ajax goodness

I’ve been using the jQuery JavaScript library for sometime now. It actually makes writing client-side programming fun, wow! The AJAX functionality is a joy to use; you can submit requests as POST instead of GET since the former is not available in the CodeIgniter PHP framework.

Say you have a controller user.php and a view user_add.php. Now you want to submit an AJAX request to the server to check if the (presumably unique) username field is available for use.


$(document).ready(function() { // user_add.php
	$('#username').blur(function() {
		$.ajax({
			url: 'username_callback/' + $(this).val(),
			type: 'post',
			success: function(result) {
				$('#username_check').attr('innerHTML', result);
			}
		});
	});
});

// html: user_add.php


So we’ve added an anonymous function to the username element. When it loses focus, we’ll submit a POST request to your your user controller, username_callback method. On success, we’ll replace the content (innerHTML) of the span username_check with the value returned by the PHP function.

I’m using the site_url() helper function to get my CI installation URL. NOTE: You’ll need to include this in the user.php constructor or in the autoloader.

// php: user.php

class User extends Controller
{
	function User()
	{
		parent::Controller();
		$this->load->helper('url');
	}

	function username_callback()
	{
		$s = trim(strip_tags($_POST['username'])); // clean

		if ($s == 'admin') { // error
			print 'The username 'admin' is reserved.'
		}
	}
}

username_check() will check if the POST-ed parameter is ‘admin’, and complain if so. So there you have it. That wasn’t too difficult, wasn’t it?

P.S. I would have loved to post up a working example but WP won’t accept .zip files.