Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connect Firefox extension with C# application

I need to be able to make an event such that every time a user loads a new page and closes firefox, I need it to call a method in my C# application that takes care of maintaining the user model. I know for sure I need to create some type of firefox extension where I use javascript to check such an event. However, I have no idea how I am going to integrate my C# application with the firefox extension. Can someone provide me with some guidance?

like image 747
michelle Avatar asked Oct 25 '22 14:10

michelle


1 Answers

I'll help you out with the parts of the question that I'm familiar with (Javascript based add-ons), and offer some suggestions for the other parts. Here goes nothing!

Add-ons

Firefox add-ons easily provide the tools you need to detect page loads and opening / closing firefox.

To detect page loads you can register a listener to the DOMContentLoaded event in window.

window.addEventListener("DOMContentLoaded", function(event){
    var url = event.originalTarget.location.href;
    alert("Oh yeah, a document is loading: " + url);
}, false);

Alternatively, you can register a nsIWebProgressListener to listen for location changes. This probably closer to what you want, since DOMContentLoaded is also triggered for iframes.

var listener = {
    //unimplemented methods (just give functions which do nothing)
    onLocationChange: function(aWebProgress, aRequest, aLocation){
        var url = aLocation.asciiSpec;
        alert("Oh yeah, a the location changed: " + url);
    }
};
gBrowser.addTabsProgressListener(listener);

To detect firefox open / close you need to first understand how firefox add-ons work with respect to multiple windows. When a new window of firefox is launched, you basically have 2 separate copies of your code running. So, if you care about firefox windows being opened and closed you can simply do:

window.addEventListener("load", function(event){ 
    alert("Looks like you just opened up a new window");
}, false);

window.addEventListener("unload", function(event){
    alert("Awh, you closed a window");
}, false);

But, most likely you want to detect opening / closing firefox as an entire application. This is achieved using a code-sharing mechanism called Javascript Modules. Javascript modules are loaded just once for the lifetime of the application. So, they enable you to share information between windows. Simply counting the number of windows opened and closed should be sufficient for this functionality.

var EXPORTED_SYMBOLS = ["windowOpened", "windowClosed"];
var windowsOpened = 0;
function windowOpened(){
    if( windowsOpened === 0) {
        alert("The first window has been opened!");
    }
    windowsOpened++;
}

function windowClosed(){
    windowsOpened++;
    if( windowsOpened === 0) {
        alert("The last window has been closed!");
    }
}

Then you can simply attach the aforementioned event handlers to call these 2 methods from their corresponding load and unload events.

So, this is all great and everything, but now you have to twiddle with the details of getting a baseline Firefox add-on setup. Fortunately, Mozilla has provided a handy Addon Builder to ease this. All the code about (except the Javascript module) should be placed in the ff-overlay.js file (assuming you use the linked builder).

C# communication

I'm a little less knowledgeable about the interprocess communication with C#. However, maybe I can point you in the right direction and let the smart people at SO fill in the rest.

I believe COM Objects are a method of communication between processes on Windows. So, you could build in a Binary Component to your add-on to perform the communication. However, as far as I understand it, setting up binary components is much more difficult than a standard javascript-based add-on. Either way, Mozilla provides a guide for setting it up in Visual Studio.

If you want to stay away from binary components you are left with the javascript enabled components of the SDK. This includes socket communication, files, pipes, a sqlite database etc. This SO question addresses exactly the question you're asking. If it were me, I would choose them in this order.

  1. Sqlite Database
  2. Named Pipes
  3. Sockets

(1) because there is a lot of code samples available for this, and would be easy to implement on both sides. (2) because this would be the way I'd implement IPC if I were given full control of both sides of the application. (3) is last because I hate that crap (maybe I'm biased from Distributed Systems in college).

tl;dr

The page load stuff should be pretty simple. Check out the Addon Builder to get going with a FF addon, and here to see about detecting page loads.

The C# communication is doable, and addressed in this SO Question. I'd do it with a sqlite database for ease if it were me.

like image 106
skabbes Avatar answered Oct 27 '22 11:10

skabbes