0

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

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

    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 。谢谢你。

4

1 回答 1

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

当然,我后来删除了这些听众。

于 2013-09-29T14:04:43.307 回答