NPAPI函数由Javascript调用

时间:2013-04-04 15:15:05

标签: c google-chrome-extension npapi browser-plugin

我正在尝试编写一个NPAPI插件的hello world示例。我实现了所需的所有基本功能,并添加了一个返回hello world字符串的Get_String()函数。

构建完成后,浏览器可以检测到插件和所有相关信息但我无法从JavaScript调用我的Get_String()函数! 这里有些代码: 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>

1 个答案:

答案 0 :(得分:1)

您需要提供NPP_GetValue()的浏览器with a custom scriptable object。这需要浏览器找出插件有哪些方法和属性,调用它们等等。

您可以在part 3 of taxilians tutorial中找到实施脚本的基本概述。