我已经编写了 NPAPI 插件来访问当前页面的 DOM。我能够构建插件。现在 我想调用 javascript 函数 console.debug("hello from c++");来自 NPAPI 插件。我采取了以下代码 我正在使用来自谷歌的 helloworld 示例代码来构建 npapi 插件: 代码:
bool ScriptablePluginObject::Invoke(NPObject* obj, NPIdentifier methodName, const NPVariant* args,uint32_t argCount, NPVariant* result)
{
// The message i want to send.
char* message = "Hello from C++";
// Get window object.
NPObject* window = NULL;
NPN_GetValue(npp_, NPNVWindowNPObject, &window);
// Get console object.
NPVariant consoleVar;
NPIdentifier id = NPN_GetStringIdentifier("console");
NPN_GetProperty(npp_, window, id, &consoleVar);
NPObject* console = NPVARIANT_TO_OBJECT(consoleVar);
// Get the debug object.
id = NPN_GetStringIdentifier("debug");
// Invoke the call with the message!
NPVariant type;
STRINGZ_TO_NPVARIANT(message, type);
NPVariant args[] = { type };
NPVariant voidResponse;
NPN_Invoke(npp_, console, id, args,sizeof(args) / sizeof(args[0]),&voidResponse);
// Cleanup all allocated objects, otherwise, reference count and
// memory leaks will happen.
NPN_ReleaseObject(window);
NPN_ReleaseVariantValue(&consoleVar);
NPN_ReleaseVariantValue(&voidResponse);
}
但是当我调用 test.html 加载后它崩溃了。 请让我知道“我是否在正确的位置调用此代码”和“我如何测试此代码”。
谢谢...
最佳答案
首先是第一件事:每当我读到“它崩溃了”但没有更多信息时,我都不得不捂着脸。如果这就是您所知道的,那么您太早停止了故障排除。 FireBreath project有一个page on debugging plugins这可能会有所帮助,但如果您在附加调试器时让它崩溃,您应该找出崩溃的位置以及崩溃的原因。
我还想提醒您,在尝试执行您在生产环境中所做的操作时,我遇到了一些严重的性能问题;不要将其用作您的主要日志记录方法!它不够快。
也就是说,在大多数情况下,我认为您的方法没有任何内在错误。可能是您的(大概是全局的)npp_
无效,或者您的字符串可能需要由 NPN_MemAlloc 分配的内存组成;我知道如果你要返回它那么它就需要,但我认为当你只是调用一个新函数时不需要它;不过,这是值得尝试的。
如果我是你,我的下一步是附加调试器并查看崩溃的位置。最简单的方法可能是在 firefox 中关闭插件 ipc,这样您就可以轻松地附加到该进程,但您也可以找到正确的进程并连接到它(有关详细信息,请参见我之前链接的调试页面)
关于javascript - 从 NPAPI 插件调用 javascript 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18299570/