Vishnu V
Vishnu V

Reputation: 412

Update tab content using firefox addon

I am trying to create an Firefox addon which gives hyper link to phone numbers on a page. Code:

  tabs.on('ready', function(tab) {
  tab.attach({
  contentScript: 'self.postMessage(document.body.innerHTML);',
  onMessage: function (message) {
  html = updatePhonenumber(message);      
  }
 });
});

how to update the content of tab with edited content

Upvotes: 2

Views: 170

Answers (2)

Noitidart
Noitidart

Reputation: 37238

it looks like function(tab) is returning a tab element. try going console.log(tab.linkedBrowser) if you see a non null message in browser console then go tab.linkedBrowser.contentDocument.documentElement.innerHTML = 'rawr' this is just an example how to change the document, you shouldnt use innerHTML, should create element and append it or remove elements.

Upvotes: 1

matagus
matagus

Reputation: 6206

The best way to achieve that is using a PageMod which according to MDN docs "run scripts in the context of web pages whose URL matches a given pattern".

So, you will have two files:

lib/main.js:

var pageMod = require("sdk/page-mod");
var self = require("sdk/self");

pageMod.PageMod({
  include: "*", // <- this tell firefox to attach the following
                //    content script to all web pages
  contentScriptFile: self.data.url("phone-detector.js")
});

data/phone-detector.js:

/* This is a normal js file that gets attached to the current web page
and has access to the DOM*/
var updatePhonenumber = function(dom) {
   // do whatever you should do
}:

updatePhonenumber(document.body.innerHTML); 

Upvotes: 2

Related Questions