问题
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.
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>
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.
Any suggestions/comments are welcome
Thanks in advance
回答1:
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;
}
回答2:
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":
#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. Check it on Google Chrome and Safari.
来源:https://stackoverflow.com/questions/12172380/unable-to-pass-integer-from-javascript-to-npapi-plugin