views:

709

answers:

2

Hi,

Im working on a FireFox extension that listenes to OnStateChange. When the current document has been loaded it should insert a script to the page and it should be able to call the script on a button event.

Now i am able to add a button to all webpages by using:

nsCOMPtr NewInputElementTest; rv = htmlDoc->CreateElement(NS_LITERAL_STRING("input"),getter_AddRefs(NewInputElementTest));

rv = NewInputElementTest->SetAttribute(NS_LITERAL_STRING("type"),NS_LITERAL_STRING("button"));

rv = NewInputElementTest->SetAttribute(NS_LITERAL_STRING("value"),NS_LITERAL_STRING("hummer"));

rv = body->AppendChild(NewInputElementTest,getter_AddRefs(AddedNewInputElement2));

The button is displayed correctly.

I wish to use the same procedure to add a SCRIPT to the page, like so:

rv = htmlDoc->CreateElement(NS_LITERAL_STRING("script"),getter_AddRefs(NewInputElement)); rv = NewInputElement->SetAttribute(NS_LITERAL_STRING("type"),NS_LITERAL_STRING("text/javascript")); rv = NewInputElement->SetAttribute(NS_LITERAL_STRING("text"),NS_LITERAL_STRING("alert('hello world!')")); rv = body->AppendChild(NewInputElement,getter_AddRefs(AddedNewInputElement));

All functions return success, but no script is added to the page. No alert is displayed, and if i insert a function and call it from the button.onclick then the FireFox log displayes that the function is not available.

If i use the exact same procedure from a javascript inside the html page, then it works find and the alert pops up.

Do i need to do anything to enable the script from my extension or why is the script not available from the button or anywhere else?

Thanks

+2  A: 

I hate to say it after you created a bunch of code, but check out Greasemonkey: https://addons.mozilla.org/en-US/firefox/addon/748

It'll probably handle a lot of your work for you.

Oren Mazor
Seems like using greasemonkey would require that that addon is already installed. Is there no way of adding my own script using my already loaded addon?
elnino
A: 

Yes, sounds like you're tryin to re-invent the wheel. Use Greasemonkey as Oren suggested.

Here is a Greasemonkey script that I use to load external JS framework (Prototype and Scriptaculous in this case) load any number of external files (js and css) into a page.

// ==UserScript==
// @name           External Loader
// @namespace      http://ifelse.org
// @description    Loads external JS and CSS
// @include        http://*.yoursitedomainetc.com/*
// ==/UserScript==

var hasPrototype  = ('Prototype' in unsafeWindow);
var hasEffects    = ('Effect'    in unsafeWindow);

function _require(url, isCSS) {
    if (isCSS) {
        var script = document.createElement('link');
        script.setAttribute('type', 'text/css');
        script.setAttribute('rel',  'stylesheet');
        script.setAttribute('href', url);
    } else {
        var script = document.createElement('script');
        script.setAttribute('type',    'text/javascript');
        script.setAttribute('charset', 'UTF-8');
        script.src = url;
    }
    document.getElementsByTagName('head')[0].appendChild(script);
}

//  Load prototype; shouldn't get here because it is already on the page
if ( !hasPrototype ) {
    _require('http://path.com/to/prototype/1.6.0.2/prototype.js');
}

//  Load scriptaculous effects if it's not already loaded
if ( !hasEffects ) {
    _require('http://path.com/to/scriptaculous/1.8.1/effects.js');
}

//  Add greasemonkey ajax object
//  Copies format of Prototype Ajax.Request to
//  Allow to easily swap out at a later point (i.e. no longer FF plugin)
unsafeWindow.Remote = new Object;
unsafeWindow.Remote.Ajax = function(url, options) {
    if (options.onCreate) {
        options["onCreate"]();
    }

    var request = {
        method: options.method || 'get',
        url: url + ('?' + unsafeWindow.Object.toQueryString(options.parameters) || ''),
        onload: function(response) {
            if (response.status == 200)
            options["onComplete"](response);
            options["onSuccess"]();
        },
        onerror: options.onFailure || null
    };
    window.setTimeout(GM_xmlhttpRequest, 0, request);
};

//  Load these External files
_require('http://path/to/anything/and/dont/cache/it.js' + '?cache=' + (new Date()).getTime());
_require('http://paht/to/something/else.css', true);
}
michael
Thanks for the help. Im new to this, perhaps you can help me further.I need to load a script from another application or a script located on the disc, not from a URL is this a problem?Also does users of my application / script require having greasemonkey installed beforehand?The attached script, where do i put it in order for it to load when any page is done loading?A little more detail for a newbie would be great:) i'll check out the greasemonekey in the meantime:)Thanks.
elnino
If you know the specific script or code to include and don't need to load it remotely, then you could replace the script above with its content and have it run instead.Users would normally need to install greasemonkey and the script to run, but you can also compile greasemonkey scripts into Firefox Plugins.http://arantius.com/misc/greasemonkey/script-compiler
michael
So let me get this straight.I need to create an extension that will load a file ie. c:\myscript.js and insert this script into any webpage that FireFox visits.Is this possible by creating a GreaseMonkey script and compiling it into an xpi file? And once i have an xpi file what do i do with it?Thank you.
elnino
The XPI file is the Firefox extension, which you and users can add to Firefox like you would any other extension.
michael
I'll give it a shot, thanks:)
elnino