c - Javascript 调用的 NPAPI 函数

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

我正在尝试编写 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/

相关文章:

api - 如何从 Chrome 扩展程序中获取用户的位置?

javascript - 在 Google Chrome 中通过 Javascript 启动系统命令

activex - 将 ActiveX 转换为 NPAPI

c++ - Firebreath NPAPI 插件如何登录

python - 如何在 c 中读取传感器,然后在 python 中使用该输入

google-chrome - 用户使用 chrome.identity.launchWebAuthFlow 登录后,如何在过期前获得新的访问 token ?

命令行中缺少对 XMapSubwindows 和 DSO 的编译/usr/bin/ld undefined reference

javascript - 如何使用 NPAPI 创建一个新的 JS 对象?

c - 如何使用 GMP 获取 C 中定义的实型数字的字节计数部分

C 获取代码内的编译选项