Bez
Bez

Reputation: 69

Firefox addon: how to grab data from webpage?

Purpose

I am trying to make a common answer database for a website form. The form is a description of computer hardware specifications, and I am trying to make it so that based on a model field, other fields are filled in automatically. The extension with have two buttons, "populate" which will based on the model field, check the database for a matching entry, and then populate data into the forms based on that. The second button being "save", which will take data in the fields and marry it to the model field and place it into the database.

Question

So, my main question is on interacting with the webpage itself, how do I grab data from the webpage, and then how do I make changes to fields?

Upvotes: 3

Views: 536

Answers (1)

erikvold
erikvold

Reputation: 16558

So, my main question is on interacting with the webpage itself, how do I grab data from the webpage, and then how do I make changes to fields?

You can do this with a Firefox Add-on SDK Page-mod, click here for the documentation

Here is an example for getting data:

Example Page-Mod

/lib/main.js:

var tag = "p";
var data = require("sdk/self").data;
var pageMod = require("sdk/page-mod");

pageMod.PageMod({
  include: "*.mozilla.org",
  contentScriptFile: data.url("element-getter.js"),
  onAttach: function(worker) {
    worker.port.emit("getElements", tag);
    worker.port.on("gotElement", function(elementContent) {
      console.log(elementContent);
    });
  }
});

/data/element-getter.js:

self.port.on("getElements", function(tag) {
  var elements = document.getElementsByTagName(tag);
  for (var i = 0; i < elements.length; i++) {
    self.port.emit("gotElement", elements[i].innerHTML);
  }
});

Upvotes: 5

Related Questions