本文介绍了Firefox插件pageMod,抓阿贾克斯contentScriptFile完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是,我需要知道什么时候AJAX在页面上完成的。我需要知道这在我contentScriptFile,我通过pageMod加载。我的需要,增加运行每一个页面已被修改的时间。所以基本上在加载时,每一次有一个AJAX调用

我想这样的:

  $(文件).ajaxComplete(函数(){
     警报(AJAX调用完成);
});
 

,但它不工作

有没有办法做到这一点?

编辑:我的网页MOD $ C $从main.js C:

  pageMod.PageMod({
     包括: *,
     contentScriptFile:[self.data.url(jQuery的-1.9.1.min.js),
     self.data.url(的script.js)],

     onAttach:功能(工人){

      VAR apiKey =要求(SDK /简单 -  preFS)prefs.apiKey。
      VAR ignoreList =要求(SDK /简单 -  preFS)prefs.ignoreList。

      worker.port.emit(prefSet,[ignoreList,apiKey]);

      }

});
 

解决方案

将这个没有内容的脚本:林不知道你将如何确定脚本的内容窗口。我不知道SDK这么好。但见面积 CONTENT_WINDOW_OF_CONTENT_SCRIPT

  const的{慈,铜,抄送,铬} =需要('铬'); // const的{接口:慈,utils的:铜,类:抄送,结果:铬} =组件;
Cu.import('资源://gre/modules/Services.jsm');
Cu.import('资源://gre/modules/devtools/Console.jsm');

