Npapi scripted Interface

Source: Internet
Author: User
Tags hasproperty

The implementation of the Scriptable interface. functions related to attributes include hasproperty, getproperty, and setproperty. Set attributes in JS (take bar as an example) using plugin. bar = barvalue;. You can directly use plugin to obtain the property. bar; if you want to create a property for the plug-in, you must return true in hasproperty. In getproperty, you can call npn_getproperty (mnpp, swindowobj, name, result, if you have a special purpose, you can write its own implementation code. Set the attributes to npn_setproperty (mnpp, swindowobj, name, value). The methods-related functions are hasmethod and invoke, if you want to create a method for the plug-in, you must return true for the function in hasmethod. The method implementation code should be implemented in invoke or called by invoke. If both method and property are used, you can use Plugin in Js. foo = value; and plugin. foo can be used to set or obtain attribute values. You can also use plugin. foo (); calls the corresponding function, but uses plugin. when Foo () is used, the getproperty method is called before the Foo () method is called.

The simplest example is:

Bool scriptablepluginobject: hasmethod (npidentifier name) {return true ;}
Bool scriptablepluginobject: hasproperty (npidentifier name) {return true ;}
Bool scriptablepluginobject: getproperty (npidentifier name, npvariant * result)
{Return npn_getproperty (mnpp, swindowobj, name, result );}
Bool scriptablepluginobject: setproperty (npidentifier name, const npvariant * value)
{Return npn_setproperty (mnpp, swindowobj, name, value );}
Bool scriptablepluginobject: invoke (npidentifier name, const npvariant * ARGs, uint32_t argcount, npvariant * result)
{MessageBox (null, "functions called", "MSG", 0); Return true ;}

In this way, the above Code will be normally called if any string is used in JS to replace the previous Foo or bar. One thing to note is that any object in JS can set attributes such:

VaR T = new object ();
T. anystr = val;
Alert (T. anystr );

This method is different from the previous one because it is only a JS kernel operation and its set attributes cannot be accessed in the plug-in code, the purpose of the attribute setting mechanism implemented by the plug-in is to facilitate access to the attributes set through Js!
You need to combine npn_getstringidentifier to set several specific attributes or methods. For details, see the example program.
In the Scriptable plug-in, it is necessary to share data between the plugin class and the scriptableobject class. Using global variables is certainly a method, however, this will cause serious consequences when there are multiple plug-in instances on a page. Therefore, this is not recommended. Note that the mnpp member of scriptableobject is returned by getscriptobject, and this mnpp is the NPP structure in the plugin class, the pdata of this NPP structure points to the plugin class itself (in npp_new, there is instance-> pdata = (void *) plugin ;). Forcibly convert mnpp-> pdata to a pointer of the plugin class, and then you can access the public members in the class.
Generally, you do not need to call the relevant interfaces of the class. You only need to define a public member variable in the plugin, and plugin * plugin = (plugin *) in the setproperty *) mnpp-> pdata;
Plugin-> m_bar = npvariant_to_int32 (* value); Use plugin * plugin = (plugin *) mnpp-> pdata; int32_to_npvariant (Plugin-> m_bar, * result) in getproperty ); you can.


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.