为什么我的Firefox for android扩展程序自行停用?

时间:2013-09-06 16:23:05

标签: android firefox firefox-addon firefox-addon-sdk

我正在为firefox android做一个扩展,当我测试它时,它工作正常,然后如果我关闭firefox或暂时不使用它,扩展无法工作,除非再次重新激活。 问题是添加的项目菜单在那里,但扩展名不起作用(它应该在加载或选择时检查页面文档中的一些属性,然后向我的localhost发送请求)。你知道为什么会这样吗?

这里是我添加菜单项和监听器的代码:

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

实际上我得到的错误是:

Error: "TypeError: window.BrowserApp.deck is null"

我需要window.BrowserApp.deck来添加上面代码中显示的事件监听器。然后,我在启动时调用该方法:

    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
}
}

然后在关机中:

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

我希望现在,你可以帮助我更多,因为我不知道为什么window.BrowserApp.deck在我关闭浏览器或暂时不使用它时变为null。谢谢。

1 个答案:

答案 0 :(得分:0)

错误是由tabSelect事件监听器引起的,它被添加到错误的位置,为了使其正常工作,我这样做了:

//添加监听器

       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(“开始检测跟踪器。”,“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);
       }

然后我当然会在以后删除这些听众。