我正在尝试编写 NPAPI 插件的 hello world 示例。我实现了所需的所有基本函数,并且添加了一个返回 hello world 字符串的 Get_String() 函数。
构建后,浏览器可以检测到插件和所有相关信息,但我无法从 JavaScript 调用我的 Get_String() 函数! 这里有一些代码: 插件.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>
最佳答案
您需要提供浏览器with a custom scriptable object来自NPP_GetValue()
。浏览器需要它来找出你的插件有哪些方法和属性,调用它们等等。
您可以在part 3 of taxilians tutorial中找到实现脚本的基本概述。 .
关于c - Javascript 调用的 NPAPI 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15815165/