[Greasemonkey] Including External Scripts.
Christopher E. Granade
cgranade at greens.org
Mon Feb 20 18:04:35 EST 2006
-----BEGIN PGP SIGNED MESSAGE-----
Hash: RIPEMD160
Christopher E. Granade wrote:
> Thanks for the help. I tried to use the DOM-hack, and it doesn't seem to
> be running the code. I attached a script that I'm working on. Any
> suggestions on why it doesn't work?
> --Chris
Odd. Seems that the attachment was stripped. Including the User Script
below in plain text:
// ==UserScript==
// @name NSFW Link Blocker
// @description Uses JavaScript Behaviors to block links marked with
rel="nsfw".
// @include *
// ==/UserScript==
/**
* Compatibity note:
* Since this script is designed as a userscript for Greasemonkey,
we assume that the hosting browser is Firefox,
* version 1.0 or better. Thus, many appropriate checks and graceful
degragations are omitted. Do not use this code in a public-
* facing web page.
*/
///////////////////////////////////////////////////////////////////////
// Constants
///////////////////////////////////////////////////////////////////////
var behaviorsURI = "http://bennolan.com/behaviour/behaviour.js";
var myRules = {
// Place your user behaviors here.
'a[rel~="nsfw"]' : function(element) {
element.onclick = function() {
alert("This link is marked as non-worksafe. Please disable
Greasemonkey to proceed.");
return false;
}
}
}
///////////////////////////////////////////////////////////////////////
// Functions
///////////////////////////////////////////////////////////////////////
/**
* includeScript(uri)
**
* Modifies the DOM of the current page to include an external script.
**
* Preconditions:
* - uri is a String that contains a valid URI which dereferences to a
resource of
* one of the following MIME types:
* - text/javascript
* - text/ecmascript
* - application/x-javascript
* Postconditions:
* - The DOM of the current pagbe is modified to include a <script>
element in the document's <head>.
*/
function includeScript(uri) {
var eleHead, eleScript;
// Find the head.
eleHead = document.documentElement.getElementsByTagName("head")[0];
// Create the new element.
eleScript = document.createElement("script");
eleScript.setAttribute("src", uri);
// Apply the new element.
eleHead.appendChild(eleScript);
}
///////////////////////////////////////////////////////////////////////
// Imperative Section
///////////////////////////////////////////////////////////////////////
includeScript(behaviorsURI);
Behaviour.register(myrules);
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)
Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org
iD8DBQFD+oNC0dXuuZr00J4RAxNNAJ4q3k/HQV72TpbzgTs1aIPwDqoX4wCfeMPg
FGOFbdLqPkmr7AkfcBmeKLw=
=BtFC
-----END PGP SIGNATURE-----
More information about the Greasemonkey
mailing list