0

I am trying to write my first Firefox add-on using WebExtensions (it is quite complex for a first extension). I need to process the results of all Google searches within a session (session = queries using the same Google page). I have the following code:

manifest.json:

{
  "manifest_version": 2,
  "name": "MyAddon",
  "version": "0.0.1",

  "description": "My Add-on",
  "icons": {
      "48": "icons/icon-48.png"
  },

  "applications": {
      "gecko": {
          "id": "myaddon@local",
          "strict_min_version": "42.0"
      }
  },

  "content_scripts": [
    {
      "matches": ["*://www.google.com/*"],
      "js": ["index.js"]
    }
  ]
}

index.js:

var pageUrl = window.location.href;

var req = new XMLHttpRequest();
req.open("GET", pageUrl, true);
req.onreadystatechange = function() {
    if(req.readyState == 4 && req.status == 200) {
        handleResponse(req.responseText);
    };
};
req.send();

function handleResponse(pageContent) {
    // Do some processing ...
}

It does exactly what I need for the first search (using the address bar). However, the add-on loads the first time I enter a Google page, does what it has to, then stops. It only reloads when I refresh the page or make another query from the address bar.

I need to find a way to reload the add-on when I make another search using the search field and do the processing again, but without losing the work from the previous searches. I suppose I'll have to use cookies, but I'm not there yet.

I tried to use req.addEventListener("load", someFunction) instead of/besides req.onreadystatechange(), but it didn't solve the problem. Any ideas?

Thank you!

guimarac
  • 65
  • 6

1 Answers1

0

As for you first question, regarding the preservation of data, you could always use storage to store that data, or communicate with the background page and store data there ( unless you need it across browser restarts, in which case you should resort to using storage ).

The problem you have with "loading" of the page is that it isn't loading at all. The page itself is loaded into the browser and it will stay so (the ready-state you mentioned will not change ) until you navigate away . What really changes is just the content on the page. You should create an onClick listener for Google search button ( or a listener to for Enter pressed in search field or even change event of the search field ). Additionally, you may want to consider using jQuery to aid you.

Transcendental
  • 983
  • 2
  • 11
  • 27