【问题标题】:Calling a Javascript API from an NPAPI DLL从 NPAPI DLL 调用 Javascript API
【发布时间】:2012-03-26 15:25:49
【问题描述】:

我正在为使用 NPAPI DLL 的 Google Chrome 编写扩展程序。在 NPAPI DLL 的调用方法中,我插入了以下代码以将消息打印到 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);

没有任何东西被打印到控制台,断言也没有失败。我不确定我的 console.log 语句是否有问题,因为即使我将它们与其他 javascript 文件一起使用,它们也不会打印任何内容。我想暂时使用alert("Hello, world!") 之类的语句。我可以修改我的代码以调用x.y() 形式的函数,但我不明白我应该如何去显示一个警告框。我使用了以下link 的教程。我应该怎么做才能显示从 NPAPI DLL 调用的警告框?

编辑:我可以使用window.alert("") 表单 (X.Y() form) 调用警报,但这仍然不能解决我的问题。我仍然不明白我应该如何直接从 NPAPI 调用 X() 类型的函数。

【问题讨论】:

    标签: javascript c++ google-chrome-extension npapi google-chrome-devtools


    【解决方案1】:

    找到了!我找不到调用X() 类型函数的特定方法。相反,我可以将其称为window.X(),方法是将函数X() 的定义放在扩展程序的background.html 页面中。因此,如果我创建了一个函数 function myAlert(message){alert(message);} 并将其放在 background.html 中,我可以从 NPAPI 调用 window.myAlert(argument),然后上面的函数就会被调用。

    【讨论】:

      猜你喜欢
      • 2013-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-05
      • 2015-10-07
      相关资源
      最近更新 更多