Some assembly required

This blog is about unimplemented ideas. At least until they get ticked off; I suppose a few eventually will have implementations too, but fresh posts never will. Because that's the primary purpose of this blog: keeping track of ideas I'd like to dive into, or problems I'd like to see solved. Feel free to join me in implementing, or further developing these ideas. I don't mind working solo, but it's a whole lot more fun working in concert!

Saturday, February 23, 2008

Greasemonkey @require libraries

Problem:

There is a shortage of well abstracted micro-libraries to @require in Greasemonkey scripts.

Solution:

Define useful, minimal feature sets, and implement and/or advertise their specs to prospective other implementors.

Interfaces:


$x(xpath, root) / $X(xpath, root)

$x evaluates an XPath expression, returning an Array of nodes, a string, number or boolean, depending on the return type it evaluates to. $X returns the first match only (in document order). Both take an optional context node to resolve the expression from.

wget(url, cb) / wget$x(url, cb, xpath) / wget$X(url, cb, xpath)

wget does a GM_xmlhttpRequest fetch of an entity and renders it into a DOM that can be queried by XPath, and gets passed on to the callback (second parameter: url). The wget$x methods also do the slicing and pass the $x result, the document, url and xhr object to cb.

MD5 singleton

MD5.string(data) yields the binary MD5 hash, MD5.hex(data) will hex code it prior to returning the result. MD5.base64(data) similarly for Base64 encoding.

SHA1 singleton

Like MD5, but for SHA1 instead, and the string method is called "hash". Also takes an optional second argument 8 or 16 for string width. MD5 ought to be updated to mimic this API instead.

Diff singleton

Renders a text/html diff of two text/plain (unfortunately) inputs. Usage:
var diff = Diff.diff_main(text1, text2);
Diff.diff_cleanupSemantic(diff);
var html = Diff.diff_prettyHtml(diff);


Notes:

  • Incomplete.

  • Update incrementally.