Search code examples
javascriptnpapibrowser-plugin

How can I pass binary-stream (read from a .jpg file, about 1M ) from firefox plugin to page-hosted javascript?


I'm work on a project recently, which need to pass a binary-stream from npapi plugin to javascript, I've tried following ways:

  1. use NPN_InvokeDefault, i created a string variant which store the binary-stream, and invoke it to javascript, it failed. (i've tried to pass binary-stream read from XXX.txt file, it works!)

  2. i tried to use NPN_NewStream, the example listed in http://www.terraluna.org/dgp/cvsweb/PluginSDK/Documentation/pi3.htm#npnnewstream workes, but the pic is loaded in a new browser tab, i don't know how to recieve it in javascript.

Is there any one have ever met similar problem before? or maybe npapi can't support such kind of data transfering?

looking forward to your suggestiongs, thanks a lot.


Solution

  • Unfortunately, NPAPI was never designed with this purpose in mind. There are a couple of ways you can do it, and none of them are really ideal:

    • You can create a javascript array and pass the data in small 1-4 byte chunks (this is really very inefficient)
    • You could create a webserver embedded in the plugin and request the data from there (I have done this and it can work quite well, but keep in mind that if you use this from an SSL website you'll get security warnings when the embedded webserver isn't SSL)
    • You can base64 encode the binary data and send it as a string.

    Those are the ways I have seen it done. The reason you can't send the actual binary data directly as a string is that NPAPI requires string data to be UTF8, but if you base64 encode it then it works fine.

    Sorry I can't give you a "happier" solution :-/