Greasemonkey script: Pitchfork links

Last.fm links on Pitchfork I read a lot of record reviews over at Pitchfork, and when something sounds good, I usually jump over to Last.fm to listen to it, or to remind myself to check it out again later. For a long time, I've been meaning to write up a Greasemonkey script for Firefox to generate the Last.fm link for me, so I don't have to type it in every time. You know, because that's so hard.

Anyway, I'm working on an, ahem, not tremendously exciting project at work, so I finally got around to it. I went ahead and made it extensible for other links besides Last.fm, so it can do links to iLike and Wikipedia, too; others are easy to add if you're reasonably familiar with Javascript. The script adds the links right next to the album artwork, right below the links to Emusic, digg, del.icio.us, and so on.

I posted it on userscripts.org. I currently have a hundred or so back reviews to read, so hopefully I'll have saved net keystrokes by the time I catch up.

Update: added links to IsoHunt, Mininova, and the Pirate Bay, by request.  I can't test right now because I'm on Firefox 3 RC2 and Greasemonkey doesn't work there yet.  But you can get the new version right here for now.  I will update it on userscripts.org as soon as I get it tested.


Tags: , , , , , , , , , , ,
Comments (2)

I'm not a math geek, but…

I'm not a math geek, but occasionally something piques my interest. Today I was trying to work out a good way to do something randomly about once every nine or ten times. I decided to check the last digit of the return value of Javascript's Math.random() function, which returns a number between 0 and 1. Like this:

0.1809368982206232

I knew it would never be zero, since the trailing zero would be removed; so I figured I could pick any other digit and I'd be right about once every nine times.

Turns out it doesn't work like that. The frequency distribution of the last digit is uneven.

<script type="text/javascript">
for (var num=1; num<=9; num++) {
var match = 0;
var re = new RegExp(num+'$');
for (var i=0; i<10000; i++) {
if (re.test(Math.random())) { match++; }
}
document.write("<p>" + num + ": " +
(match/i*100+'').substring(0,5) + "%</p>");
}
</script>

This script calls Math.random() 10,000 times for each digit from 1 to 9. It prints the last digit match rate as a percentage. Its output looks like this:

1: 8.790%
2: 10.4%
3: 12.19%
4: 13.17%
5: 13%
6: 12.95%
7: 11.51%
8: 9.35%
9: 8.58%

You'd expect something around 11.11% (one of nine) for each digit; but you can see that the frequency increases until 4, and then decreases again. In fact, this looks a heck of a lot like a bell curve, ergo, a Gaussian distribution.

I found that if I check the first digit after the decimal point, I get what I'm expecting: a list of roughly equal values somewhere around 11.11% for each digit. So although the output of Math.random() is uniformly distributed, the last digits are normally distributed.

I can't grok this much further than that; perhaps it's got something to do with the way the numbers are generated, or maybe that's just The Way Things AreTM. If anyone can explain to me why this happens, leave a note. Maybe I'll go poke around in the Gecko source code or something.


Tags: , , ,
Comments (6)

Wordpress plugin: Batch image uploader 0.2

I've gotten a great response from my initial release of the Batch image uploader. So let me first say thanks to everyone who left their comments and suggestions.

Today I'm releasing the next version of the plugin - Batch image uploader 0.2. The killer feature this time around is the ability to use different "backends" to resize the images. (This was initially suggested by Alexander Radsby.) There are three supported backends right now: GD, mogrify, and imagick.

GD is PHP's default image processing library. It's fast, but the quality of the images is not so good. This is what version 0.1 of the plugin used, and it's still an option in version 0.2.

mogrify has nothing to do with me, other than being the program from which I chose my Internet identity many years ago. It's actually one of the programs in the ImageMagick distribution of command-line image processing tools. This backend offers far better image quality, but is most likely slower, than GD. To use this, you have to have ImageMagick installed, and you have to tell my plugin where to find the mogrify binary. On UNIX systems, this is nearly always /usr/bin/mogrify, so that's the default.

Finally, imagick is the imagick PECL extension for PHP. It has the same image quality as the mogrify program, but is likely to be a great deal faster. This is because it uses ImageMagick libraries instead of invoking a program, so there is none of the overhead involved with starting another shell process.

You can force the plugin to use any one of these backends, but I recommend using the 'Auto' option. This will automatically select the best available backend, in this order: imagick, mogrify, GD. You can figure out which one it's using if you enable the debugging info.

With ImageMagick support comes support for dozens of image formats. The GD backend still only supports PNG, JPG, or GIF images.

