Teach you how to quickly develop the Firefox plug-in for Sina Weibo (1)

Source: Internet
Author: User
Tags oauth

Firefox plug-in mechanism

For a Firefox plug-in, we first need to understand its organizational structure. Open a Firefox plug-in project, you usually see the following elements: chrome folder, defaults folder, chrome. manifest, install. rdf.

Let's start with install. rdf, and you will understand what this file is about compared to the file name. Yes, this file is the installation file of the Firefox plug-in.

 
 
  1. <!--?xml version="1.0"?-->  
  2. <RDF xmlns:em="http://www.mozilla.org/2004/em-rdf#">  
  3.        
  4.     <DESCRIPTION about="urn:mozilla:install-manifest">  
  5.         <EM:ID>flashcard@gmail.com</EM:ID>  
  6.         <EM:VERSION>0.7</EM:VERSION>  
  7.         <EM:TYPE>2</EM:TYPE>  
  8.         <EM:NAME>flashcard</EM:NAME>  
  9.         <EM:DESCRIPTION>Post the selected word to Sina weibo.</EM:DESCRIPTION>  
  10.         <EM:HOMEPAGEURL>https://feihe.cnblogs.com</EM:HOMEPAGEURL>  
  11.                 <EM:ICONURL>chrome://flashcard/skin/icon.png</EM:ICONURL>  
  12.           
  13.         <EM:CREATOR>Fei He</EM:CREATOR>  
  14.  
  15.         <EM:TARGETAPPLICATION>  
  16.             <DESCRIPTION>  
  17.                 <EM:ID>{ec8030f7-c20a-464f-9b0e-13a3a9e97384}</EM:ID>  
  18.                 <EM:MINVERSION>3.0</EM:MINVERSION>  
  19.                 <EM:MAXVERSION>4.0.*</EM:MAXVERSION>  
  20.             </DESCRIPTION>  
  21.         </EM:TARGETAPPLICATION>  
  22.     </DESCRIPTION>  
  23. </RDF> 

There are some descriptions of your Firefox plug-in, two of which are critical. The id of your Firefox plug-in is under the root node description, that is to say, this item must be unique (at least unique among all your Firefox plug-ins), and the other one is the Firefox id, which cannot be modified. And they are used to describe the compatibility of your Firefox plug-in to the Firefox version.

Next, let's take a look at ults. In a word, defaults is the default setting of the preferences of your Firefox plug-in.

The core of a Firefox plug-in is chrome. manifest and chrome folders. Chrome. manifest is a bit like a. NET project file. It basically refers to the location information of all elements of the entire Firefox plug-in. Firefox itself uses this manifest to locate specific elements. What elements does a Firefox plug-in contain? Open chrome. manifest and you will see it at a glance.

Overlay chrome: // browser/content/browser. xul chrome: // flashcard/content/overlay. xul

Content flashcard chrome/content/flashcard/

Skin flashcard classic chrome/skin/classic/flashcard/

Locale flashcard en-US chrome/locale/flashcard/en-US/

Style chrome: // global/content/customiz1_lbar. xul chrome: // flashcard/skin/skin.css

The structure here is basically a line structure. The header of each line is the specific Firefox plug-in element name, and the following is to tell Firefox to find this element at that position. This includes the following elements:

◆ Overlay: indicates a UI element of your Firefox plug-in, including contextmenu, toolbar, and navigator bar. In the above manifest, you can see that I pointed to a file with the suffix xul. In fact, its full name is Xml User Interface. As the name implies, the UI is described in xml format.

 
 
  1. <!--?xml version="1.0"?-->  
  2. <!--?xml-stylesheet href="chrome://flashcard/skin/skin.css" type="text/css"?-->  
  3.  
  4. <OVERLAY id=flashcardOverlay>  
  5.  
  6.     <SCRIPT type=application/x-javascript><!--mce:0--></SCRIPT>  
  7.     <SCRIPT type=application/x-javascript><!--mce:1--></SCRIPT>  
  8.     <SCRIPT type=application/x-javascript><!--mce:2--></SCRIPT>      
  9.     <SCRIPT type=application/x-javascript><!--mce:3--></SCRIPT>  
  10.  
  11.     <SCRIPT type=application/x-javascript><!--mce:4--></SCRIPT>  
  12.     <SCRIPT type=application/x-javascript><!--mce:5--></SCRIPT>  
  13.     <SCRIPT type=application/x-javascript><!--mce:6--></SCRIPT>  
  14.       
  15.     <MENUPOPUP id=contentAreaContextMenu>  
  16.         <MENUSEPARATOR id=separator_flashcard>  
  17.         <MENUITEM id=menuitem_flashcard_add class=menuitem-iconic image="chrome://flashcard/skin/word.png">  
  18.     </MENUITEM></MENUSEPARATOR></MENUPOPUP>  
  19. </OVERLAY>  

