Return to Snippet

Revision: 5648
at March 22, 2008 18:33 by scriptmakingcom


Initial Code
var myExtension = {

    // This allows you to disable the listener
    // without having to remove the script file from
    // your xul overlay every time
    enabled: true,

    // Log info on each loaded/unloaded page to the console?
    dumpToConsole: false,

    // Display an alert box displaying page info as each page loads/unloads?
    enableAlerts: true,

    onWindowLoad: function(e){
        // Should we even bother?
        if(!this.enabled) return;

        // Get some useful services
        this.cs = Components.classes["@mozilla.org/consoleservice;1"]
                            .getService(Components.interfaces.nsIConsoleService);
        this.ps = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
                            .getService(Components.interfaces.nsIPromptService);

        // What end of line character do we use?
        this.eol = this.getEol();

        // Note that it is necessary to wrap our onPageLoad function
        // and explicitly state "myExtension" here instead of just using
        // the "this" keyword

        // Notice that I use gBrowser here because
        // window.gBrowser == document.getElementById("content")
        // If gBrowser is not defined then you have bigger problems than your extension.       

        // Add our page listener(s)
        //gBrowser.addEventListener("DOMContentLoaded", function(e) { myExtension.onPageLoad(e); }, true);
        gBrowser.addEventListener("load", function(e) { myExtension.onPageLoad(e); }, true);


        // Notice we use our onPageLoad even for unload
        // because we can tell the type of page event by event.type
        // This uses less code
        gBrowser.addEventListener("unload", function(e) { myExtension.onPageLoad(e); }, true);
    },

    onPageLoad: function(e) {
        if(e.originalTarget instanceof HTMLDocument) {
            var doc = e.originalTarget;
            if(!doc.defaultView.frameElement) {
                // For top level documents only
                var title = (doc.title || "<Untitled>");
                var message = "Page Event: \"" + e.type + "\"" + this.eol + title + this.eol + doc.documentURI;
                if(this.dumpToConsole) {
                    this.cs.logStringMessage("myExtension" + this.eol + message);
                }
                if(this.enableAlerts) {
                    this.ps.alert(window,"myExtension",message);
                }
            }
            // Do something depending on the event type
         /* switch(e.type) {
                case "DOMContentLoaded": break;
                case "load": break;
                case "unload": break;
                default: break;
            } */
         }
     },

    getPlatform: function() {
        var platform = navigator.platform.toLowerCase();
        if(platform.indexOf('win') != -1) return 'win'; // win
        else if(platform.indexOf('mac') != -1) return 'mac'; // mac
        else return 'other'; // other (nix)
    },
    getEol: function() {
        switch(this.getPlatform()) {
            case 'win': return '
';
            case 'mac': return '\r';
            case 'other': return '\n';
            default: return '<eol>';
        }
    }
};
window.addEventListener("load", function(e) { myExtension.onWindowLoad(e); }, false);

Initial URL
http://forums.mozillazine.org/viewtopic.php?t=606756&postdays=0&postorder=asc&postsperpage=15&start=15

Initial Description


Initial Title
Detecting Page Onload, FF EXTENSION DEV

Initial Tags


Initial Language
JavaScript