The best kittens, technology, and video games blog in the world.

Saturday, May 22, 2010

Greasemonkey and jQuery easier than ever

Douc Langur (Pygathrix nemaeus) by ucumari from flickr (CC-NC-ND)

Last year I wrote a short Greasemonkey tutorial, in which I explained how to use it with jQuery for some really simple scripts. Since then it became even easier, so here a few more useful scripts.

Since version 0.8 Greasemonkey has @require feature, in which your scripts can be made to depend on some external Javascript files - like jQuery. Unfortunately there are a few gotchas:
  • jQuery 1.4 doesn't work with Greasemonkey, you need to use jQuery 1.3 (or keep pestering them until it's fixed)
  • @require only works on installation time, you cannot use it with "New User Script..." feature, nor can you change @requires from the editor. If you simply use published scripts and don't write anything - you don't need to worry. If you write your own scripts you need to prepare something.user.js file somewhere, then open it from Firefox (copy&paste it's file path to Firefox URL bar, then click Install).

Show spoilers on tvtropes

Let's start with something really trivial. I don't care about spoilers. Maybe 1% of "spoilers" significantly diminish viewing pleasure, vast majority of them do not. I knew that Vader is Luke's father, that Snape killed Dumbledore, and that Titanic sunk before watching/reading, and it made it no less enjoyable.

On the other hand I'm quite annoyed that to read tvtropes I needed to constantly select "spoiler" text for everything to see it - but no more. This trivial script solves it entirely:

// ==UserScript==
// @name           Show all spoilers on tvtropes
// @namespace
// @include*
// @require
// ==/UserScript==


It's really simple - @name is unique name for the script, which should really be a very short description. We're not going to rely on @namespace at all, so put anything you feel like there.

@include is a pattern of URLs for which the script should be executed. @require is the jQuery library we're including. And thanks to jQuery, removing spoiler tags is really easy.

You can download this script here.

Always sort by seeders on The Pirate Bay

And extremely annoying thing about The Pirate Bay search engine is how it sorts results by its idea of "relevance" - usually giving you some dead ISO of three year old Ubuntu version when you obviously want the most recent one. And nearly always sorting by seeders is the right thing to do.

There is actually a GreaseMonkey script that claims to do exactly that, but it doesn't always work correctly, and the author decided to obfuscate the source for lulz. I'll have none of that, so I just wrote my own. Again, thanks to jQuery it's truly trivial.

// ==UserScript==
// @name           Always order by seed count
// @namespace
// @include*
// @require
// ==/UserScript==


You can download this script here.

Download Stewart and Colbert from bittorrent

Now a more complicated and perhaps more useful script. One of the recent developments on the Internet I hate the most are geographic restrictions - Americans can view whatever they like, for everyone else it's "Sorry, Videos are not currently available in your country" or other such nonsense.

Personally I don't care about licensing restrictions which led to this - I'm not going to accept development like that if I can help it. Fortunately in this case I can - all Stewart and Colbert is available on bittorrent.

The script does the following:
  • Find all dates on the page. They're not kind enough to use consistent class for those, I found at least 3, and perhaps I still missed a few.
  • Convert every date to from "Month DD, YYYY" to "YYYY MM DD" format, as this is what most torrents use. Half of the script is just doing that because Javascript doesn't have anything like Ruby's Date.parse.
  • Append link to bittorrent site next to the date. I'm using yourbittorrent here, as ThePirateBay is down due to overload far too often.
So every time someone on Reddit posts a "omg Colbert totally pwned Glenn Beck" link, I can actually see the pwnage now thanks to this script.

// ==UserScript==
// @name           View Stewart and Colbert on bittorrent
// @namespace
// @description    No more "Sorry, Videos are not currently available in your country"
// @include*
// @include*
// @require
// ==/UserScript==

var month_names = {
  'January': '01',
  'February': '02',
  'March': '03',
  'April': '04',
  'May': '05',
  'June': '06',
  'July': '07',
  'August': '08',
  'September': '09',
  'October': '10',
  'November': '11',
  'December': '12'

$(".date, .airDate, .clipDate").each(function() {
  var txt = $(this).text();
  var m = txt.match(/(\S+)\s*(\d+),\s*(\d{4})/);
  var date = m[3] + "+" + month_names[m[1]] + "+" + m[2];
  var url;
  if(window.location.hostname == "") {
    url = '' + date;
  } else {
    url = '' + date;
  $(this).append("<div><a href='"+ url + "'>Download on bittorrent</a></div>");

You can download the script here.

See how easy it all was? Now start writing your own scripts and share them.


tjgrathwell said...

How do you debug scripts that @require jQuery?

I'd like to test out my jQuery-isms in the Firebug console, but it seems $ doesn't exist (I guess it's only installed into the Greasemonkey sandbox).

A way around that would be to breakpoint in to the Greasemonkey code with Firebug but Firebug doesn't seem to see it.

Soo... console.log() ?

taw said...

tjgrathwell: Debugging greasemonkey anything is pretty hard - JS in website and JS in greasemonkey are completely different programs in completely different contexts, except greasemonkey program can mess in page content.

Firebug operates within page context not greasemonkey context.

* console.log works (assuming you have firebug installed).
* If you just want to look around the page and copypasta what you find into greasemonkey, use firequery to inject jquery into page content, and happily hack away from firebug.
* I guess you could inject $ from greasemonkey context into page context, I have no idea how much the security sandbox would like it.

Together, they work well enough unless you're doing something very complicated. This is all right, as most thing you want to do with Greasemonkey are fairly simple, especially with jQuery.

maddy said...

yea i agree u peoples completely .....nice discussion going pver really nive views been discussed .........

Rory O’Kane said...

Uh, you don’t need a Greasemonkey script to show spoilers on TVTropes. Just go to your profile and next to "spoiler default", choose “visible”.

taw said...

Rory O’Kane: tvtropes has such options? Nice.

On the other hand, greasemonkey+jquery is so easy that it simply takes less time to write a script like that than to search for options ;-)

Anonymous said...

Regarding your comment:

"Americans can view whatever they like, for everyone else it's "Sorry, Videos are not currently available in your country" or other such nonsense."

I am an American and while this seems mostly true, I cannot stream videos from the BBC website. I'm a Doctor Who fan and the web-only content is inaccessible. It's a shitty system and seems contrary to the entire idea of the internet.