0

我正在尝试将数组从插件传递给 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>

欢迎提出任何建议......

4

1 回答 1

0

从你给我们的信息中我能收集到的信息很少,我猜你的 NPNFuncs 函数指针设置不正确(因此你调用了一个错误的函数)或者你的 m_npp 以某种方式损坏了。当然,如果是其中任何一种情况,我认为请求 DOM Window 对象也会死掉。

我可能需要更多的代码来推测。您可能要考虑使用FireBreath或 nixysa,而不是自己做所有事情;它会让你专注于你的实际插件,而不是处理 NPAPI。只是一个想法。

下一个想法,是 NPN_Invoke 调用 g_browser->invoke 吗?因为你提到你正在检查 hasmethod 但是你转身并调用 NPN_HasMethod; 如果这不是全部正确连接,那肯定会导致这样的问题。

归根结底,在某个地方,您的一个指针已关闭;可能性是:

  • NPN_Invoke 和 NPN_HasMethod 没有在 g_browser 上调用正确的方法
  • g_browser(您的 NPNFuncs 结构)没有有效的函数指针
  • 您的 npp 无效(未正确保存等)

我看不出它怎么可能是其他任何东西。

于 2012-08-03T05:14:18.407 回答