NPAPI Scripting interface

Source: Internet
Author: User
Tags hasproperty

http://blog.csdn.net/hgl868/article/details/8576531

The implementation of the scriptable interface, the functions related to properties are Hasproperty, GetProperty, SetProperty. Set properties in JS (in bar for example) with Plugin.bar=barvalue; To set, get properties directly with Plugin.bar; If you want to create properties for a plug-in, you must return true in Hasproperty. In GetProperty General call Npn_getproperty (Mnpp,swindowobj,name,result), you can write their own implementation code if there is a special purpose; Set properties with Npn_setproperty (MNPP, Swindowobj,name,value); Method-related functions are Hasmethod and invoke, and if you want to create a method for the plug-in, you must return true for the function in Hasmethod. The implementation code of method should be called by invoke or by Invoke. If both method and property, then in the JS can be plugin.foo=value, and plugin.foo to set or get the attribute value, but also can use Plugin.foo (), call the corresponding function function, but use Plugin.foo () is the implementation code that calls GetProperty before calling the Foo () method.

One of the simplest examples 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, in JS with any string instead of the preceding Foo or bar will normally call the above-mentioned related code. One point to note is that any object in JS can set properties such as:

var t=new Object ();
T.anystr=val;
alert (T.ANYSTR);

this way with the previous difference is that this is only the operation of the JS kernel, the properties of its settings are inaccessible in the plug-in code, the plug-in implementation of the mechanism of property settings is to facilitate access to the properties through the JS settings! The
to set several specific properties or methods needs to be implemented in conjunction with Npn_getstringidentifier. See example procedure for details.
in scriptable plug-ins, it is necessary to share data between the plugin class and the Scriptableobject class, but using global variables is certainly an option, but this can have serious consequences when there are multiple plug-in instances in a page. This is therefore not advocated. Note that the Scriptableobject member MNPP is Getscriptobject returned, and this MNPP is the NPP structure in the plugin class, and the pdata of this NPP structure is pointing to the plugin class itself (in Npp_ New has instance->pdata = (void *) plugin;). Cast the Mnpp->pdata to a pointer to the plugin class, and then you can access the public members in it.
setting or getting properties generally does not require the invocation of an NPN-related interface, just define a public member variable in plugin, plugin* plugin= (plugin*) in SetProperty mnpp->pdata;
Plugin->m_bar = Npvariant_to_int32 (*value); using plugin* plugin= (plugin*) mnpp->pdata;int32_to_ in GetProperty Npvariant (Plugin->m_bar,*result); it's ready.

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.