我正在为使用NPAPI DLL的Google Chrome编写扩展程序。在NPAPI DLL的invoke方法中,我插入了以下代码来将消息打印到javascript控制台:
char* message = "Hello from C++";
// Get window object.
NPObject* window = NULL;
npnfuncs->getvalue(thisObj->npp, NPNVWindowNPObject, &window);
// Get console object.
NPVariant consoleVar;
NPIdentifier id = npnfuncs->getstringidentifier("console");
npnfuncs->getproperty(thisObj->npp, window, id, &consoleVar);
NPObject* console = NPVARIANT_TO_OBJECT(consoleVar);
// Get the debug object.
id = npnfuncs->getstringidentifier("log");
//console.
// Invoke the call with the message!
NPVariant type;
STRINGZ_TO_NPVARIANT(message, type);
NPVariant args[] = { type };
NPVariant voidResponse;
bool didRun = npnfuncs->invoke(thisObj->npp, console, id, args, sizeof(args) / sizeof(args[0]), &voidResponse);
if (!didRun) assert(false);
// Cleanup all allocated objects, otherwise, reference count and
// memory leaks will happen.
npnfuncs->releaseobject(window);
npnfuncs->releasevariantvalue(&consoleVar);
npnfuncs->releasevariantvalue(&voidResponse);
// Get window object.
NPObject* window = NULL;
npnfuncs->getvalue(thisObj->npp, NPNVWindowNPObject, &window);
// Get console object.
NPVariant consoleVar;
NPIdentifier id = npnfuncs->getstringidentifier("console");
npnfuncs->getproperty(thisObj->npp, window, id, &consoleVar);
NPObject* console = NPVARIANT_TO_OBJECT(consoleVar);
// Get the debug object.
id = npnfuncs->getstringidentifier("log");
//console.
// Invoke the call with the message!
NPVariant type;
STRINGZ_TO_NPVARIANT(message, type);
NPVariant args[] = { type };
NPVariant voidResponse;
bool didRun = npnfuncs->invoke(thisObj->npp, console, id, args, sizeof(args) / sizeof(args[0]), &voidResponse);
if (!didRun) assert(false);
// Cleanup all allocated objects, otherwise, reference count and
// memory leaks will happen.
npnfuncs->releaseobject(window);
npnfuncs->releasevariantvalue(&consoleVar);
npnfuncs->releasevariantvalue(&voidResponse);
没有任何东西被打印到控制台,断言也没有失败。我不确定我的console.log语句是否有问题,因为即使我将它们与其他javascript文件一起使用它们也不会打印任何内容。我想暂时使用像这样的语句。我可以修改我的代码以调用
alert("Hello, world!")
形式的函数,但我不明白我应该如何显示警告框。我使用了以下link中的教程。如何显示从NPAPI DLL调用的警告框?
编辑:我可以使用x.y()
表单(window.alert("")
)来调用警报,但这仍然无法解决我的问题。我仍然不明白我应该如何直接从NPAPI调用X.Y() form
类型的函数。
答案 0 :(得分:0)
X()
的函数的特定方法。相反,我可以将其称为window.X()
,方法是将函数X()
的定义放在扩展程序的background.html页面中。所以,如果我创建了一个函数function myAlert(message){alert(message);}
并将其放在background.html中,我可以从NPAPI调用window.myAlert(argument)
,然后调用上面的函数。