-1

I'm trying to code a hello world example of an NPAPI plugin. I implemented all the basics functions needed and i have added a Get_String() function that return a hello world string.

After building, the browser can detect the plugin and all information related to but I can't call my Get_String() function from JavaScript! Here Some Code : plugin.c

#define PLUGIN_NAME        "Name Plugin"
#define PLUGIN_DESCRIPTION " Plugin Description"
#define PLUGIN_VERSION     "1.0"

static NPNetscapeFuncs* sBrowserFuncs = NULL;

NP_EXPORT(NPError)
NP_Initialize(NPNetscapeFuncs* bFuncs, NPPluginFuncs* pFuncs)
{
  sBrowserFuncs = bFuncs;

  if (pFuncs->size < (offsetof(NPPluginFuncs, setvalue) + sizeof(void*)))
    return NPERR_INVALID_FUNCTABLE_ERROR; 

  pFuncs->newp = NPP_New;
  pFuncs->destroy = NPP_Destroy;

  return NPERR_NO_ERROR;
}


NP_EXPORT(char*)
NP_GetPluginVersion()
{
  return PLUGIN_VERSION;
}


NP_EXPORT(const char*)
NP_GetMIMEDescription()
{
  return "application/my-plugin::";
}


NP_EXPORT(NPError)
NP_GetValue(void* future, NPPVariable aVariable, void* aValue) {
  switch (aVariable) {
    case NPPVpluginNameString:
      *((char**)aValue) = PLUGIN_NAME;
      break;
    case NPPVpluginDescriptionString:
      *((char**)aValue) = PLUGIN_DESCRIPTION;
      break;
    default:
      return NPERR_INVALID_PARAM;
      break;
  }
  return NPERR_NO_ERROR;
}


NP_EXPORT(NPError)
NP_Shutdown()
{
  return NPERR_NO_ERROR; 
}


NPError NPP_New(NPMIMEType pluginType, NPP instance, uint16_t mode, int16_t argc, char* argn[], char* argv[], NPSavedData* saved)
{

  return NPERR_NO_ERROR;
}


NPError NPP_Destroy(NPP instance, NPSavedData** save)
{

  return NPERR_NO_ERROR;
}

char* Get_String()
{
    return "hello world from Get function" ;
}

void Set(NPObject object){}

test.html

<doctype html>
<html>
<head>
<script>
  var plugin = document.getElementById("plugin");
  console.log(plugin.Get_String());
</script>
</head>
<embed id="plugin" type="application/typemine-plugin"> 
<body>
</body>
</html>
Georg Fritzsche
  • 97,545
  • 26
  • 194
  • 236
Abdelbari Anouar
  • 246
  • 3
  • 10
  • Have you read a [NPAPI tutorial](http://colonelpanic.net/2009/03/building-a-firefox-plugin-part-one/) or the [documentation](https://developer.mozilla.org/en-US/docs/Gecko_Plugin_API_Reference/Scripting_plugins)? Are you aware of easier options like [Firebreath](http://www.firebreath.org/)? – Georg Fritzsche Apr 04 '13 at 16:03
  • yes I had a look on it and others. But in my case I don't need it. I want to do it with the hard way. – Abdelbari Anouar Apr 04 '13 at 16:10
  • So, have you read the tutorial and/or documentation? It sounds like you are missing all the setup needed for scriptable methods and expect a plain C function to just be accessible from JS. – Georg Fritzsche Apr 04 '13 at 17:35
  • I didn't show you all the code I had developped juste a function that I want to call from javascript as example. All necessary functions for NPAPI are already implemented. – Abdelbari Anouar Apr 04 '13 at 21:16
  • 2
    If all necessary functions for NPAPI were already implemented then it would work. you have not included any of the code that we would need to see to have any idea what is wrong. is it even being instantiated? Have you tried setting a breakpoint or adding log statements to find out? You haven't given us anything to work with here. – taxilian Apr 05 '13 at 04:26
  • these are all functions I had implemented. – Abdelbari Anouar Apr 05 '13 at 08:22

1 Answers1

1

You need to provide the browser with a custom scriptable object from NPP_GetValue(). This is needed for the browser to find out what methods and properties your plugin has, to invoke them, etc.

You can find a basic overview of implementing scripting in part 3 of taxilians tutorial.

Georg Fritzsche
  • 97,545
  • 26
  • 194
  • 236