无法将整数从javascript传递到npapi插件 [英] Unable to pass integer from javascript to npapi plugin

查看:144
本文介绍了无法将整数从javascript传递到npapi插件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个简单的napapi插件,我必须在html页面中打印从javascript函数传递的值。但是我在做这件事时遇到了问题。它在firefox上运行正常。但我想在qt fancybrowser示例中做到这一点。无论我在javascript代码中传递什么值,打印的值始终为0.

I am writing a simple napapi plugin where I have to print the value passed from javascript function in html page. But I am facing problem while doing it. It works properly on firefox. But I want to do it on qt fancybrowser example. The value printed is always 0 no matter whatever value I pass in javascript code.

javascript代码如下:

The javascript code is as follows :

<html>
.....
<script>
function process_data()
{
    PluginObject = document.getElementById("Object");
    var i =100;
    if(PluginObject){
        ret = PluginObject.process_Data(i); 
    }
}
</script>
....
</html>

插件代码如下:

.....
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);
}
......

输出如下:

process_Data
     argCount : 1
type: 4 type: 4
tempi : 0 tempf : 0.000000

实际上它应该打印100,这是从javascript传递给var i的值。

Actually it should print 100 which is the value passed in var i from javascript.

欢迎任何建议/意见

提前致谢

推荐答案

在为NPAPI方法提供数字参数时,未定义是否会收到 Int32 变体,因此您必须在代码中处理这两种情况。

此外, NPVARIANT_TO _ * 宏仅提取相应的值 - 它们不进行任何转换。

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.
Furthermore, the NPVARIANT_TO_* macros only extract the respective value - they don't do any conversion.

要从任何数字参数中提取整数,您必须编写自己的代码,例如类似于:

To extract an integer from any numeric argument, you have to write your own code, e.g. 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;
}

这篇关于无法将整数从javascript传递到npapi插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