Search code examples
firefox-addonextension-methodsc#-2.0firefox-addon-sdkjsctypes

Receiving message in C# from Firefox Extension through Native Messaging


I am trying to get active Tab Title as well as URL from Mozilla Firefox. I have installed Firefox extension manually and created a C# windows class Library application for getting the active tab title and url.

First of all I have installed Firefox Extension which contains file like package.json, main.js, install.rdf etc. Below is my code.

main.js

// import js-ctypes 

var {Cu} = require("chrome");
var {ctypes} = Cu.import("resource://gre/modules/ctypes.jsm", null);

var self = require("sdk/self");
var tabs = require("sdk/tabs");

var button = require("sdk/ui/button/action").ActionButton({
id: "style-tab",
label: "Style Tab",
icon: "./icon-16.png",
onClick: function() { 

// Open the library 
    var winffAddonewdl;
    try 
    {
    // Linux 
        winffAddonewdl = ctypes.open("filename.dll");
    }
    catch (e) 
    {
    // Most other Unixes 
        winffAddonewdl = ctypes.open("filename.dll");
    }

    //alert(tabs.activeTab.url);
    // Import a function 

    var OpenStandardStreamIn = winffAddonewdl.declare("OpenStandardStreamIn",  // function name
    ctypes.default_abi, // call ABI
    ctypes.int,         // return type
    ctypes.char.ptr,   // argument type
    ctypes.char.ptr);   // argument type

    var ret = OpenStandardStreamIn(tabs.activeTab.title, tabs.activeTab.url);

    winffAddonewdl.close();

    }
});

I have developed my add-ons using the Add-on SDK with the help of the below link (1) https://developer.mozilla.org/en-US/Add-ons/SDK/Tutorials/Installation (2) https://developer.mozilla.org/en-US/Add-ons/SDK/Tutorials/Getting_started and my add-ons is installed successfully.

I have also created Registry Entry for Installing my Add-ons and my Add-ons is also installed successfully and displaying the current URL as well as Title whenever I clicked on the icon.

Now, I just want to get both Title as well URL from the Firefox browser whenever I switch to a different firefox tab or firefox window. I have developed a c# windows class Library application by which I could able to get this active Tab Title and URL.

Now my question is how to execute this windows class Library application and fetch the Title and URL from the JS file through native messaging. Shall I have to run the windows class Library application? Even I am not able to receive Title and URL at my windows class Library application

What I am supposed to do now. Please help me in this regard.

Edit

I have written this simple code inside main.js file. Below is my code

var tabs = require('sdk/tabs');

tabs.on('activate', function(tab) {
    console.log('active: ' + tab.url);
});

My add-on gets installed successfully but when I switch to different Firefox tab or window nothing is coming at the browser console except "no element found" or "content is null" at tab-events.js.

I simply want to log the active tab url. How to do that?

I will discuss native portion later once I shall accomplice the above issue.


Solution

  • I have found the solution of my above questions. Below is my code for main.js file.

    var {Cu} = require("chrome");
    var {ctypes} = Cu.import("resource://gre/modules/ctypes.jsm", null);
    
    var self = require("sdk/self");
    var tabs = require("sdk/tabs");
    
    tabs.on('activate', callNative);
    
    function callNative(tab)
    { 
    
        var ff;
        try {
    
        ff = ctypes.open("filename.dll");
    
    
        var nativeCaller = ff.declare("OpenStandardStreamIn", // function name
                                ctypes.default_abi, // call ABI
                                ctypes.int32_t,         // return type
                                ctypes.char.ptr,   // argument type
                                ctypes.char.ptr);   // argument type
    
    
        var ret = nativeCaller(tab.title, tab.url);
        ff.close();
        } catch (e) {
    
        }
    
    
    }
    

    and for the native, I have built an empty dll project in visual studio. below is the code.

    #include <stdio.h>
    #include <stdlib.h>
    
    extern "C"
    {
        _declspec(dllexport) int OpenStandardStreamIn(char *title, char *url)
        {
            FILE *fp;
    
            fp = fopen("dumfilename.txt", "a");
    
            fprintf(fp, "%s\n", title);
            fprintf(fp, "%s\n", url);
    
            fclose(fp);
        return 0;
        }
    }
    

    Compile the dll project and dump this dll file at the system folder. Make a registry entry for the firefox-addon. Restart Mozilla Firefox browser to activate the addon.

    After successful installation, open any url at Mozilla Firefox browser or switch to different tab or window and then check the dump file where the title as well as the url is getting.