Tuesday, January 27, 2009

Mozilla Ubiquity: Metacritic Script

I developed this rarely used script to search Metacritic way back when, as my first Ubiquity script. Ever since I got it working I just kinda left it there. It's a total hack and it'll break when they change the way the page renders, but you, dear viewer, might find it useful.

Yes, Mozilla Ubiquity does rock. However, having no API to your site does not rock. It is the exact opposite of rock, in fact!


function __searchMetaCritic(pblock, searchText) {
jQuery.get("http://www.metacritic.com/search/process?sb=0&tfs=all&ty=3&x=0&y=0&ts=" + escape(searchText),
function(response) {
var resultsREGXP = /\[0-9]\.(.*)\<\/p\>/gmi;
var results = response.match(resultsREGXP);
found = true;

if (results == null) {
var msg = 'No results for "${what}"';
var subs = {what: searchText};

pblock.innerHTML = CmdUtils.renderTemplate( msg, subs );
return;
}
var tempElement = CmdUtils.getHiddenWindow().document.createElementNS("http://www.w3.org/1999/xhtml", "div");

tempElement.innerHtml = '<link href="http://www.metacritic.com/general.css" rel="stylesheet" type="text/css">';
tempElement.innerHtml = tempElement.innerHtml + results.join('');

var msg = 'Searching for "${what}"
${results}';
var subs = {what: searchText, results: tempElement.innerHtml};

pblock.innerHTML = CmdUtils.renderTemplate( msg, subs );
});

}

CmdUtils.CreateCommand({
name: "metacritic",
takes: {"what": noun_arb_text},
preview: function(pblock, what) {
__searchMetaCritic(pblock, what.text);
var msg = 'Searching for "${what}"';
var subs = {what: what.text};

pblock.innerHTML = CmdUtils.renderTemplate( msg, subs );

},
execute: function(what) { __searchMetaCritic(what); }
});

1 comment:

trentonknight said...

Hey, hows it going? I'm attempting to surf the web and find a variety of Ubiquity coders. I'm noticing allot of people are sharing there scripts. If you would like you are welcome to use our site at www.cipherhive.org . You can upload your javascript to folder and then put the link in a html block. That way when someone wants to install your script they can just view your page and opt to install it. We are fairly new so we are starting with what ever coders we can find to build the site up. Let me know what you think if you check it out. here is the Ubiquity tutorial...

http://www.cipherhive.org/course/view.php?id=19

Make sure to briefly review the Course Making Tutorial First just to learn how to add html blocks. It a no brainer and uses a simple gui.

http://www.cipherhive.org/course/view.php?id=11

V/r
Jason Mansfield