Skip to content Skip to sidebar Skip to footer

Accessing Every Document That A User Currently Views From An Extension

I'm writing an extension that checks every document a user views on certain data structures, does some back-end server calls and displays the results as a dialog.The problem is sta

Solution 1:

The common approach is using a progress listener. If I understand correctly, you want to get a notification whenever a browser tab finished loading. So the important method in your progress listener would be onStateChange (it needs to have all the other methods as well however):

onStateChange: function(aWebProgress, aRequest, aFlag, aStatus)
{
  if ((aFlag & Components.interfaces.nsIWebProgressListener.STATE_STOP) &&
      (aFlag & Components.interfaces.nsIWebProgressListener.STATE_IS_WINDOW) &&
      aWebProgress.DOMWindow == aWebProgress.DOMWindow.top)
  {
    // A window finished loading and it is the top-level frame in its tabFabogore.Start(aWebProgress.DOMWindow);
  }
},

Solution 2:

Ok, I found a way which works from the MDN documentation, and achieves that every document a user opens can be accessed by your extension. Accessing every document a user focuses is too much, I want the code to be executed only once. So I start with initializing the Exentsion, and Listen to DOMcontentloaded Event

window.addEventListener("load", function() { Fabogore.init(); }, false);


varFabogore = {
init: function() {
var appcontent = document.getElementById("appcontent");   // browserif(appcontent)
  appcontent.addEventListener("DOMContentLoaded", Fabogore.onPageLoad, true);
},

This executes the code every Time a page is loaded. Now what's important is, that you execute your code with the new loaded page, and not with the old one. You can acces this one with the variable aEvent:

onPageLoad: function(aEvent)
{
var doc = aEvent.originalTarget;//Document which initiated the event

With the variable "doc" you can check data structures using XPCNativeWrapper etc. Thanks Wladimir for bringing me in the right direction, I suppose if you need a more sophisticated event listening choose his way with the progress listeners.

Post a Comment for "Accessing Every Document That A User Currently Views From An Extension"