Method for actively calling js functions using the npapi plug-in

Source: Internet
Author: User
Tags add numbers

I have read many articles and introduced the function methods of js callback plug-ins, but there are few examples of the plug-ins actively calling js functions.


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

The code linked above is a good example of a function in the browser callback plug-in, there is no codeproject account, please move here download: http://download.csdn.net/detail/yzsyb/5814095

(Sorry, I forgot to set it to free download during the upload process. Now I need a point. I have to apply for a part to delete the resource. If you are in trouble, just one point! Sorry)


In the original example, let me talk about it a little bit:

In the Codeproject.html file: <inputvalue = "Add Field 1 and 2" onclick = "Add ()" type = "button"> is a button, and the button event is defined as Add (), and Add () is:

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); the plug-in function is called, in plugin. cpp, as shown below:

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

This article describes how to call js from the npapi plug-in. I also learned from this article. Thanks to the original author.

 

The following describes how to call js functions from the plug-in.

The download link of my code is at the end of the article.

PS: the code I added is between // smiler and /// smiler.


First, I created a Button in the plug-in window)

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


Button event:

Case wm_command: {wordncode = hiword (wparam); wordcontrol = loword (wparam); If (ncode = bn_clicked) {cplugin * P = (cplugin *) getwindowlongptr (hwnd, gwlp_userdata); P-> Hello (); // call the hello function of the plug-in} break ;}

Hello () function:

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, npnv1_wnpobject, & npwindow); if (ret! = NPERR_NO_ERROR) {MessageBox (NULL, ("sf"), ("prompt"), MB_ OK); return ;}// Get windowobject. NPVariantwindowVar; NPIdentifierwinID = NPN_GetStringIdentifier ("window"); bool bRet = NPN_GetProperty (npp, npwindow, winID, & windowVar); if (! BRet) {MessageBox (NULL, ("Callback"), ("prompt"), MB_ OK); NPN_ReleaseObject (npwindow); return;} NPObject * window = NPVARIANT_TO_OBJECT (windowVar ); // Invoke "callback_hello" NPVarianttype1, type2; Messages (message1, type1); Messages (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 );}

In the above Code:

Bret = npn_invoke (NPP, window, funcid, argS, 2, & voidresponse); calls the callback_hello function in Js.

 

Test the callback_hello function in the page npruntime_demo.html:

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

Shows the running result:



Click "hello:






My code download link: http://download.csdn.net/detail/yzsyb/5814395

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.