Creating Applications with Mozilla-Chapter 5. Scripting Mozilla- P4
Số trang: 22
Loại file: pdf
Dung lượng: 48.63 KB
Lượt xem: 10
Lượt tải: 0
Xem trước 3 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
Tham khảo tài liệu creating applications with mozilla-chapter 5. scripting mozilla- p4, công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả
Nội dung trích xuất từ tài liệu:
Creating Applications with Mozilla-Chapter 5. Scripting Mozilla- P4 Chapter 5. Scripting Mozilla- P4Figure 5-4. How XPConnect fits into the application modelIn Mozilla, XPConnect is the bridge between JavaScript and XPCOMcomponents. The XPConnect technology wraps natively compiledcomponents with JavaScript objects. XPCOM, Mozillas own cross-platformcomponent technology, is the framework on top of which these scriptablecomponents are built. Using JavaScript and XPConnect, you can createinstances of these components and use their methods and properties as youdo any regular JavaScript object, as described here. You can access any orall of the functionality in Mozilla in this way.Chapter 8 describes more about the XPConnect technology and how itconnects components to the interface. It also describes the componentsthemselves and their interfaces, the XPCOM technology, and how you cancreate your own XPCOM components.5.4.1.1. Creating XPCOM objects in scriptExample 5-10 demonstrates the creation and use of an XPCOM componentin JavaScript. In this example, the script instantiates the filepickerobject and then uses it to display a file picker dialog with all of the file filtersselected. To run this example, add the function to your xfly.js file and call itfrom an event handler on the New menu item you added in Example 3-5.Example 5-10. Scriptable component example// chooseApp: Open file picker and prompt user forapplication.chooseApp: function( ) { var nsIFilePicker =Components.interfaces.nsIFilePicker; var fp =Components.classes[@mozilla.org/filepicker;1]. createInstance( nsIFilePicker ); fp.init( this.mDialog, this.getString( chooseAppFilePickerTitle ), nsIFilePicker.modeOpen ); fp.appendFilters( nsIFilePicker.filterAll ); if ( fp.show( ) == nsIFilePicker.returnOK &&fp.file ) { this.choseApp = true; this.chosenApp = fp.file; // Update dialog.this.updateApplicationName(this.chosenApp.unicodePath);}Note the first two lines in the function and the way they work together tocreate the fp filepicker object. The first line in the function assigns thename of the nsFilepicker interface to the nsIFilePicker variable inJavaScript. This variable is used in the second line, where the instance iscreated from the component to specify which interface on that componentshould be used. Discovering and using library interfaces is an importantaspect of XPCOM, where components always implement at least twointerfaces.In Example 5-11, an HTML file (stored locally, since it wouldnt have therequired XPConnect access as a remote file because of security boundaries)loaded in Mozilla instantiates a Mozilla sound component and plays a soundwith it. Go ahead and try it.Example 5-11. Scripting components from HTML Sound Service Play Example function play() {netscape.security.PrivilegeManager.enablePrivilege(UniversalXPConnect); var sample =Components.classes[@mozilla.org/sound;1].createInstance(); sample =sample.QueryInterface(Components.interfaces.nsISound); const SND_NETWORK_STD_CID =@mozilla.org/network/standard-url;1; const SND_I_URL = nsIURL; const SND_URL = newC.Constructor(SND_NETWORK_STD_CID, SND_I_URL); var url = new SND_URL(); url.spec =http://jslib.mozdev.org/test.wav; sample.play(url); } As in Example 5-10, the classes[ ] array on the special MozillaComponents object refers to a particular component -- in this case, thesound component -- by contract ID. All XPCOM objects must have acontract ID that uniquely identifies them with the domain, the componentname, and a version number [@mozilla.org/sound;1],respectively. See the Section 8.1.5 section in Chapter 8 for more informationabout this.5.4.1.2. Finding components and interfacesMost components are scripted in Mozilla. In fact, the challenge is not to findcases when this scripting occurs (which you can learn by searching LXR forthe Components), but to find Mozilla components that dont use scriptablecomponents. Finding components and interfaces in Mozilla and seeing howthey are used can be useful when writing your own application.The Mozilla Component Viewer is a great tool for discovering componentsand provides a convenient UI for seeing components and looking at theirinterfaces from within Mozilla. The Component Viewer can be built as anextension to Mozilla (see cview in the extensions directory of the Mozillasource), or it can be downloaded and installed as a separate XPI fromhttp://www.hacksrus.com/~ginda/cview/. Appendix B describes theComponent Viewer in more detail.Commonly used XPCOM objects in the browser and other Mozillaapplications include file objects, RDF services, URL objects, and categorymanagers.5.4.1.3. Selecting the appropriate interface from the componentIn all cases, the way to get the object into script is to instantiate it with thespecial classes object and use the createInstance( ) method onthe class to select the interface you want to use. These two steps are oftendone together, as in the following example, which gets the component withthe contract ID ldap-connection;1, instantiates an object from thensILDAPConnection interface, and then calls a method on that object:var connection = Components.classes [@mozilla.org/network/ldap-connection;1].createInstance(Components.interfaces.nsILDAPConnection); connection.init(queryURL.host, queryURL.port,null,generateGetTargetsBoundCallback( ));These two common processes -- getting a component and selecting one of itsinterfaces to assign to an object -- can also be separated into two differentstatements:// get the ldap connec ...
Nội dung trích xuất từ tài liệu:
Creating Applications with Mozilla-Chapter 5. Scripting Mozilla- P4 Chapter 5. Scripting Mozilla- P4Figure 5-4. How XPConnect fits into the application modelIn Mozilla, XPConnect is the bridge between JavaScript and XPCOMcomponents. The XPConnect technology wraps natively compiledcomponents with JavaScript objects. XPCOM, Mozillas own cross-platformcomponent technology, is the framework on top of which these scriptablecomponents are built. Using JavaScript and XPConnect, you can createinstances of these components and use their methods and properties as youdo any regular JavaScript object, as described here. You can access any orall of the functionality in Mozilla in this way.Chapter 8 describes more about the XPConnect technology and how itconnects components to the interface. It also describes the componentsthemselves and their interfaces, the XPCOM technology, and how you cancreate your own XPCOM components.5.4.1.1. Creating XPCOM objects in scriptExample 5-10 demonstrates the creation and use of an XPCOM componentin JavaScript. In this example, the script instantiates the filepickerobject and then uses it to display a file picker dialog with all of the file filtersselected. To run this example, add the function to your xfly.js file and call itfrom an event handler on the New menu item you added in Example 3-5.Example 5-10. Scriptable component example// chooseApp: Open file picker and prompt user forapplication.chooseApp: function( ) { var nsIFilePicker =Components.interfaces.nsIFilePicker; var fp =Components.classes[@mozilla.org/filepicker;1]. createInstance( nsIFilePicker ); fp.init( this.mDialog, this.getString( chooseAppFilePickerTitle ), nsIFilePicker.modeOpen ); fp.appendFilters( nsIFilePicker.filterAll ); if ( fp.show( ) == nsIFilePicker.returnOK &&fp.file ) { this.choseApp = true; this.chosenApp = fp.file; // Update dialog.this.updateApplicationName(this.chosenApp.unicodePath);}Note the first two lines in the function and the way they work together tocreate the fp filepicker object. The first line in the function assigns thename of the nsFilepicker interface to the nsIFilePicker variable inJavaScript. This variable is used in the second line, where the instance iscreated from the component to specify which interface on that componentshould be used. Discovering and using library interfaces is an importantaspect of XPCOM, where components always implement at least twointerfaces.In Example 5-11, an HTML file (stored locally, since it wouldnt have therequired XPConnect access as a remote file because of security boundaries)loaded in Mozilla instantiates a Mozilla sound component and plays a soundwith it. Go ahead and try it.Example 5-11. Scripting components from HTML Sound Service Play Example function play() {netscape.security.PrivilegeManager.enablePrivilege(UniversalXPConnect); var sample =Components.classes[@mozilla.org/sound;1].createInstance(); sample =sample.QueryInterface(Components.interfaces.nsISound); const SND_NETWORK_STD_CID =@mozilla.org/network/standard-url;1; const SND_I_URL = nsIURL; const SND_URL = newC.Constructor(SND_NETWORK_STD_CID, SND_I_URL); var url = new SND_URL(); url.spec =http://jslib.mozdev.org/test.wav; sample.play(url); } As in Example 5-10, the classes[ ] array on the special MozillaComponents object refers to a particular component -- in this case, thesound component -- by contract ID. All XPCOM objects must have acontract ID that uniquely identifies them with the domain, the componentname, and a version number [@mozilla.org/sound;1],respectively. See the Section 8.1.5 section in Chapter 8 for more informationabout this.5.4.1.2. Finding components and interfacesMost components are scripted in Mozilla. In fact, the challenge is not to findcases when this scripting occurs (which you can learn by searching LXR forthe Components), but to find Mozilla components that dont use scriptablecomponents. Finding components and interfaces in Mozilla and seeing howthey are used can be useful when writing your own application.The Mozilla Component Viewer is a great tool for discovering componentsand provides a convenient UI for seeing components and looking at theirinterfaces from within Mozilla. The Component Viewer can be built as anextension to Mozilla (see cview in the extensions directory of the Mozillasource), or it can be downloaded and installed as a separate XPI fromhttp://www.hacksrus.com/~ginda/cview/. Appendix B describes theComponent Viewer in more detail.Commonly used XPCOM objects in the browser and other Mozillaapplications include file objects, RDF services, URL objects, and categorymanagers.5.4.1.3. Selecting the appropriate interface from the componentIn all cases, the way to get the object into script is to instantiate it with thespecial classes object and use the createInstance( ) method onthe class to select the interface you want to use. These two steps are oftendone together, as in the following example, which gets the component withthe contract ID ldap-connection;1, instantiates an object from thensILDAPConnection interface, and then calls a method on that object:var connection = Components.classes [@mozilla.org/network/ldap-connection;1].createInstance(Components.interfaces.nsILDAPConnection); connection.init(queryURL.host, queryURL.port,null,generateGetTargetsBoundCallback( ));These two common processes -- getting a component and selecting one of itsinterfaces to assign to an object -- can also be separated into two differentstatements:// get the ldap connec ...
Tìm kiếm theo từ khóa liên quan:
thủ thuật máy tính công nghệ thông tin tin học quản trị mạng computer networkGợi ý tài liệu liên quan:
-
52 trang 429 1 0
-
24 trang 353 1 0
-
Top 10 mẹo 'đơn giản nhưng hữu ích' trong nhiếp ảnh
11 trang 312 0 0 -
Làm việc với Read Only Domain Controllers
20 trang 299 0 0 -
74 trang 294 0 0
-
96 trang 291 0 0
-
Báo cáo thực tập thực tế: Nghiên cứu và xây dựng website bằng Wordpress
24 trang 288 0 0 -
Đồ án tốt nghiệp: Xây dựng ứng dụng di động android quản lý khách hàng cắt tóc
81 trang 278 0 0 -
EBay - Internet và câu chuyện thần kỳ: Phần 1
143 trang 273 0 0 -
Tài liệu dạy học môn Tin học trong chương trình đào tạo trình độ cao đẳng
348 trang 269 1 0