While writing user scripts, you may find JQuery a very powerful tool. It is a javascript library with many handy functions for extracting and manipulating HTML code in runtime.
In order to use JQuery within a user script, you may either download JQuery and embed it (being Minified version particularly insteresting in this case), or you may add some code to load JQuery with the actual page being fetched. I like the last better since it keeps my scripts smaller and cleaner, and linked to the latest version of JQuery. Unfortunately, this piece of code if incompatible with Chrome user scripts API.
Here is the modified version which works in Chrome:
// ==UserScript==
// @match http://*
// @run-at document-start
// ==/UserScript==
var jq_script = document.createElement("script");
jq_script.src = "http://jquery.com/src/jquery-latest.js";
jq_script.type = "text/javascript";
document.getElementsByTagName("html")[0].appendChild(jq_script);
function wait_for_jquery(attempts) {
try {
$ = window.jQuery || this.jQuery;
} catch(err) { alert(err); }
if ($ == null || typeof $ == "undefined") {
if (attempts > 0) {
window.setTimeout(wait_for_jquery, 250, attempts-1);
}
} else { letsJQuery(); }
}
wait_for_jquery(20);
function letsJQuery() {
alert($);
}
Core changes are:
// @run-at document-startdocument.getElementsByTagName("html")[0].appendChild(jq_script);window.JQuery rather than windowUnsafe.JQuery