Firefox addon which refreshes the tap and auto res

2019-07-19 07:35发布

问题:

I'm trying make a firefox add-on, which reloads a page automatically under some conditions.

First I add a contentScript to a tab to get some information on the page in my addon

tab.attach({
            contentScript:self.port.emit...

I have it working to the point of the refresh with

tab.reload();

but then an alert pops up every time "if it should resend data".

I want to resend the data automatically. How and where do I do it? In the add-on or in the contentScript? Has it to do with the Load Flags constant?

回答1:

This clicks the accept button when the prompt opens. But you see the prompt window for like a couple hundred ms.

i had to use setTimeout with 0 wait time other wise the aDOMWindow.args property and aDOMWindow.Dialog and a bunch of other stuff would be undefined or null so weird. But this works:

var stringBundle = Services.strings.createBundle('chrome://browser/locale/appstrings.properties');

try {
    windowListener.unregister();
} catch (ignore) {}

var windowListener = {
    //DO NOT EDIT HERE
    onOpenWindow: function(aXULWindow) {
        // Wait for the window to finish loading
        let aDOMWindow = aXULWindow.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowInternal || Ci.nsIDOMWindow);
        aDOMWindow.addEventListener('load', function() {
            aDOMWindow.removeEventListener('load', arguments.callee, false);
            windowListener.loadIntoWindow(aDOMWindow, aXULWindow);
        }, false);
    },
    onCloseWindow: function(aXULWindow) {},
    onWindowTitleChange: function(aXULWindow, aNewTitle) {},
    register: function() {
        // Load into any existing windows
        let XULWindows = Services.wm.getXULWindowEnumerator(null);
        while (XULWindows.hasMoreElements()) {
            let aXULWindow = XULWindows.getNext();
            let aDOMWindow = aXULWindow.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowInternal || Ci.nsIDOMWindow);
            windowListener.loadIntoWindow(aDOMWindow, aXULWindow);
        }
        // Listen to new windows
        Services.wm.addListener(windowListener);
    },
    unregister: function() {
        // Unload from any existing windows
        let XULWindows = Services.wm.getXULWindowEnumerator(null);
        while (XULWindows.hasMoreElements()) {
            let aXULWindow = XULWindows.getNext();
            let aDOMWindow = aXULWindow.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowInternal || Ci.nsIDOMWindow);
            windowListener.unloadFromWindow(aDOMWindow, aXULWindow);
        }
        //Stop listening so future added windows dont get this attached
        Services.wm.removeListener(windowListener);
    },
    //END - DO NOT EDIT HERE
    loadIntoWindow: function(aDOMWindow, aXULWindow) {
        if (!aDOMWindow) {
            return;
        }
        if (aDOMWindow.location == 'chrome://global/content/commonDialog.xul') {
            var repostString = stringBundle.GetStringFromName('confirmRepostPrompt');
            var repostStringFormatted = stringBundle.formatStringFromName('confirmRepostPrompt', [aDOMWindow.Application.name], 1);
            aDOMWindow.setTimeout(function() {
                console.log('setimeout val 00:', aDOMWindow.args)
                    //aDOMWindow.args and aDOMWindow.Dialog is not available till after setTimeout of 0 so weird
                if (aDOMWindow.args.text == repostString || aDOMWindow.args.text == repostStringFormatted) {
                    console.log('this is resend prompt so accept it');
                    //aDOMWindow.Dialog.ui.button0.click(); //doesnt work
                    //aDOMWindow.Dialog.onButton0(); // doesnt work
                    //aDOMWindow.ondialogaccept(); //doesnt work
                    var dialog = aDOMWindow.document.getElementById('commonDialog');
                    var btnAccept = aDOMWindow.document.getAnonymousElementByAttribute(dialog, 'dlgtype', 'accept');
                    btnAccept.click();
                    console.log('clicked');
                }
            }, 0);
        }

    },
    unloadFromWindow: function(aDOMWindow, aXULWindow) {
        if (!aDOMWindow) {
            return;
        }
    }
};

windowListener.register();

open scratchpad. set environemnt to browser. run the code. refresh a page to get prompt you'll see it get clicked.