JavaScript 检测页面上载,FF扩展DEV

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaScript 检测页面上载,FF扩展DEV相关的知识,希望对你有一定的参考价值。

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);

以上是关于JavaScript 检测页面上载,FF扩展DEV的主要内容,如果未能解决你的问题,请参考以下文章

使用 javascript 进行可靠的浏览器检测?

如何在不重新加载页面的情况下从 FF Web 扩展内容脚本更改 3rd 方网站上的 Angular 应用程序路由/URL

Javascript:验证文件上载

javascript EV-上载和zip.js

javascript 模态弹出式上载脚本

PHP - 文件上载后执行JavaScript