Search code examples
javascriptfirefoxfirefox-addonnpapibrowser-plugin

Communication between extension and plugin


I have written a Firefox extension which queries some browser information. I need to send this information to an NPAPI plugin from my extension.

How can I achieve this? Is there any way to communicate between NPAPI plugins and the extension's JavaScript code? I am grateful for any useful links or code snippets.


Solution

  • If the plugin provides a scripting interface, the extension can just call its functions when wants to send the information.

    You need to have access to the plugin element, e.g.:

    • var plug = someDocument.getElementById('yourPluginId');
    • or by finding the embed/object elements you want without a specific ID

    To find the elements without having a specific id, you could e.g. collect them using XPath or by using getElementsByTagName().
    Keep in mind that the mime type might not necessarily identify your plugin if you are not using one specific to your plugin. You might need to check custom plugin name/version information in addition to the type attribute.

    Once you have the plugin element you can simply call functions on it from JavaScript (e.g. plug.someFunction(someData);).