My Firefox plug-in development journey (6) -- some basic knowledge of FF plug-ins

Source: Internet
Author: User

In the previous article, I learned the npruntime example program. I can't wait to implement my own plug-in. I decided to use VS 2005.

A new project named npgnet is created. According to the npruntime example, np_entry.cpp, npn_gate.cpp,
The npp_gate.cpp and npgnet. Def files create a cgnetffplugin class and add the key code in the example.

Some Function entity codes in the cplugin class, because the functions I implemented are irrelevant to the ones in the example ). After compilation, put the generated npgnet. dll in the FF plugins directory, and then

Enter "about: plugins" in the address bar. I don't know how to use this plug-in! What's going on? I have written all the three export functions according to the standard? After comparing the files, I did not add. RC and
Resource. H. This may be the reason.

Back to
2005. A version resource item is added to the resource panel. After modifying resource items such as productname, It is inferior to the npruntime example.
Mimetype. This stuff is very important. This gives me the feeling that it is the ID card of the FF plug-in. FF relies on this stuff to match and identify your plug-in. But I do not know
How to add a version key value in 2005, so I have to use editplus to open npgnet. RC and manually add
Mimetype: Application/Mozilla-npgnet-Scriptable-plugin. OK, now. RC and
Resource. H is all European, compile again, put the generated npgnet. dll in the FF plugins directory, and then type about: Plugins in the address bar, I
No plug-ins for me! It's hard to understand!

Big Head ...... Next, I made a series of code comparisons and attempts, but failed n times, so I will skip them here. Finally, we found that the cause was above this. RC. My. RC is in
In 2005, the default language is simplified Chinese, while the npruntime example is in English. After comparing it with a file comparison tool, codePage and some code are not stored.

Too. In fact, you only need to replace the. RC file of npruntime with my file, compile the output DLL, and FF will be able to recognize it! Why? Yes. RC is missing some.

What? Or can FF only recognize English. RC? I don't plan to study that much. At least the key points of my plug-in are not above. I still have a lot to do later. As long as FF can recognize it,
That's good.

Since the decision to write plug-ins, we need to first understand the concept of plug-ins, on this page has a very detailed introduction: https://developer.mozilla.org/en/Gecko_Plugin_API_Reference/Plug-in_Basics

The following are some of my reading notes and experiences:

I. Plug-in loading process

When a page is opened, if a plug-in is embedded on the page, the browser will do the following:

  • Check whether a matching plug-in exists through mimetype
  • Load plug-in code to memory
  • Initialize the plug-in
  • Create a new plug-in instance

A plug-in can be added to multiple objects on a page or instantiated in different windows at the same time. When the page is closed, the plug-in instance is destroyed. When the last instance is deleted,
The plug-in code will be detached from the memory.

The following describes the function calling process in the plug-in:

  • If the plug-in is loaded into memory for the first time, the browser will call the plug-inNp_initialize
    Method. For convenience, all
    The plug-in definition function starts with "NPP", and all browser-defined functions start with "NPP.
  • When the browser creates a plug-in instance, it callsNpp_new
    Method.
  • When a plug-in instance is deleted (such as closing the page or closing the window ),Npp_destroy
    Method.
  • When the last instance is deleted and the plug-in is detached from the memoryNp_shutdown
    Method.

2. plugin Detection

You can use JavaScript to check whether a plug-in has been installed. The following is the test code:
Function detectffplugin ()
{
VaR mimetype =
Navigator. mimetypes ["application/Mozilla-npgnet-Scriptable-plugin"];
If (mimetype)
{
VaR plugin = mimetype. enabledplugin;
If (plugin)
{
Document. writeln ("plugin had been installed and be enabled .");
}
}
Else
{
Document. writeln ("sorry, plugin has not been installed .");
}
}

Well, I think this detection is useful here. When the user has not been installed, You can instruct the user where to download and install (to a pretty page). When the detection has been installed, it will be dynamic
Load the plug-in code. Good. :)

Iii. Plug-in structure Overview

The methods in a plug-in can be divided into the plug-in method (plug-in methods) and the browser method (Browser
Methods ). The plug-in method is the method that you execute in the plug-in, with the NPP prefix as the name. The browser method is the methods executed by Gecko, named with the prefix of "x6. Quantity
Data Structures start with NP.

The plug-in can be divided into two types: window and window. However, it is recommended to use window in the article. This will be more stable and easy to control. In addition, the plug-in can be used as part of the page, and can be used
HTML code to control whether the plug-in is displayed or not.

There are two ways to make a plug-in invisible:

1. If you use the embed label, you can use its hidden attribute, for example, <embed src = "audiplay. AIFF"
Type = "audio/X-AIFF" hidden = "true">
2. If you use the object tag, you can use CSS to hide it because it does not have the den attribute:

Object
{
Visibility: visible;
}
Object. hiddenobject
{
Visibility: hidden! Important;
Width: 0px! Important;
Height: 0px! Important;
Margin: 0px! Important;
Padding: 0px! Important;
Border-style: none! Important;
Border-width: 0px! Important;
Max-width: 0px! Important;
Max-Height: 0px! Important;
}

<Object Data = "audiplay. AIFF" type = "audio/X-AIFF"Class = "hiddenobject"
> </Object>

Next, we will introduce the use of the Object Label. An example is provided to illustrate how ActiveX and plug-ins can be integrated for use.

The last part is the description and examples of various attributes of the object and embed labels.

It is worth mentioning that a plug-in can be embedded on the page for both object and embed labels. How can we choose between them? The article contains the following paragraph:

Thoughobject
Element is the preferred way to invoke
Plug-ins,embed
Element can be used for backward
Compatibility with Netscape 4.x browsers, and in cases where you
Specifically want to prompt the user to install a plug-in, because
Default plug-in is only automatically invoked when you useembed
Element.

Although the object is a recommended method for calling plug-ins, embed can also be embedded into the plug-in (Netscape
4. if you want to prompt the user to install the plug-in before installing the plug-in, you should choose to use embed, because FF's default plug-in system only uses the embed label
This will automatically help you complete the prompts for user installation.

After reading it, it seems that the use of an object is more complex than that of embed. At least this is true for the FF browser, although it is html
W3C standards. In most cases, you can use embed. What's more, who is still using a browser later than Netscape 4.x? :)

OK. After several days of study, I finally had a preliminary perceptual knowledge about writing FF plug-ins. So far, everything has been quite smooth.

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.