NPN_Invoke getElementsByTagName不起作用

时间:2013-07-25 02:27:22

标签: c++ npapi browser-plugin

我尝试使用npapi whith chrome通过标记名获取dom元素,但结果的长度始终为零。

    NPVariant tagName;  
    STRINGZ_TO_NPVARIANT("input", tagName);  
    NPVariant inputCollection;
    if(!NPN_Invoke(m_pNPInstance, doc,NPN_GetStringIdentifier("getElementsByTagName"), &tagName, 1, &inputCollection))
    {
        outLog<<"get input error"<<endl;
    }
    NPVariant npvlength;
    if (NPN_GetProperty(m_pNPInstance, NPVARIANT_TO_OBJECT(inputCollection), NPN_GetStringIdentifier("length"), &npvlength))
    {
        outLog<<npvlength.type<<"," <<npvlength.value.intValue<<endl;
    }

npvlength.value.intValue始终为0,但是当我尝试获取元素时,它没问题。我可以得到元素和它的属性。

        NPVariant index;
        INT32_TO_NPVARIANT(0, index);

        NPVariant Item;
        if (NPN_Invoke(m_pNPInstance, NPVARIANT_TO_OBJECT(inputCollection), NPN_GetStringIdentifier("item"), &index, 1, &Item))
        {
            NPVariant typeVal;
                if (NPN_GetProperty(m_pNPInstance, NPVARIANT_TO_OBJECT(Item), NPN_GetStringIdentifier("type"), &typeVal))
               {
                   outLog<<NPVARIANT_TO_STRING(typeVal).UTF8Characters<<endl;
               }
        }

1 个答案:

答案 0 :(得分:1)

您确定它实际上是NPVariantType_Int32而不是NPVariantType_Double吗?

特别是跨浏览器,你不应该依赖它是一个或另一个(它是未定义的,它们实际上用于number值)。而是使用助手进行转换,例如:

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