[Greasemonkey] Re: Including External Scripts.
Andre
gm at andrecgn.de
Tue Feb 21 17:17:55 EST 2006
There are 2 bugs in the code that I noticed. Furthermore, what is the
included code?
bug1 (not the cause that nothing happens):
window.addEventListener('load', includeScript(), false);
should be:
window.addEventListener('load', includeScript, false);
because the second parameter is supposed to be a function reference. You
are calling the function and passing the result to the addEventListener
call. But that only changes the timing of the execution of
includeScript. I think you don't need to wait until the page has loaded.
bug2:
element.onclick = function() { ...
The onclick Attribute of an element is not available in GM. you need to
use addEventListener here as well. all elements are XPCNativeWrapper
which do not expose 'onclick' or any other 'on...'.
Here is some documentation on that:
http://dunck.us/collab/GreaseMonkeyUserScripts?action=show&redirect=GreasemonkeyUserScripts#head-4ac4d1e80f8bbd66bf4f1fbea77ea2390b6a2870
cheers, Andre
Christopher E. Granade wrote:
> Andre wrote:
>> The code snippet referenced below points out the important difference.
>>
>> If you add the script tag to the DOM, it will be in a different name
>> space than where GM is running. You will have to reference it via
>> "unsafeWindow".
>>
>> Or you 'eval' the source of the script tag after it loaded, which will
>> add it to the GM sandbox.
>>
>> BTW the load event is not needed and not even used in the code snippet:
>> window.addEventListener('load', loadProto(), false);
>> "loadProto" should be used without parenthesis.
>>
>> When a GM injected script runs, the DOM is there and to add an element
>> you don't need to wait for the load event.
>>
>> Gavri Fernandez wrote:
>>> On 2/21/06, Christopher E. Granade <cgranade at greens.org> 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
>>> You need to wait for the library to be loaded
>>> http://manalang.com/archives/2006/01/04/loading-external-javascript-libraries-in-greasemonkey/
>>>
>>>
>>> Also, there's a mismatch in the case of the variable "myRules"
>>>
>>> --
>>> gavri
>>> http://ga-woo.livejournal.com/
>> _______________________________________________
>> Greasemonkey mailing list
>> Greasemonkey at mozdev.org
>> http://mozdev.org/mailman/listinfo/greasemonkey
>>
> I hate to bother everyone, but it is still giving me a lot of grief.
> Forgive my lack of knowledge, please, for I am new to DOM scripting and
> it gives me a bit of a headache at times. Anyway, the revised code is
> included below, and seems to not do anything when applied against a test
> file on my hard drive.
>
> BTW, if anyone was wondering about /why/ I'm doing this, I wanted to
> provide a means of making it so someone doesn't accidentally click on an
> NSFW link at while at work, and provide a reference implementation for
> my idea for the rel="nsfw" construction.
>
> --Chris
>
> // ==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 and Global Variables
> ///////////////////////////////////////////////////////////////////////
>
> 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;
> }
> }
> }
>
> var waitOb = null;
>
> ///////////////////////////////////////////////////////////////////////
> // 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() {
> var eleHead, eleScript;
>
> // Find the head.
> eleHead = document.getElementsByTagName("head")[0];
>
> // Create the new element.
> eleScript = document.createElement("script");
> eleScript.src = behaviorsURI;
> eleScript.type = "text/javascript";
>
> // Apply the new element.
> eleHead.appendChild(eleScript);
> }
>
> function waitForScript() {
> if (typeof waitOb == "undefined") {
> window.setTimeout(waitForScript, 100);
> } else {
> alert("Done!");
> unsafeWindow.Behaviour.register(myRules);
> }
> }
>
> ///////////////////////////////////////////////////////////////////////
> // Imperative Section
> ///////////////////////////////////////////////////////////////////////
>
> window.addEventListener('load', includeScript(), false);
> // wait for it to be included.
> waitOb = unsafeWindow.Behaviour;
> waitForScript();
More information about the Greasemonkey
mailing list