VAR观察家= {
    HTTP上 - 修改 - 要求:{
        注意:功能(aSubject,特应性,ADATA){
            console.info(HTTP上 - 修改 - 要求:aSubject ='+ aSubject +'|特应性='+特应性+'| ADATA ='+ ADATA);
            VAR的HTTPChannel = aSubject.QueryInterface(Ci.nsIHttpChannel);
            VAR requestUrl = httpChannel.URI.spec
            VAR好吃的东西= loadContextAndGoodies(aSubject,真正的);
            如果(goodies.contentWindow){
               如果(goodies.contentWindow == CONTENT_WINDOW_OF_CONTENT_SCRIPT){
                    //做的东西在这里
               }
            }
        },
        章:函数(){
            Services.obs.addObserver(观察者['HTTP-上修改请求'],HTTP上 - 修改 - 要求,假);
        },
        UNREG:函数(){
            Services.obs.removeObserver(观察者['HTTP-上修改请求'],HTTP上 - 修改 - 要求);
        }
    }
};
 

然后在同一范围内添加在这个辅助函数:

 函数loadContextAndGoodies(请求,return_goodies){
  VAR loadContext = NULL;

  如果(请求的instanceof Ci.nsIRequest){
      尝试 {
          如果(request.loadGroup&安培;&安培; request.loadGroup.notificationCallbacks){
              loadContext = request.loadGroup.notificationCallbacks.getInterface(Ci.nsILoadContext);
          }
      }赶上(前){
        console.exception(的要求loadGroup与notificationCallbacks但oculd得不到nsIloadContext,前);
      }
      如果(!loadContext){
        尝试 {
            如果(request.notificationCallbacks){
                loadContext = request.notificationCallbacks.getInterface(Ci.nsILoadContext);
            }
        }赶上(前){
          console.exception(的要求有notificationCallbacks但无法得到nsILoadContext,前);
          / *启动 -  noit的备份尝试,它可能是多余的(我不知道)作为Wladamir Palant没有这样* /
          尝试 {
            VAR interfaceRequestor = httpChannel.notificationCallbacks.QueryInterface(Ci.nsIInterfaceRequestor);
            loadContext = interfaceRequestor.getInterface(Ci.nsILoadContext);
          }赶上(前){
            console.exception(备份失败的方法:前);
          }
          / *结束 - 我的备份尝试,它可能是多余的,因为Wladamir Palant没有这样* /
        }
      }
  } 其他 {
    console.warn(的要求的说法是不nsIRequest实例)
  }

  如果(return_goodies){
    如果(!loadContext){
      返回null;
    }

    VAR contentWindow = loadContext.associatedWindow;
    VAR DOMWindow = contentWindow.top.QueryInterface(Ci.nsIInterfaceRequestor)
                                     .getInterface(Ci.nsIWebNavigation)
                                     .QueryInterface(Ci.nsIDocShellTreeItem)
                                     .rootTreeItem
                                     .QueryInterface(Ci.nsIInterfaceRequestor)
                                     .getInterface(Ci.nsIDOMWindow);
    VAR gBrowser = DOMWindow.gBrowser;
    如果(gBrowser){
      VAR选项卡= gBrowser._getTabForContentWindow(contentWindow.top);
      VAR浏览器= tab.linkedBrowser;
    } 其他 {
      VAR标签,浏览器= NULL;
    }
    VAR好吃的东西= {
      loadContext:loadContext,
      DOMWindow:DOMWindow,
      gBrowser:gBrowser,
      contentWindow:contentWindow,
      浏览器:浏览器,
      标签:标签
    };

    回好吃的东西;
  } 其他 {
    返回loadContext;
  }
}
 

开始观察

要开始启动obseving所有的请求做到这一点(例如你的插件启动)

 为(变种o在观察者){
    观察家[O]的.reg();
}
 

停止观察

其重要停止observring(请确保至少在插件的停机运行它,你不想离开注册的内存原因观察者)

 为(变种o在观察者){
    观察家[O] .unreg();
}
 

my problem is that i need to know when AJAX is done on the page. I need to know this in my contentScriptFile that i load through pageMod. My add on needs to run every time that the page has been modified: so basically when it loads and every time there is an AJAX call.

i tried this:

$(document).ajaxComplete(function() {
     alert("AJAX call completed");
});

but it does not work.

is there a way to do this?

edit: my page mod code from main.js:

pageMod.PageMod({
     include: "*",
     contentScriptFile: [self.data.url("jquery-1.9.1.min.js"), 
     self.data.url("script.js")],

     onAttach: function(worker){

      var apiKey = require("sdk/simple-prefs").prefs.apiKey;
      var ignoreList = require("sdk/simple-prefs").prefs.ignoreList;

      worker.port.emit("prefSet", [ignoreList, apiKey]);

      }

});
解决方案

Put this not in a content script:Im not sure how you will identify the content window of the script. I dont know sdk so well. But see the area CONTENT_WINDOW_OF_CONTENT_SCRIPT

const { Ci, Cu, Cc, Cr } = require('chrome'); //const {interfaces: Ci, utils: Cu, classes: Cc, results: Cr } = Components;
Cu.import('resource://gre/modules/Services.jsm');
Cu.import('resource://gre/modules/devtools/Console.jsm');

var observers = {
    'http-on-modify-request': {
        observe: function (aSubject, aTopic, aData) {
            console.info('http-on-modify-request: aSubject = ' + aSubject + ' | aTopic = ' + aTopic + ' | aData = ' + aData);
            var httpChannel = aSubject.QueryInterface(Ci.nsIHttpChannel);
            var requestUrl = httpChannel.URI.spec
            var goodies = loadContextAndGoodies(aSubject, true);
            if (goodies.contentWindow) {
               if (goodies.contentWindow == CONTENT_WINDOW_OF_CONTENT_SCRIPT) {
                    //do something here
               }
            }
        },
        reg: function () {
            Services.obs.addObserver(observers['http-on-modify-request'], 'http-on-modify-request', false);
        },
        unreg: function () {
            Services.obs.removeObserver(observers['http-on-modify-request'], 'http-on-modify-request');
        }
    }
};

Then in that same scope add in this helper function:

function loadContextAndGoodies(request, return_goodies) {
  var loadContext = null;

  if (request instanceof Ci.nsIRequest) {
      try {
          if (request.loadGroup && request.loadGroup.notificationCallbacks) {
              loadContext = request.loadGroup.notificationCallbacks.getInterface(Ci.nsILoadContext);
          }
      } catch (ex) {
        console.exception('request loadGroup with notificationCallbacks but oculd not get nsIloadContext', ex);
      }
      if (!loadContext) {
        try {
            if (request.notificationCallbacks) {
                loadContext = request.notificationCallbacks.getInterface(Ci.nsILoadContext);
            }
        } catch (ex) {
          console.exception('request has notificationCallbacks but could not get nsILoadContext', ex);
          /* start - noit's backup try, it might be redundant (im not sure) as Wladamir Palant didn't have this way*/
          try {
            var interfaceRequestor = httpChannel.notificationCallbacks.QueryInterface(Ci.nsIInterfaceRequestor);
            loadContext = interfaceRequestor.getInterface(Ci.nsILoadContext);
          } catch (ex) {
            console.exception('backup method failed:' ex);
          }
          /* end - my backup try, it might be redundant as Wladamir Palant didn't have this way*/
        }
      }
  } else {
    console.warn('request argument is not instance of nsIRequest')
  }

  if (return_goodies) {
    if (!loadContext) {
      return null;
    }

    var contentWindow = loadContext.associatedWindow;
    var DOMWindow = contentWindow.top.QueryInterface(Ci.nsIInterfaceRequestor)
                                     .getInterface(Ci.nsIWebNavigation)
                                     .QueryInterface(Ci.nsIDocShellTreeItem)
                                     .rootTreeItem
                                     .QueryInterface(Ci.nsIInterfaceRequestor)
                                     .getInterface(Ci.nsIDOMWindow);
    var gBrowser = DOMWindow.gBrowser;
    if (gBrowser) {
      var tab = gBrowser._getTabForContentWindow(contentWindow.top);
      var browser = tab.linkedBrowser;
    } else {
      var tab, browser = null;
    }
    var goodies = {
      loadContext: loadContext,
      DOMWindow: DOMWindow,
      gBrowser: gBrowser,
      contentWindow: contentWindow,
      browser: browser,
      tab: tab
    };

    return goodies;
  } else {
    return loadContext;
  }
}

To start observing

To start start obseving all requests do this (for example on startup of your addon)

for (var o in observers) {
    observers[o].reg();
}

To stop observing

Its important to stop observring (make sure to run this at least on shutdown of addon, you dont want to leave the observer registered for memory reasons)

for (var o in observers) {
    observers[o].unreg();
}

这篇关于Firefox插件pageMod,抓阿贾克斯contentScriptFile完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 03:37