encdec – BaseXX Encoding and Decoding in JavaScript

Recently, while hacking around with some Python code I came up with a Gist for base58 encoding and decoding. Now, just for fun, I thought
it would be a good idea / exercise to port this over to JavaScript. This gave rise to encdec – a small base encoding / decoding library.

By default if uses base58 encoding, but it does let you pass it
your own base alphabet allowing you to do base16, base32 or base-whatever-you-like encoding.

Please have a play with it and let me know what you think :)

Posted in javascript at June 3rd, 2011. 2 Comments.

2 Things I Learnt About JavaScript This Week

I recently picked up a copy of JavaScript: The Good Parts and have been reading it on the train to and from work. Although the book's preface states that the book is targeted at programmers who are venturing into JavaScript for the first time or who have been working with JavaScript at a novice level I'm still finding it enjoyable to read and am managing to pick up a few tips along the way.

Two such tips that I picked up this week are:

  1. When using the throw statement you can use an object literal to give an exception a type and a message. For example:

    throw {
    	type: 'FooError',
    	message: 'Invalid foo'
    };

  2. When using closures, avoid creating an anonymous function inside a loop as it can be computationally wasteful and potentially lead to confusing code. For example (this is taken from the book):

    var add_the_handlers = function (nodes) { 
    	var helper = function (i) {
    		return function (e) { 
    			alert(i);
    		};
    	}; 
     
    	var i; 
    	for (i = 0; i < nodes.length; i += 1) {
    		nodes[i].onclick = helper(i);
    	}
    };

Posted in javascript at February 3rd, 2011. No Comments.

GMail Toolbar Chrome Greasemonkey Script

I quite like the toolbar at the top of GMail. You know, the one with links to Google Calendar, Google Reader, Google Docs, et al. But I’m not that keen on having to scroll right back to the top of the page each time I need access to it. So, I created a simple Greasemonkey script for Google Chrome to apply fixed positioning to the toolbar. That way it always stays at the top of your screen :)

(Firefox users: I did create a user stylesheet that achieves the same effect
recently. However, that relies on the @-moz-document domain(mail.google.com) to restrict the CSS to GMail and Google seems to send different HTML to Firefox than it sends to Chrome).

You can fork a copy of the script from GitHub: https://github.com/ianoxley/gmailtoolbar

Or alternatively download it from here: gmailtoolbar.user.js

Posted in web at December 10th, 2010. No Comments.

Moving Greasemonkey Scripts To GitHub

Recently I made a few amends to some Greasemonkey scripts I wrote a while back and, since I’ve been using git quite a bit recently, it made sense to me to move my Greasemonkey scripts onto GitHub.

So I did :) And here they are:

Posted in javascript, web at August 18th, 2010. No Comments.

Greasemonkey Open Selected Links Script

I hadn’t done any monkeying around with Greasemonkey for a while until the other day when I came up with this script: http://github.com/ianoxley/open-selected-links

Any links that are present in the selected text will be opened on the mouseup event, except for the Cached and Similar links you get in Google search results.

If you’ve got and suggestions for improvements, let me know in the comments :)

UPDATE

The script has been updated so that you now have to press the Ctrl or Cmd key while selecting the text, in the same way that you would Ctrl / Cmd + click to open a link in a new tab.

Posted in javascript at June 1st, 2010. No Comments.

JavaScript WebKit Notifications API Demo With Flickr and JSONP

I’ve been playing around with the JavaScript Notifications API recently – or to be a bit more specific the WebKit Notifications API – and put together this little demo using Flickr and JSONP:

  1. You search Flickr by entering a tag
  2. The JSONP callback function then displays the first few photos returned using the Notifications API
  3. Each notification is cancelled after 10 seconds (there is no Dismiss button, like there is when you use the API on localhost)

You’ll need to grant notification permissions first to be able to view the notifications – you should see an info bar like the one below the first time you click on Search:

WebKit Notifications API requestPermission screenshot

After that, you should see the notifications stack up in the bottom right-hand corner of your screen:

Screenshot of the notifications

At the time of writing, you’ll need to be using Google Chrome for the demo to work (although I’m pretty sure that support for the Notifications API in Safari is imminent).

And, as an aside, I managed to throw a bit of HTML 5 into the demo as well with some autofocus and placeholder attributes on the <input type="text" /> field, just for good measure :)

Posted in javascript at April 24th, 2010. No Comments.

A Simple jQuery Print Page Plugin

The other day I was after a really simple way to add a print page link to a page to, well, er, print it.  So I came up with my jQuery Print Page plugin and stuck it on GitHub with a demo page: http://github.com/ianoxley/jqueryprintpage

This really is nothing fancy: it’s just a convenient way of adding an <a> tag plus an event-handler to a page to print it. Using it is as simple as, well, using File -> Print. But you can’t style the File menu with CSS so this is much more fun :-)

Posted in javascript at February 26th, 2010. No Comments.

Lorem Ipsum Bookmarklet

I often use a couple of paragraphs of Lipsum when testing forms containing <textarea> tags and have had this bookmarklet I cobbled together sitting on my Bookmarks Toolbar for a while now.

Anyway, I thought it might be fun to hook it up to a HTML 5 form to let you customise how many paragraphs of Lipsum you want each time you click it. So that’s what I went and did:

Lorem Ipsum Bookmarklet Generator

Hopefully it’ll save you a few mouse clicks next time you’re testing some forms.

(Please note that the form used in the generator uses the <input type="range" /> HTML 5 tag which currently works best in the latest version of Opera. YMMV when using other browsers).

Posted in devtools at October 16th, 2009. No Comments.

jQuery Form Focus Plugin Released

I have released the first version of my jQuery Form Focus plugin. This lets you set the initial focus on any form element but is careful not to set the focus if the user has already started filling in the form. To use it you call something like:

$('#username').formFocus();
$('form input:first').formFocus();
$('form#options input[type="checkbox"]:first').formFocus();

Hope you find it useful.

Posted in javascript at June 9th, 2008. No Comments.

OpenSearch Added to Unobtrusive JavaScript Google Co-op Search

I have added OpenSearch to my Google Co-op Search Engine Unobtrusive JavaScript. Now you use the search engine in OpenSearch aware browsers you can add it to the browser’s search box.

Posted in javascript at February 5th, 2008. No Comments.