Search code examples
javascriptjqueryajaxfirefox-addonfirefox-addon-sdk

FireFox addon: pageMod how to run contentScript everytime AJAX is Done


so i have created an add-on that finds certain strings on every page that opens, it works as it should. The only problem i have is: when a page updates its content with AJAX i don't know how to detect it and rerun my contentScriptFile.

Is there a module i should use or some type of JS listener in my content script file?

here is the pageMod:

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

 });

Solution

  • Any Ajax event completion will be detected by below jQuery code regardless of whether the Ajax event completed successfully or failed:

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

    To make JQuery available to FireFox Add-On, please install this firefox add-on/extension