The above is an overlay. xul I used. Here I add a new menuitem to Firefox's contextmenu and separate it with the original menuitems using separator. Here, the file name can be retrieved at will, which is your name must be applied in chrome. manifest. A lot of people are curious here, so where are the corresponding actions of the menuitem you added? Careful, you may find that I reference some javascript in xul, and for our menuitem, Firefox provides the method in 2 to associate behavior: the first is to directly specify the action of control in the oncommand of control; the second is to use document in javascript. getElementByID:

 
 
  1. <DIALOG id=dialog_login title=FlashCard persist="screenX screenY width height" 
  2. windowtype="DialogWindowType" 
  3. buttons="accept,cancel" 
  4. onload="window.sizeToContent();"
  5.  ondialogaccept="return login();">  
  6.   </DIALOG>  
 
 
  1. document.getElementById("menuitem_flashcard_add").addEventListener("click", function(event){}, false);  

In addition, I also specified the css file in this file. In fact, the use of javascript and css in xul files is basically the same as that in html.

◆ Content: the core of your Firefox plug-in, including javascript scripts and XUL

◆ Skin: even if it is skin, you can make different skins for your plug-in. My mainifest specifies the skin that I use classic, so I should correspond to the classic folder in the skin folder.

◆ Locale: International. For our Firefox plug-in that requires a national UI control, we can use its label attribute, at the same time in chrome. define the dtd file under the specified culture folder in manifest, for example, overlay. control definition in xul:

 
 
  1.         <MENUITEM id=menuitem_flashcard_add class=menuitem-iconic   
  2. image="chrome://flashcard/skin/word.png" label="&flashcard.menuitem.label;"> 
  3. </MENUITEM> 

Dtd file definition:

 
 
  1. <!--ENTITY flashcard.menuitem.label "Post to flashcard"-->  

They are associated using the label attribute of control, while overlay. xul and chrome. manifest specifies the dtd file association under the culture folder, you can see in my overlay. xul has the following definition:

◆ Style: some overlay styles, such as css.

Your Firefox plug-in has a UI and corresponding behaviors and styles. What else do you need? You need to store, that is, you need to store preferences or other information. For example, here I want to store the Oauth_token information obtained after the user authorization on Sina Weibo. Firefox provides many storage methods. You can store files in special locations, or use a small file database like sqlite. Another simplest way is to store preferences. For the Firefox plug-in, you can specify your preference file in your install. rdf file so that you can set and save the preference file. Here I want to use preferences for storage, so I don't want users to see it, so I don't need to specify it in install. rdf.

The preference file is also an xul file, so you can also use javascript and css to bind the behavior of the control in your preference and render the style. The preference here is as follows:

 
 
  1. <!--?xml version="1.0"?-->  
  2. <!--?xml-stylesheet href="chrome://global/skin/" type="text/css"?-->  
  3.  
  4. <PREFWINDOW>  
  5.     <PREFPANE label="Flash Card Preferences">  
  6.     <PREFERENCES>  
  7.         <PREFERENCE id=pref_access_token name="Sina.WeiBo.oauth.access_token" type="string">  
  8.         <PREFERENCE id=pref_access_token_secret name="Sina.WeiBo.oauth.access_token_secret" type="string">  
  9.     </PREFERENCE></PREFERENCE></PREFERENCES>  
  10. </PREFPANE></PREFWINDOW>  

The content in the preferences node is used for preference storage.

 
 
  1. Components.classes["@mozilla.org/preferences-service;1"].getService  
  2. (Components.interfaces.nsIPrefService).getBranch("Sina.WeiBo.")  

To get all the names prefixed with Sian. the preference of Weibo, and then call its getCharPref ('oauth. access_token ') to obtain the value, or use setCharPref ('oauth. access_token ') specifies the value. For preference, the mdn api is described in detail. If you reference javascript in preference to compare tricky, if you use prePanel in preference, the reference code of javascript must be behind prePanel. Otherwise, your prePanel will be invisible.

Finally, sometimes you may want your Firefox plug-in to send a notification to the user after some behaviors are completed. In Firefox3, you can use the normal notification or alert, in Firefox4, you can use popupNotification, which is very effective. You can also specify images and corresponding actions so that you can perform further actions after obtaining the notification. Example:

 
 
  1. Popupconfigurications. show (gBrowser. selectedbroications, "flashcard-add ",
  2. '"' + SelectedWord + '" has been successfully added to your ',
  3. Null,
  4. {
  5. Label: "OK ",
  6. AccessKey: "D ",
  7. Callback: function (){
  8. }
  9. },
  10. [
  11. {
  12. Label: "Reset ",
  13. AccessKey: "R ",
  14. Callback: function (){
  15. Browser. Preferences. clearUserPref ("oauth. access_token ");
  16. Browser. Preferences. clearUserPref ("oauth. access_token_secret ");
  17. }
  18. },
  19. ]);

The specified image must be in css.

 
 
  1. .popup-notification-icon[popupid="flashcard-add"] {  
  2.     list-style-image: url("chrome://flashcard/skin/icon.png");  
  3. }  

Here is how to quickly develop a Firefox plug-in. If you want more detailed knowledge, you still need to refer to MDN. It is found that the length is a bit long here, and it is decided to divide it into the first and second articles. Next, we will discuss the Oauth authorization mechanism of Sina WeiBo.


Related Article

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.