-1

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

构建后,浏览器可以检测到插件和所有相关信息,但我无法从 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){}

测试.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>
4

1 回答 1

1

您需要为浏览器提供来自NPP_GetValue(). 浏览器需要它来找出你的插件有哪些方法和属性,调用它们等等。

您可以在出租车司机教程的第 3 部分中找到实现脚本的基本概述。

于 2013-04-05T16:52:01.173 回答