Still an example of the official website, is http://mxr.mozilla.org/seamonkey/source/modules/plugin/tools/sdk/samples/scriptable/
The platform I want to use is Windows.
Compile the project as instructed on the web page: https://developer.mozilla.org/en/Compiling_The_npruntime_Sample_Plugin_in_Visual_Studio
It should be noted that because this project references the IDL Language, you should not use the ms midl compiler. You need to modify the settings. after the idl is added to the project, select the file to view the file attributes and change the value of "general"-> "tool" from "MIDL tool" to "Custom generation tool ", set the value of "Custom generation step"-> "command line" to "$ (ProjectDir) script. bat $ (InputFileName) ", change the" output "value ". /$ (InputName ). h ", if your project name is not
Npscript. bat. The file name can start at will. The content is
.. /SDK/bin/xpidl.exe-M header-I .. /SDK/IDL % 1 <br/> .. /SDK/bin/xpidl.exe-M typelib-I .. /SDK/IDL % 1
You need to download some shared files to this project.
Http://mxr.mozilla.org/seamonkey/source/modules/plugin/tools/sdk/samples/include/
Http://mxr.mozilla.org/seamonkey/source/modules/plugin/tools/sdk/samples/common/
Then, you can compile the project.
Next let's take a look at how to use this program to call the xpcom-test component we wrote earlier.
Add the configuration of the Linked Library
"Project Properties"-> "linker"-> "input"-> "Additional dependency" value is set to xpcomglue_s.lib xpcom. lib nspr4.lib version. lib
"Project Properties"-> "linker"-> "general"-> "additional library directory" value is set to./sdk/lib
Add in the idl file
Long Run (in long A, in long B );
Implementation in nsScriptablePeer. cpp
NS_IMETHODIMP nsScriptablePeer: Run (PRInt32 a, PRInt32 B, PRInt32 * _ retval) <br/>{< br/> if (mPlugin) <br/> * _ retval = mPlugin-> run (a * a, B * B); <br/> return NS_ OK; <br/>}
Add functions in plugin. h
Long run (long a, long B );
In plugin. cpp
Long nsPluginInstance: run (long a, long B) <br/>{< br/> PRInt32 lTemp = 0; <br/> ISpecialThing * nsspecial; <br/> nsCOMPtr <nsIServiceManager> serviceMgr; <br/> NS_GetServiceManager (getter_AddRefs (serviceMgr); <br/> if (serviceMgr) <br/>{< br/> serviceMgr-> GetServiceByContractID ("@ starkravingfinkle.org/specialthing1_1", NS_GET_IID (ISpecialThing), (void **) & nsspecial ); </p> <p> if (nsspecial) <br/> nsspecial-> Add (a * a, B * B, & lTemp ); <br/>}< br/> NS_IF_RELEASE (nsspecial); <br/> return lTemp; <br/>}
Then compile
Next, convert the compiled plugin to extension.
Step 1. Create a file named install. rdf
<RDF xmlns = "http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns: em = "http://www.mozilla.org/2004/em-rdf#">
<Description about = "urn: mozilla: install-manifest">
<Em: Id> npscript@myplugin.com </em: Id>
<Em: Name> npscript </em: Name>
<Em: version> 1.0 </em: version>
<Em: type> 2 </em: type>
<Em: targetapplication>
<Description>
<Em: Id> {ec8030f7-c20a-464f-9b0e-13a3a9e97384} </em: Id>
<Em: minversion> 1.5 </em: minversion>
<Em: maxversion> 3. 0. * </em: maxversion>
</Description>
</Em: targetapplication>
</Description>
</RDF>
[Description]
1. The mail format is better.
2. You must have a name ID.
3. You can describe the version information by yourself, but the Toolkit version format must be satisfied.
4. ID {ec8030f7-c20a-464f-9b0e-13a3a9e97384} cannot be modified. This is the Firefox identifier
Step 2. Run the generated npscript. DLL, nsiscriptablepluginsample. xpt is placed in the directory of A plugins, And the plug-ins and the install. the RDF file is placed in the directory npscript@myplugin.com (the directory name should use install. (EM: ID value in RDF), use RAR to pack and change the suffix. xpi
The directory structure is
Npscript@myplugin.com
|-Install. RDF
|-Plugins/
| -- Npscript. dll
| -- Nsiscriptablepluginsample. xpt
|-Comoponents
| -- Comp. xpt
| -- Test. dll
Step 3. Place the directory npscript@myplugin.com under program files/Mozilla Firefox/extensions and restart Firefox
Add buttons and methods to the test.html file under the npscriptproject
<Input type = button value = "A * A + B * B" onclick = 'calc () '> <br/> function calc () <br/>{< br/> alert (embed. run (2, 3); <br/>}
13. Open the HTML in Firefox. click the button to view the result.