[英]Unable to pass integer from javascript to npapi plugin
I am writing a simple napapi plugin where I have to print the value passed from javascript function in html page. 我正在编写一个简单的napapi插件,我必须在其中打印从html页面中的javascript函数传递的值。 But I am facing problem while doing it. 但是我在这样做时面临问题。 It works properly on firefox. 它在Firefox上正常工作。 But I want to do it on qt fancybrowser example. 但我想在qt fancybrowser示例中做到这一点。 The value printed is always 0 no matter whatever value I pass in javascript code. 无论我在javascript代码中传递什么值,输出的值始终为0。
The javascript code is as follows : javascript代码如下:
<html>
.....
<script>
function process_data()
{
PluginObject = document.getElementById("Object");
var i =100;
if(PluginObject){
ret = PluginObject.process_Data(i);
}
}
</script>
....
</html>
The plugin code is as follows : 插件代码如下:
.....
bool ScriptableObject::process_Data(const NPVariant* args, uint32_t argCount, NPVariant* result)
{
printf(" process_Data\n");
printf("\t argCount : %d\n",argCount);
int tempi =args[0].value.intValue;
int tempf =args[0].value.doubleValue;
printf("type: %d type: %u\n",args[0].type,args[0].type);
printf("tempi : %d tempf : %f\n",tempi,tempf);
}
......
The output is as follows : 输出如下:
process_Data
argCount : 1
type: 4 type: 4
tempi : 0 tempf : 0.000000
Actually it should print 100 which is the value passed in var i from javascript. 实际上,它应该打印100,这是从javascript中在var i中传递的值。
Any suggestions/comments are welcome 欢迎任何建议/意见
Thanks in advance 提前致谢
When providing a number argument to a NPAPI method, it is undefined whether you will receive a Int32
or Double
variant, so you have to handle both cases in your code. 在为NPAPI方法提供数字参数时,是否会收到Int32
或Double
变体是不确定的,因此您必须在代码中处理这两种情况。
Furthermore, the NPVARIANT_TO_*
macros only extract the respective value - they don't do any conversion. 此外, NPVARIANT_TO_*
宏仅提取相应的值-它们不进行任何转换。
To extract an integer from any numeric argument, you have to write your own code, eg something like: 要从任何数字参数中提取整数,您必须编写自己的代码,例如:
bool convertToInt(const NPVariant& v, int32_t& out) {
if (NPVARIANT_IS_INT32(v)) {
out = NPVARIANT_TO_INT32(v);
return true;
}
if (NPVARIANT_IS_DOUBLE(v)) {
out = NPVARIANT_TO_DOUBLE(v);
return true;
}
// not a numeric variant
return false;
}
From here http://code.google.com/p/chromium/issues/detail?id=68175 and here https://bugs.webkit.org/show_bug.cgi?id=49036 I understand that it is bug in WEB kit, so I add next in "npruntime.h": 从这里http://code.google.com/p/chromium/issues/detail?id=68175和这里https://bugs.webkit.org/show_bug.cgi?id=49036我了解这是WEB中的错误工具包,因此我接下来在“ npruntime.h”中添加:
#define FIX_WEB_KIT_INT32_BUG
#ifdef FIX_WEB_KIT_INT32_BUG
#define NPVARIANT_IS_INT32(_v) ((_v).type == NPVariantType_Int32 || (_v).type == NPVariantType_Double)
#define NPVARIANT_TO_INT32(_v) ((_v).type == NPVariantType_Double ? (_v).value.doubleValue : (_v).value.intValue)
#else
#define NPVARIANT_IS_INT32(_v) ((_v).type == NPVariantType_Int32)
#define NPVARIANT_TO_INT32(_v) (_v).value.intValue)
#endif
on web(from javascript) I use parseInt(myVal,10) for all int values that passed to plugin. 在网络上(来自javascript),我将parseInt(myVal,10)用于传递给插件的所有int值。 Check it on Google Chrome and Safari. 在Google Chrome和Safari上进行检查。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.