0

I'm try to get the dom elements by tagname using npapi whith chrome, but the length of the result is always zero.

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

the npvlength.value.intValue is always 0, but when i try to get the element it's ok. I can get the element and it's property.

        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;
               }
        }
tosneytao
  • 1
  • 1

1 Answers1

1

Are you sure it's actually NPVariantType_Int32 and not NPVariantType_Double?

Especially cross-browser you shouldn't rely on it being one or the other (it is undefined which of them is actually used for number values). Instead use a helper for conversions, e.g.:

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;
}
Georg Fritzsche
  • 97,545
  • 26
  • 194
  • 236