Listening to tabs and close/log tabs with errors

Talk about add-ons and extension development.
Post Reply
RaymondNL
Posts: 3
Joined: May 29th, 2016, 2:18 pm

Listening to tabs and close/log tabs with errors

Post by RaymondNL »

Hello all,

I receive a large amount of emails. I process them with a Thunderbird extension i wrote. Thunderbird collects links from them and opens filtered links in Firefox (45.1.0 ESR). I have set the extension to open a max of 50 links at a time. Thunderbird shows a confirmation window to continue for the next batch.

Now I'm working on an extension for Firefox that watches all opened tabs. If a tab is (completely) loaded i want the script to check if the page contains an error message. if it contains a predefined message, the url should be logged and the tab must be closed. The tabs that are left are the pages that I need to check manualy. I came up with the following code, but that does not seem to work. I tried different ways but can't get it to work. Here is my code (I used a browseroverlay to load the script):

Code: Select all

const STATE_START = Components.interfaces.nsIWebProgressListener.STATE_START;
const STATE_STOP = Components.interfaces.nsIWebProgressListener.STATE_STOP;

var closeTitles = ["400 Request Header Or Cookie Too Large",
                    "400 Bad Request"];

var checkText = ["Item does not exist!",
                 "This item is sold!",
                 "More to come"];

var myListener = {
    QueryInterface: XPCOMUtils.generateQI(["nsIWebProgressListener",
                                           "nsISupportsWeakReference"]),

    onStateChange: function(aWebProgress, aRequest, aFlag, aStatus) {
        if (aFlag & STATE_START) {
            // Do nothing
        }
        if (aFlag & STATE_STOP) {
            var tab = aWebProgress.DOMWindow;
            var docBody = tab.window.document.body;
            if (closeTitles.indexOf(tab.title) > -1) {
                console.log("Closed tab (400 error): " + getTabURL(tab));
                gBrowser.closeTab(tab);
            };
            var getId = docBody.getElementById("AuctionEnded");
            if (getId.length > 0) {
                console.log("Item not available: " + getTabURL(tab));
                gBrowser.closeTab(tab);
            }
            checkText.forEach(function (a, i) {
                if (docBody.indexOf(a) > -1) {
                    console.log("Item not available: " + getTabURL(tab));
                    gBrowser.closeTab(tab);
                };
           });
        }
    }
}

gBrowser.addProgressListener(myListener);
What is the right way, or might be an easier way, to make this work?

Greets Ray.
Post Reply