我尝试使用 npapi 和 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;
}
}
最佳答案
您确定它实际上是 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;
}
关于c++ - NPN_Invoke getElementsByTagName 无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17848000/