Search code examples
javascriptgoogle-chromenotificationsnpapi

How to send notification from NPAPI pluguin to javascript code?


From my point of view it should be some code on javascript side, setting up callback function in NPAPI plugin. On NPAPI side it should be some way to call this callback function, thus sending notification from NPAPI to javascript.

I am trying to do it for Chrome browser but it should work for any browser supporting NPAPI. Short NPAPI and javascript code example would be very helpful.


Solution

  • I don't have a working code example for you, but it wasn't too difficult to implement. The way FireBreath does it is basically to implement addEventListener and removeEventListener yourself; the callback function will come as an NPObject (inside the NPVariant) and you can trigger that callback simply by calling NPN_InvokeDefault on the NPObject with the parameters you want.

    This won't actually act like a real javascript event but it will be close enough for most purposes.

    You can also of course just have any method that accepts an NPObject as one of the params and use it that way to do the callback.

    You can find example code in the FireBreath codebase but it is abstracted a bit for simplicity and to be DRY.