阅读:12800回复:5
求助:Firefox插件开发,如何返回字符串给JavaScript?
我写了一个FireFox插件(按照Mozilla网站上的那个nprt的例子),编译完成之后,加了一个接口testmethod,返回一个字符串,在Invoke函数中进行的处理,就是一句:
if (name == sTestMethod_id) { printf( "=========================1\r\n" ); STRINGZ_TO_NPVARIANT(strdup("foo"), *result); printf( "=========================2\r\n" ); return PR_TRUE; } 外边的html里这样写的: <embed id="embed1" type="application/mozilla-npruntime-scriptable-plugin" style="display: block; width: 50%; height: 100px;"><br> <script> var embed1 = document.getElementById('embed1'); var tt = embed1.testmethod("my test11111119"); alert(tt); </script> 但是不知道为什么总是编译能过,在后边的控制台上,两个printf的输出也都没有问题,就是页面再也出不来了,注释掉STRINGZ_TO_NPVARIANT(strdup("foo"), *result);就没有问题了。搞了很久也没有办法。哪位遇到过这样的问题?请指点一下,在这里多谢了,呵呵。 Firefox版本是3.08 |
|
1楼#
发布于:2009-07-01 08:52
我也遇到这样的问题, 我需要执行JS代码,请问该怎么调用?然后把执行后的结果返回!
|
|
2楼#
发布于:2009-07-01 08:52
同样的问题,顶一下~
|
|
3楼#
发布于:2009-07-01 08:52
char * myStr = (char *)NPN_MemAlloc(32);
strcpy(myStr, "helloWorld test"); STRINGN_TO_NPVARIANT(myStr, 32, *result); 问题已解决,调用STRINGN_TO_NPVARIANT之前需要调用NPN_MemAlloc,用于在浏览器内存空间创建一块内存以保存插件的返回值。注意使用malloc分配时不行的。 |
|
4楼#
发布于:2009-07-01 08:52
本人也碰到过,根据官方的说法是
//When the caller no longer needs the result, it must call NPN_ReleaseVariantValue() to release it //We need to use the use-define func below to fix this issue nor strdup //NPN_ReleaseVariantValue will call NPN_FreeMem() on NPVariants of type NPVARIANTTYPE_STRING //NPN_MemFree deallocates a block of memory that was allocated using NPN_MemAlloc only. //NPN_MemFree does not free memory allocated by any other means. //used NPN_MemAlloc for NPN_ReleaseVariantValue. |
|
5楼#
发布于:2009-07-01 08:52
在plugin.cpp里把
NPN_Evaluate(m_pNPInstance, doc, &str, NULL); 改成 NPVariant result; NPN_Evaluate(m_pNPInstance, doc, &str, &result); |
|