您的位置:首页 > Web前端 > JavaScript

npapi插件主动调用js函数的方法

2013-07-25 16:04 453 查看
看了很多文章介绍了js回调插件的函数方法,但是插件主动调用js函数的例子比较少

http://www.codeproject.com/Articles/92787/Working-on-an-NPAPI-browser-plugin

上面链接的代码是浏览器回调插件里函数的一个很好的例子,没有codeproject账号的请移步这里下载:http://download.csdn.net/detail/yzsyb/5814095

(不好意思,上传的时候忘记设置成免费下载了,现在需要一个积分,要删除资源还要到某板块申请,麻烦,那就一个积分吧!抱歉)

原始例子中我稍微说一下:

Codeproject.html 文件中:<inputvalue="Add Field 1 and 2" onclick="Add()"type="button">是一个按钮,按钮事件定义为Add(),而Add()为:

function Add()
{
try
{
if(CheckBrowser() ) return;
vara1 = document.getElementById("f1").value;
vara2 = document.getElementById("f2").value;
varres = PLUGIN.Add(a1,a2);//we can also add numbers and strings (because of theplugin implementation)
alert("Plugin Added result is: " + res );
}
catch(err) { alert(err); }
}


其中var res = PLUGIN.Add(a1,a2); 调了插件的函数,在plugin.cpp中,如下所示:

ScriptablePluginObject::Invoke(NPIdentifiername, const NPVariant *args, uint32_t argCount, NPVariant *result)
{
//kk
char*pFunc = NPN_UTF8FromIdentifier(name);

if(!strcmp( "Add", pFunc ) )
{
intsum = 0;

for(unsigned int i = 0; i < argCount; i++ )
{
if(args[i].type == NPVariantType_Int32 )
{
sum+= args[i].value.intValue;
}
elseif( args[i].type == NPVariantType_String )
{
CNPStrings(args[i].value.stringValue);
sum+= atoi( s );
}
elsereturn false;//an error happenend

}
//valuefor GUI output
sprintf(m_szTextGui, "Sum = %ld", sum );
//triggering
::InvalidateRect(m_hWnd, 0, true );
//niceand handy little helpers, there are more of it
INT32_TO_NPVARIANT(sum,*result);
returntrue;
}
…………
}


http://www.m0interactive.com/archives/2010/10/21/how_to_communicate_back_to_javascript_from_npapi_plugin.html

这篇文章很好的介绍了怎么从npapi插件调js,我也是从这篇文章学习的,感谢原作者。

 

下面重点介绍:如何从插件里调用js的函数

我的代码下载链接在文章最后

PS:代码中//smiler和////smiler之间是我添加的代码

首先我在插件窗口中创建了一个按钮(Button)

case WM_CREATE:
{
CreateWindow(TEXT("BUTTON"),TEXT("Hello"),
WS_VISIBLE|WS_CHILD|BS_FLAT,
10,120,
40,40, hWnd,
NULL,0,0);
break;

}


按钮的事件为:

case WM_COMMAND:
{
WORDNCode = HIWORD(wParam);
WORDControl = LOWORD(wParam);
if(NCode == BN_CLICKED)
{
CPlugin* p = (CPlugin*)GetWindowLongPtr(hWnd,GWLP_USERDATA);
p->hello();//调用了插件的hello函数

}
break;
}

hello()函数为:

void CPlugin::hello()
{
char* message1 = "This is a hello";
char* message2 = "from NPAPI plugin";

NPP npp = (NPP)m_pNPInstance;
NPObject*npwindow = NULL;
NPError ret = NPN_GetValue(npp, NPNVWindowNPObject,&npwindow);
if(ret != NPERR_NO_ERROR)
{
MessageBox(NULL, ("sf"),("提示"), MB_OK);
return;
}

// Get windowobject.
NPVariantwindowVar;
NPIdentifierwinID = NPN_GetStringIdentifier("window");
bool bRet = NPN_GetProperty(npp, npwindow, winID, &windowVar);
if(!bRet)
{
MessageBox(NULL, ("NPN_GetPropertyfailed"), ("prompt"), MB_OK);
NPN_ReleaseObject(npwindow);
return;
}

NPObject*window = NPVARIANT_TO_OBJECT(windowVar);
// Invoke “callback_hello”
NPVarianttype1, type2;
STRINGZ_TO_NPVARIANT(message1, type1);
STRINGZ_TO_NPVARIANT(message2, type2);
NPVariantargs[] = { type1,type2 };

NPVariantvoidResponse;
NPIdentifierfuncID = NPN_GetStringIdentifier("callback_hello");
bRet = NPN_Invoke(npp,window, funcID,args, 2, &voidResponse);
if(!bRet)
{
MessageBox(NULL, ("invokefailed"), ("prompt"), MB_OK);
return;
}

// clean up
NPN_ReleaseObject(npwindow);
NPN_ReleaseVariantValue(&windowVar);
NPN_ReleaseVariantValue(&voidResponse);
}

上述代码中:
bRet =NPN_Invoke(npp,window, funcID,args,2, &voidResponse);调用了js中的callback_hello函数

 

测试页面npruntime_Demo.html中callback_hello函数:

function callback_hello(hellotext1,hellotext2)
{
alert(hellotext1);
alert(hellotext2);
}


运行结果如下图所示:



点击Hello按钮:





我的代码下载链接:http://download.csdn.net/detail/yzsyb/5814395
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  npapi 插件 回调