I'm making an extension for firefox android and when I test it, it works fine, then if I shut down firefox or don't use it for a while, the extension doesn't work unless reactivated again. The thing is that the added items menu are there but the extension doesn't work (it is supposed to check some properties in the document of the page when loaded or selected, then send a request to my localhost). Do you have any idea why this happens?
Here the code where I add the menu items and the listeners:
function loadIntoWindow(window) {
if (!window)
return;
if (isNativeUI()) {
//adding the items to the menu
icoUrl = gAddonData.resourceURI.spec + "iconMenu.png"; //the icon uri
var tabAddr=gAddonData.resourceURI.spec+"/content/t.html"; // the uri of a page about the extension
//the item opens the page and displays a toast
menuIdInfo = window.NativeWindow.menu.add("About TrackDetect",icoUrl, function({
window.BrowserApp.addTab(tabAddr);
showToast(window); });
// This menu item shows the details collected about the docHTML of currently visited page, these details are brought by the listeners ("TabSelect" and "pageshow")
gDoorhangerMenuId = window.NativeWindow.menu.add("Show details ", icoUrl,
function(){ //doc is global variable
if (doc==null) {window.NativeWindow.toast.show("Please REACTIVATE TrackDetect extension.", "long");}
else {showDoorhanger(window, trackersSum);}});
}
//adding the listeners
window.BrowserApp.deck.addEventListener("TabSelect", function(){ watchTab(window);}, false);
let addListener = function() {
window.BrowserApp.deck.addEventListener("pageshow", function(){ watchTab(window);}, false);
window.NativeWindow.toast.show("Starting to detect trackers.", "long");
};
if(window.BrowserApp.deck) {
// BrowserApp.deck has been initialized
addListener();
}
else {
// use the chrome window to wait for BrowserApp to initialize
window.addEventListener("UIReady", addListener);
}
}
Actually the error that I get is:
Error: "TypeError: window.BrowserApp.deck is null"
and I need the window.BrowserApp.deck to add the event Listeners like showed in the code above. Then, I call the method in the startup:
var windowListener = {
onOpenWindow: function(aWindow) {
// Wait for the window to finish loading
let domWindow = aWindow.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowInternal || Ci.nsIDOMWindow);
domWindow.addEventListener("load", function() {
domWindow.removeEventListener("load", arguments.callee, false);
loadIntoWindow(domWindow);
}, false);
},
onCloseWindow: function(aWindow) {
},
onWindowTitleChange: function(aWindow, aTitle) {
}
};
function startup(aData, aReason) {
gAddonData = aData;
setDefaultPrefs();
// Load into any existing windows
let windows = Services.wm.getEnumerator("navigator:browser");
while (windows.hasMoreElements()) {
let domWindow = windows.getNext().QueryInterface(Ci.nsIDOMWindow);
loadIntoWindow(domWindow);
}
Services.scriptloader.loadSubScript(aData.resourceURI.spec + "content/scanner.js", TrackScanner);
Services.scriptloader.loadSubScript(aData.resourceURI.spec + "content/storageReq.js", TrackReq);
Services.wm.addListener(windowListener);
obs.addObserver(httpRequestObserver, "http-on-modify-request", false);
obs.addObserver(httpResponseObserver, "http-on-examine-response", false);
try {
alertsService.showAlertNotification(gAddonData.resourceURI.spec+"skin/oeil1.png",
"TrackDetect activated", "Detecting web Tracking.",
false, "", null, "");
} catch (e) {
// if the extension fail to start
alertsService.showAlertNotification("",
"Please REACTIVATE Trackdetet", "Please REACTIVATE Trackdetet.",
false, "", null, "");
}
if (aReason== ADDON_INSTALL) {
//some code to set a cookie
}
if ((aReason== APP_STARTUP)||(aReason== ADDON_ENABLE)||(aReason== ADDON_UPGRADE)) {
//some code to get a cookie
}
}
Then in the shutdown:
function shutdown(aData, aReason) {
if ((aReason == APP_SHUTDOWN) || (aReason == ADDON_DISABLE) || (aReason == ADDON_DOWNGRADE) || (aReason == ADDON_UNINSTALL))
{ // code to make sure that the cookie I set is there
}
//cleaning up (removing observers and the window listener)
// Stop listening for new windows
Services.wm.removeListener(windowListener);
obs.addObserver(httpRequestObserver, "http-on-modify-request", false);
obs.removeObserver(httpResponseObserver, "http-on-examine-response", false);
// Unload from any existing windows
let windows = Services.wm.getEnumerator("navigator:browser");
while (windows.hasMoreElements()) {
let domWindow = windows.getNext().QueryInterface(Ci.nsIDOMWindow);
unloadFromWindow(domWindow);
}
}
I hope that now, you can help me more, because I don't know why the window.BrowserApp.deck becomes null when I shut the browser down or don't use it for a while. Thank you.
the error was caused by the tabSelect event listener, it was added in the wrong place, to make it work properly, I did this:
//adding the listeners
let addListener = function() {
window.BrowserApp.deck.addEventListener("TabSelect", function(){ watchTab(window);}, false); window.BrowserApp.deck.addEventListener("pageshow", function(){ watchTab(window);}, false); window.NativeWindow.toast.show("Starting to detect trackers.", "long"); };
if(window.BrowserApp.deck) {
// BrowserApp.deck has been initialized
addListener();
}
else {
// use the chrome window to wait for BrowserApp to initialize
window.addEventListener("UIReady", addListener);
}
Then of course I removed these listeners later.