简体   繁体   English

从NPAPI DLL调用Javascript API

[英]Calling a Javascript API from an NPAPI DLL

I'm writing an extension for Google Chrome that uses an NPAPI DLL. 我正在编写使用NPAPI DLL的Google Chrome扩展程序。 In the invoke method of the NPAPI DLL, I have inserted the following code to print a message to the javascript console: 在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); 

Nothing is getting printed to the console, and neither is the assert failing. 什么都不会打印到控制台,断言也不会失败。 I'm not sure if there's a problem with my console.log statements as they don't print anything even when I use them with other javascript files. 我不确定console.log语句是否存在问题,因为即使我将它们与其他javascript文件一起使用也不会打印任何内容。 I want to use a statement like alert("Hello, world!") instead for the moment. 我暂时想使用诸如alert("Hello, world!")类的语句。 I could modify my code to call functions of the form xy() , but I don't understand how should I go about displaying an alert box. 我可以修改代码以调用形式为xy()函数,但我不知道如何显示警报框。 I used the tutorial at the following link . 我在以下链接中使用了本教程。 What should I do to display an alert box, called from the NPAPI DLL? 如何显示从NPAPI DLL调用的警报框?

Edit: I am able to call the alert using it's window.alert("") form ( XY() form ), but this doesn't still solve my problem. 编辑:我可以使用它的window.alert("")形式( XY() form )调用警报,但这仍然不能解决我的问题。 I still don't understand how should I call a function of type X() directly from the NPAPI. 我仍然不明白如何直接从NPAPI调用X()类型的函数。

Found it! 找到了! I couldn't find a specific method to call a function of type X() . 我找不到调用X()类型的函数的特定方法。 Instead, I was able to call it as window.X() , by placing the definition of function X() in the background.html page of the extension. 相反,通过将函数X()的定义放在扩展的background.html页面中,我可以将其称为window.X() So, if I created a function function myAlert(message){alert(message);} and placed it in background.html, I could make a call to window.myAlert(argument) from the NPAPI, and the above function would get called. 因此,如果我创建了一个函数function myAlert(message){alert(message);}并将其放置在background.html中,则可以从NPAPI调用window.myAlert(argument) ,上述函数将被调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM