2

I have firefox addon, which injects two content scripts to all pages.

var workers = [];

pageMod.PageMod({
  include: "*",
  contentScriptFile: [
       self.data.url("content/autofill/lib_generic.js"),
       self.data.url("content/autofill/lib.js"),
    ],
  // add worker to the list
  onAttach: function(worker)
      {
        workers.push(worker);

        var filename = getDomainSpecificFilename(worker.contentURL);

        worker.on("detach", function()
        {
          var index = workers.indexOf(worker);
          if (index >= 0)
            workers.splice(index, 1);
        });
      }
});

lib_generic.js contains one function named apply_forms(...) (its description is not important). The function is called from lib.js file. But this procedure doesn't work with several pages, so for each such page a I have a specific script - these file also contain only one function named apply_forms(...).

I have a function, which takes current domain as input and returns name of desired specific script or false if generic should be used.

What I need is - when neccessary - redefina generic apply_forms with specific apply_forms.

I've tried to use

tabs.activeTab.attach({
    contentScriptFile: [ filename ]
});

worker.port.emit("apply_forms_loaded");

and in one of content scripts:

var apply_forms_loaded = false;

self.port.on("apply_forms_loaded", function() {
    console.log("LOADED");
    apply_forms_loaded = true;
});

and the whole procedure is started like this:

var timer;

timer = setInterval(function(){

  if (apply_forms_loaded) {

    clearInterval(timer);
    start(); // apply_forms is called somewhere inside this call
  }
}, 10);

Unfortunately it seems that tabs.activeTab.attach injects content scripts in different context so generic function is called allways.

Is there anything I can do to convince activeTab to add content scripts in same context or should I do it different way? (which one then)

Or could problem be in - I don't know - that content script is not fully injected when I send apply_forms_loaded message?

I've been trying to redefine function definition also for Chrome and I've made it work (url to SO question)

Thanks for advice.

Community
  • 1
  • 1
dakov
  • 1,039
  • 2
  • 12
  • 33

1 Answers1

1

That's correct, scripts added via pagemod are in a different JS sandbox from those added by tabs.attach. What you could do is inject the functions into the actual window using the new exportFunction() function - this way the function is in the real window and will always get overwritten. You can then call it from unsafeWindow..

therealjeffg
  • 5,790
  • 1
  • 23
  • 24