vendredi 29 mai 2015

Firefox extension that interacts with DOM

I'm developing a firefox extension with a panel of options to interact with de current tab DOM. When you select an option, all elements in the DOM of the active tab are highlited when the mouse is over and when you click in one element I want to fire different actions depending on the option chosen in the panel. I've made it work, but when you choose an option and then choose other the content script "opt" variable has both of the values.

main.js

panel.port.on('select', function(sel){
 attachScript(sel);
 panel.hide();
});

function attachScript(selected)
{
  var worker = tabs.activeTab.attach({
   contentScriptFile: [self.data.url("js/jquery-1.11.3.min.js"), self.data.url("js/content.js")]   
  });

  worker.port.emit('option', selected);
}

content.js

var opt = '';

self.port.on('option', function(option){
 $('body').off('click', '.activelem',controlClick);
 if (option)
 {
  $('body').on('click', '.activelem', controlClick);
  opt = option;      
  window.addEventListener('mousemove', handler);    
 }
});

My questions:

  • Every time than I get the panel option selected and execute the attachScript function, the scripts are injected, or just de first time?
  • Do I need to set a port.once to inject the scripts and then use a normal port.on? If this is the way, how do I communicate with the content script every time?
  • How can I remove the listener in the content script every time than the ActionButton is pressed, so every time the panel is opened its a fresh start.

Aucun commentaire:

Enregistrer un commentaire