问问题
364 次
1

我正在用 NPAPI 编写一个 safari 插件。如何将整数从 NPAPI 插件(不使用 FireBreath)返回到 JavaScript?javascript:

<html>
<head>
<script>
function run() {
    var plugin = document.getElementById("pluginId");
    var number = plugin.getBrowserName();
    alert(number);
}
</script>
</head>
<body >
<embed width="0" height="0" type="test/x-open-with-default-plugin" id="pluginId">
<button onclick="run()">run</button>
</body>
</html>

插件代码:

bool plugin_invoke(NPObject *obj, NPIdentifier methodName, const NPVariant *args, uint32_t argCount, NPVariant *result) {
// Make sure the method called is "open".
NPUTF8 *name = browser->utf8fromidentifier(methodName);
if(strcmp(name, plugin_method_name_getBrowserName) == 0) {
  //what can i do here?
}
return true;

}

如何从 plugin.getBrowserName() 返回一个数字?

请帮忙!

我找到了这个线程:Return an integer/String from NPAPI plugin to JavaScript(Not using FireBreath),但我不知道这些代码在哪里

char* npOutString = (char *)pNetscapefn->memalloc(strlen(StringVariable) + 1);
if (!npOutString) return false; strcpy(npOutString, StringVariable); 
STRINGZ_TO_NPVARIANT(npOutString, *result);

放。

4

1 回答 1

2

你看过http://npapi.com/tutorial3吗?

返回值出现在 NPVariant* 结果中。去看看NPVariant的文档,你会看到有一个类型,然后是一个用于不同类型数据的联合。您正在谈论的字符串代码将代替您的“//我可以在这里做什么?” 评论。要返回一个整数,你可以这样做:

bool plugin_invoke(NPObject *obj, NPIdentifier methodName, const NPVariant *args, uint32_t argCount, NPVariant *result) {
// Make sure the method called is "open".
NPUTF8 *name = browser->utf8fromidentifier(methodName);
if(strcmp(name, plugin_method_name_getBrowserName) == 0) {
  result->type = NPVariantType_Int32;
  result->intValue = 42;
}
return true;

您还可以使用 *_TO_NPVARIANT 宏(在上面的NPVariant 文档链接中记录),如下所示:

bool plugin_invoke(NPObject *obj, NPIdentifier methodName, const NPVariant *args, uint32_t argCount, NPVariant *result) {
// Make sure the method called is "open".
NPUTF8 *name = browser->utf8fromidentifier(methodName);
if(strcmp(name, plugin_method_name_getBrowserName) == 0) {
  INT32_TO_NPVARIANT(42, *result);
}
return true;

如果您查看INT32_TO_NPVARIANT 宏的源代码,您会发现它的作用与我在上面所做的相同,因此两者是等价的。

于 2012-12-06T17:16:30.103 回答