我正在尝试将数组从插件传递给 Javascript。但我无法做到。我参考了其他类似的帖子,其中给出的解决方案是首先获取 Dom 窗口对象,然后使用 NPN_Invoke 和 NPN_GetStringIdentifier("array") 调用该函数,然后推送数组元素。但是当我尝试它时,它在 NPN_Invoke 上崩溃了。我无法找出原因,是因为 Dom 的 NPObject 没有任何与“数组”相关的方法还是其他原因?
这是我的插件代码...(我的数组数据是 0,1,2,...9 循环中 i 的值)
bool ScriptableObject::method_process_getarray_intval(const NPVariant* args, uint32_t argCount, NPVariant* result)
{
printf("\n DATATYPE method_process_getarray_intval");
NPObject DomWin;
NPVariant Array;
NPError l_RetErr;
bool l_RetBool;
NPIdentifier l_NPId;
//Get the Window Object
l_RetErr = NPN_GetValue(m_npp,NPNVWindowNPObject,&DomWin);
if(l_RetErr == NPERR_NO_ERROR)
printf("\n \t Got the Dom Window Object");
else
printf("\n \t Error occured while getting the Dom Window Object");
//Get the Array by invoking DomWin using ARRAY method of browser
//Get string Identifier for Array
l_NPId = NPN_GetStringIdentifier("Array");
l_RetBool = NPN_Invoke(m_npp,&DomWin,l_NPId,NULL,0,&Array);
if(l_RetBool)
printf("\n \t Invoked for Array");
else
printf("\n \t Error while Invoking for Array");
//Fill the array elements using PUSH method of Array NPVariant
l_NPId = NPN_GetStringIdentifier("push");
for(int i = 0;i < 10; i++)
{
NPVariant *arg =(NPVariant *) g_browser->memalloc(sizeof(NPVariant));
INT32_TO_NPVARIANT(i,*arg);
NPVariant result;
l_RetBool= NPN_Invoke(m_npp,Array.value.objectValue,l_NPId,arg,1,&result);
if(l_RetBool)
printf("\n \t Invoked for Array i : %d",i);
else
printf("\n \t Error while Invoking for Array i :%d",i);
}
return true;
}
我的html页面看起来像这样。我不知道这是否正确。请让我知道是否有任何更正...
<html>
<script type="text/javascript">
var inarray ;
function handleEvent(e) {
...
if (e.keyCode == 55)
{
document.getElementById('arrayint_ele_get').innerHTML = inarray;
process_getintarray(inarray);
}
}
</script>
<script type="text/javascript">
function process_getintarray(inarray)
{
if(obj)
{
obj.process_getarray_intval(inarray);
}
}
</script>
<body onload="init()" onkeydown="handleEvent(event)">
<div id="sq" style="width:50px;height:50px;position:relative;left:0px;border:1px solid #333333;background-color:#FF0000"></div>
......
<div style="position:absolute;left:100px;top:10px"> PLUGIN-SCRIPTS </div>
<div id ="arrayint_ele_get" style="position:absolute;left:100px;top:300px"> ARRAY_GETINT VAL </div>
.....
</body>
</html>
欢迎提出任何建议......