The download and other current information is at the Batch image uploader page.


Tags: , , , , , , ,
Comments

Wordpress plugin: Batch image uploader 0.1

One of my other sites is a baby blog that my wife and I maintain for friends and family to keep in touch and read about my daughter. We're routinely uploading about 20 images per post. Until recently, the site was running Wordpress 1.2, and the uploading was handled by a fiercely hacked copy of Wordpress' image upload page that allowed an arbitrary number of images to be uploaded at once.

I updated the site to Wordpress 2 last weekend, so I decided to do the image uploading page properly, as a plugin. It seemed to work out, so I'm posting the result now.

Update: Information on the Batch image uploader plugin is now on its own page.


Tags: , , , ,
Comments (36)

Download Google Video with a bookmarklet

Sometimes you find something on Google Video that you want to download and keep for ever and ever. I found some instructions on how to do this, but it's an awful lot of trouble - look at the source code, find the URL, unescape it… but it inspired me, so I wrote up a javascript bookmarklet.

Here goes:

javascript:void(window.location=unescape(/http.*dgvuniqueid/.exec(document.documentElement.innerHTML)[0]));

There. Create a bookmark with that ugly mess as the location/URL - all on one line (copy and paste it - it should work). Then, when you're on the video page, hit the bookmark, and you'll start downloading a Flash Video file (which you can play in a number of different ways, including with VLC 0.8.4a and newer).

Tested with exactly two videos. This depends somewhat on the way Google formats their page, so it could break at any time. Also, don't pirate anything.

Update: You can drag this link to your bookmarks toolbar to make a shiny Google Video button: Grab Video. Also, it seems that VLC does not yet play the newer Flash 8 videos encoded with the On2 VP6 codec - only the Flash 7 ones that use Sorenson Spark. I don't know if Google uses On2 videos yet, but anyway, here's a very nice standalone FLV player that can handle both types.

Update again: You are using Video Downloader, aren't you?


Tags: , , , , , , ,
Comments (3)

click tracking

A while back I was at a small conference in Charlottesville for public affairs types that work at environmental agencies in our region. One of the EPA webmasters gave a presentation about their site. She pointed out that they track the clicks on their website by logging each click in a database. In this way, they can tell which stories are the most popular, and which areas of the page have the highest value and are most visible to users. It has given them data that helps them design their pages.

I liked that idea, so I went home and started to write my own click tracker. What I didn't like about their setup was that each link pointed, not to the actual page that the user wanted, but to a CGI program called "epalink" that would (apparently) log the click, and then bounce the user to the page they really wanted. It's fairly transparent to the users, but I saw a few problems:

  • The URL in the status bar is long and ugly
  • The page maintainer has to remember to format the links in a certain way
  • If applying to a large existing site, changing all the links would be nightmarish

I wanted to work out a way to track links on a page without changing any page body code at all. Since my current interest is JavaScript development, I jumped in and tried to figure out a way to do this the way I wanted.

Read the rest of this entry »


Tags: , , , ,
Comments (1)

mog.rif.icio.us google homepage module

Google announced yesterday that they would provide an API for people to develop their own modules for the Google personalized homepage.

So, to try it out, I wrote one for mog.rif.icio.us. As promised, it was pretty easy. It uses your existing WordPress site with the mog.rif.icio.us plugin already installed. All that's needed is a wrapper script that places the content in a chunk of XML so Google knows how to handle it.

As a demo, you can add a module to the personalized homepage using my URL:

http://code.mogrify.org/wp-content/mog-google.php

It will initally appear as an iframe with a scroll bar, but you can make it display inline on the page, too. Check out the API documentation for how to do this.

I've got more detailed information and instructions on the mog.rif.icio.us project page.


Tags: , , , , ,
Comments (2)

introducing mog.rif.icio.us

As part of setting up this site, I wrote a plugin for WordPress to display all my del.icio.us bookmarks in the sidebar. It will check once a day to see if any changes have been made, and download the bookmarks if necessary.

Bookmarks are displayed as a DHTML menu, so that you can expand one tag at a time to see all the bookmarks underneath.

I set up a project page for the plugin, which I call mog.rif.icio.us. It's designed as a WordPress plugin, but it should work in any PHP environment (4.3 for now, no PHP5 support yet). You'll be able to download it from there, with more detailed documentation.


Tags: , , , , , ,
Comments (1)

Bad Behavior has blocked 202 access attempts in the last 7 days.