SlideShare a Scribd company logo
1 of 28
Download to read offline
An Introduction to
XPConnect
Writing Extensions in Pure JavaScript

Anant Narayanan
Malaviya National Institute of Technology
FOSS.IN 2007
Why is Firefox
successful?
Apart from the fact that it is Open Source
EXTENSIONS
No other browser provides such a feature-rich extensible
development environment
Community
• There is a thriving community behind
extension development

• There are extensions to help you do every

imaginable task with web pages (Firefox) and
email (Thunderbird)

• Powered by the Mozilla platform
Contribution
• Writing extensions is one of the easiest and
most useful ways of contributing to Mozilla

• You just have to scratch your own itch, or

come up with an idea for your extension

• We’ll look into the technical aspect of
developing your extension in this
presentation
Overview
• XPCOM & why it is useful
• XPConnect & why it is useful
• How XPConnect packs punch into Javascript
• How you can develop your very own
extension in pure Javascript in a matter of
hours
(You need to know basic JavaScript)
XPCOM
• Cross Platform Component Object Model
• Provides a framework for writing crossplatform, modular software

• Provides the abstraction required to write
applications that will run on the Mozilla
platform across the variety of operating
systems that Mozilla supports
Components
• Core: Type System, Data Structures, Streams
• UI: Clipboard, Drag-and-Drop, XUL
• Application: Preferences, Profiles, WM
• Network: Channels, Protocol Handlers
• DOM, Mail and several others
• You can even create you own!
Interface Description
• Language neutral way to specify interfaces to
the XPCOM components

• The Interface Definition Language used by
Mozilla (IDL) is slightly different than the
conventional ones

• XPCOM initially meant to be used in C++
XPConnect
• Allows scriptability of XPCOM components
• Simple interoperation between XPCOM and
languages like Javascript, Perl and Python

• Allows transparent access and manipulation

of XPCOM objects via the XPIDL definitions
Javascript & XPConnect
• Javascript run in the Mozilla environment will
have access to all XPCOM components

• Caveat: Only those components that have

interfaces defined in XPIDL will be available

• Developing extensions is breeze, assuming
you already know Javascript

• JS is considerably easier than C++!
Development Tools
• Best way to work with Javascript is Firefox
• Plugins that you will find helpful:
• Console
• Extension Developer
• Firebug
• jsLib
• XPCOMViewer
2
Let’s Get Started
Skeleton of an
Extension
• Every extension is made up of a set of base
files and directory

• This hierarchy is zipped to create your .xpi
re-distributable extension
Visit the Wizard
• Don’t waste time in creating these base files
and directories

• Get your skeleton at
• http://ted.mielczarek.org/code/mozilla/
extensionwiz/

• Will generate a zip file containing the base
extension code
The Code
• All the JavaScript code goes into the
components/ directory

• Put all your other resources - HTML, Images
et. al. in content/

• This content will be available as
chrome://<name>/content/
Power Javascript
• Think of Javascript as a language
• All XPCOM components are available as
regular Javascript OBJECTS
The Lifecycle of an
XPConnect Object
• Every component is uniquely identified by a
Contract ID

• Usually something like:
•
•

@mozilla.org/network/simple-uri;1
@mozilla.org/consoleservice;1
Instantiating a
Component
• Usually, you will just call the getService

method on the component class passing an
interface along

• Components.classes[“@mozilla.org/
moz/jssubscript-loader;1].
getService(Components.interfaces.
mozIJSSubScriptLoader);
Code Snippets
Logging

function jsLog(msg, error) {
var consoleService = Components.classes
["@mozilla.org/consoleservice;1"].getService
(Components.interfaces.nsIConsoleService);
if (error) {
consoleService.logStringError(msg);
} else {
consoleService.logStringMessage(msg);
}
}
Loading other JS files
into a given Object
function jsImport(obj, fName) {
var loader = Components.classes
["@mozilla.org/moz/jssubscript-loader;
1"].getService
(Components.interfaces.mozIJSSubScriptLoader);
loader.loadSubScript
("file://"+__LOCATION__.parent.path+"/"+fName,
obj);
}
Some Theory
• Mozilla introduces the Components object
into the Global JS Namespace

• Components.classes
• Components.interfaces
• Components.results
• etc...
Preventing Clashes
• Since everything Javascript is in the global
namespace...

• ... you need to protect your code by
wrapping them suitably into objects

• Remember, multiple extensions may run on a
single Mozilla instance, and they all share the
namespace
Resources
• Use the XPCOMViewer for offline ready
documentation on the various scriptable
XPCOM components available to you

• eg: Ever felt the need for sockets in
Javascript?

@mozilla.org/network/sockettransport-service;1
Resources (Contd.)
• A lot of repetitive tasks and frequently used
components in Javascript are available as
friendly JS objects via jsLib

• Disadvantage: If your code uses jsLib, it

becomes a pre-requisite for your extension

• Mozilla normally doesn’t allow

dependencies between extensions, but it’s
Ok in this case
Resources (Contd.)
• Run XPConnect powered code in Firebug to
get instantaneous results (kind of like
working in the python interpreter)

• Firebug also will give you helpful error

messages when something goes wrong. Use
the Logger to segregate different types of
messages and view them in Console2
Resources (Contd.)
• Visit XULPlanet for comprehensive online
documentation on XPCOM scriptable
components:

• http://www.xulplanet.com/references/
xpcomref/

• Every serious JS programmer must visit:
• http://javascript.crockford.com/
Questions?
Thank You!
Feel free to contact me:
<anant@kix.in>
http://www.kix.in/
The Web9 Project implements a new protocol handler
entirely in Javascript:
http://code.kix.in/projects/web9

More Related Content

What's hot

MozTW Jetpack Workshop: Taipei
MozTW Jetpack Workshop: TaipeiMozTW Jetpack Workshop: Taipei
MozTW Jetpack Workshop: Taipei
littlebtc
 
Jetpack SDK: The new possibility of the extensions on browser
Jetpack SDK: The new possibility of the extensions on browserJetpack SDK: The new possibility of the extensions on browser
Jetpack SDK: The new possibility of the extensions on browser
littlebtc
 
MozTW Jetpack Workshop: Taichung
MozTW Jetpack Workshop: TaichungMozTW Jetpack Workshop: Taichung
MozTW Jetpack Workshop: Taichung
littlebtc
 

What's hot (20)

Evolution of .NET Framework and Features of different versions
Evolution of .NET Framework and Features of different versionsEvolution of .NET Framework and Features of different versions
Evolution of .NET Framework and Features of different versions
 
Dr. Strangelove, or how I learned to love plugin development
Dr. Strangelove, or how I learned to love plugin developmentDr. Strangelove, or how I learned to love plugin development
Dr. Strangelove, or how I learned to love plugin development
 
docker : how to deploy Digital Experience in a container drinking a cup of co...
docker : how to deploy Digital Experience in a container drinking a cup of co...docker : how to deploy Digital Experience in a container drinking a cup of co...
docker : how to deploy Digital Experience in a container drinking a cup of co...
 
IBM Think Session 8598 Domino and JavaScript Development MasterClass
IBM Think Session 8598 Domino and JavaScript Development MasterClassIBM Think Session 8598 Domino and JavaScript Development MasterClass
IBM Think Session 8598 Domino and JavaScript Development MasterClass
 
Bootstrapping a simple enterprise application with Java EE successor, Jakarta...
Bootstrapping a simple enterprise application with Java EE successor, Jakarta...Bootstrapping a simple enterprise application with Java EE successor, Jakarta...
Bootstrapping a simple enterprise application with Java EE successor, Jakarta...
 
Docker for .NET Developers
Docker for .NET DevelopersDocker for .NET Developers
Docker for .NET Developers
 
Let me introduce you: DOTS
Let me introduce you: DOTSLet me introduce you: DOTS
Let me introduce you: DOTS
 
.Net platform .Net core fundamentals
.Net platform .Net core  fundamentals.Net platform .Net core  fundamentals
.Net platform .Net core fundamentals
 
.Net standard 2.0
.Net standard 2.0.Net standard 2.0
.Net standard 2.0
 
Servlets made easy. 
Write once and run everywhere.
Servlets made easy. 
Write once and run everywhere.Servlets made easy. 
Write once and run everywhere.
Servlets made easy. 
Write once and run everywhere.
 
BP207 - Meet the Java Application Server You Already Own – IBM Domino
BP207 - Meet the Java Application Server You Already Own – IBM DominoBP207 - Meet the Java Application Server You Already Own – IBM Domino
BP207 - Meet the Java Application Server You Already Own – IBM Domino
 
MozTW Jetpack Workshop: Taipei
MozTW Jetpack Workshop: TaipeiMozTW Jetpack Workshop: Taipei
MozTW Jetpack Workshop: Taipei
 
Jetpack SDK: The new possibility of the extensions on browser
Jetpack SDK: The new possibility of the extensions on browserJetpack SDK: The new possibility of the extensions on browser
Jetpack SDK: The new possibility of the extensions on browser
 
Docker Workshop
Docker WorkshopDocker Workshop
Docker Workshop
 
MozTW Jetpack Workshop: Taichung
MozTW Jetpack Workshop: TaichungMozTW Jetpack Workshop: Taichung
MozTW Jetpack Workshop: Taichung
 
Find your data - use GraphDB capabilities in XPages applications - and beyond
Find your data - use GraphDB capabilities in XPages applications - and beyond	Find your data - use GraphDB capabilities in XPages applications - and beyond
Find your data - use GraphDB capabilities in XPages applications - and beyond
 
Java Class 2
Java Class 2Java Class 2
Java Class 2
 
Overview of the new .NET Core and .NET Platform Standard
Overview of the new .NET Core and .NET Platform StandardOverview of the new .NET Core and .NET Platform Standard
Overview of the new .NET Core and .NET Platform Standard
 
What's New in NetBeans IDE 7.x
What's New in NetBeans IDE 7.xWhat's New in NetBeans IDE 7.x
What's New in NetBeans IDE 7.x
 
Extending NetBeans IDE
Extending NetBeans IDEExtending NetBeans IDE
Extending NetBeans IDE
 

Viewers also liked (7)

From OCaml To Javascript At Skydeck
From OCaml To Javascript At SkydeckFrom OCaml To Javascript At Skydeck
From OCaml To Javascript At Skydeck
 
Mozilla Full Text Search
Mozilla Full Text SearchMozilla Full Text Search
Mozilla Full Text Search
 
A Study of SVG-based Application Development @ OSDC 2010
A Study of SVG-based Application Development @ OSDC 2010A Study of SVG-based Application Development @ OSDC 2010
A Study of SVG-based Application Development @ OSDC 2010
 
Your First Xulrunner-based application
Your First Xulrunner-based applicationYour First Xulrunner-based application
Your First Xulrunner-based application
 
ずーるらんなー
ずーるらんなーずーるらんなー
ずーるらんなー
 
Pcom xpcom
Pcom xpcomPcom xpcom
Pcom xpcom
 
Rich Desktop Applications
Rich Desktop ApplicationsRich Desktop Applications
Rich Desktop Applications
 

Similar to Introduction to XPConnect

Intro to Microsoft.NET
Intro to Microsoft.NET Intro to Microsoft.NET
Intro to Microsoft.NET
rchakra
 

Similar to Introduction to XPConnect (20)

Build 2017 - Whats new for Xamarin Devs
Build 2017 - Whats new for Xamarin DevsBuild 2017 - Whats new for Xamarin Devs
Build 2017 - Whats new for Xamarin Devs
 
[DanNotes] XPages - Beyound the Basics
[DanNotes] XPages - Beyound the Basics[DanNotes] XPages - Beyound the Basics
[DanNotes] XPages - Beyound the Basics
 
XPages -Beyond the Basics
XPages -Beyond the BasicsXPages -Beyond the Basics
XPages -Beyond the Basics
 
Firefox (in)Security
Firefox (in)SecurityFirefox (in)Security
Firefox (in)Security
 
FITC - Node.js 101
FITC - Node.js 101FITC - Node.js 101
FITC - Node.js 101
 
Firefox extension Development
Firefox extension DevelopmentFirefox extension Development
Firefox extension Development
 
.NET Core: a new .NET Platform
.NET Core: a new .NET Platform.NET Core: a new .NET Platform
.NET Core: a new .NET Platform
 
B2g
B2gB2g
B2g
 
Introduction to vb.net
Introduction to vb.netIntroduction to vb.net
Introduction to vb.net
 
Intro to Microsoft.NET
Intro to Microsoft.NET Intro to Microsoft.NET
Intro to Microsoft.NET
 
.Net introduction
.Net introduction.Net introduction
.Net introduction
 
Node.js 101 with Rami Sayar
Node.js 101 with Rami SayarNode.js 101 with Rami Sayar
Node.js 101 with Rami Sayar
 
Kushal
KushalKushal
Kushal
 
Plug yourself in and your app will never be the same (2 hr editon)
Plug yourself in and your app will never be the same (2 hr editon)Plug yourself in and your app will never be the same (2 hr editon)
Plug yourself in and your app will never be the same (2 hr editon)
 
Plug yourself in and your app will never be the same (2 hour edition)
Plug yourself in and your app will never be the same (2 hour edition)Plug yourself in and your app will never be the same (2 hour edition)
Plug yourself in and your app will never be the same (2 hour edition)
 
Getting started with add ons
Getting started with add onsGetting started with add ons
Getting started with add ons
 
Moving microsoft .net applications one container at a time
 Moving microsoft .net applications one container at a time  Moving microsoft .net applications one container at a time
Moving microsoft .net applications one container at a time
 
ITB_2023_Extend_your_contentbox_apps_with_custom_modules_Javier_Quintero.pdf
ITB_2023_Extend_your_contentbox_apps_with_custom_modules_Javier_Quintero.pdfITB_2023_Extend_your_contentbox_apps_with_custom_modules_Javier_Quintero.pdf
ITB_2023_Extend_your_contentbox_apps_with_custom_modules_Javier_Quintero.pdf
 
AWS Summit Auckland - Moving MS .NET Applications One Container at a Time
AWS Summit Auckland - Moving MS .NET Applications One Container at a TimeAWS Summit Auckland - Moving MS .NET Applications One Container at a Time
AWS Summit Auckland - Moving MS .NET Applications One Container at a Time
 
Extension Library - Viagra for XPages
Extension Library - Viagra for XPagesExtension Library - Viagra for XPages
Extension Library - Viagra for XPages
 

More from Anant Narayanan

More from Anant Narayanan (20)

Enterprise Scale Knowledge Graphs
Enterprise Scale Knowledge GraphsEnterprise Scale Knowledge Graphs
Enterprise Scale Knowledge Graphs
 
Building an Intelligent Assistant
Building an Intelligent AssistantBuilding an Intelligent Assistant
Building an Intelligent Assistant
 
WebRTC: A Practical Introduction
WebRTC: A Practical IntroductionWebRTC: A Practical Introduction
WebRTC: A Practical Introduction
 
Message Passing vs. Data Synchronization
Message Passing vs. Data SynchronizationMessage Passing vs. Data Synchronization
Message Passing vs. Data Synchronization
 
Firebase: Tales from the Trenches
Firebase: Tales from the TrenchesFirebase: Tales from the Trenches
Firebase: Tales from the Trenches
 
WebRTC: An Overview
WebRTC: An OverviewWebRTC: An Overview
WebRTC: An Overview
 
Error Handling in WebRTC
Error Handling in WebRTCError Handling in WebRTC
Error Handling in WebRTC
 
WebRTC Demystified
WebRTC DemystifiedWebRTC Demystified
WebRTC Demystified
 
WebRTC: User Security & Privacy
WebRTC: User Security & PrivacyWebRTC: User Security & Privacy
WebRTC: User Security & Privacy
 
Firefox Architecture Overview
Firefox Architecture OverviewFirefox Architecture Overview
Firefox Architecture Overview
 
πP
πPπP
πP
 
Next Generation Browser Add-Ons
Next Generation Browser Add-OnsNext Generation Browser Add-Ons
Next Generation Browser Add-Ons
 
An Overview of Distributed Debugging
An Overview of Distributed DebuggingAn Overview of Distributed Debugging
An Overview of Distributed Debugging
 
A Brief Incursion into Botnet Detection
A Brief Incursion into Botnet DetectionA Brief Incursion into Botnet Detection
A Brief Incursion into Botnet Detection
 
Mozilla Weave: Integrating Services into the Browser
Mozilla Weave: Integrating Services into the BrowserMozilla Weave: Integrating Services into the Browser
Mozilla Weave: Integrating Services into the Browser
 
about:labs
about:labsabout:labs
about:labs
 
Distributed File Systems: An Overview
Distributed File Systems: An OverviewDistributed File Systems: An Overview
Distributed File Systems: An Overview
 
Innovating with Mozilla Labs
Innovating with Mozilla LabsInnovating with Mozilla Labs
Innovating with Mozilla Labs
 
Glendix: The Why and the How
Glendix: The Why and the HowGlendix: The Why and the How
Glendix: The Why and the How
 
Mozilla Prism
Mozilla PrismMozilla Prism
Mozilla Prism
 

Recently uploaded

CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 

Recently uploaded (20)

The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 

Introduction to XPConnect

  • 1. An Introduction to XPConnect Writing Extensions in Pure JavaScript Anant Narayanan Malaviya National Institute of Technology FOSS.IN 2007
  • 2. Why is Firefox successful? Apart from the fact that it is Open Source EXTENSIONS No other browser provides such a feature-rich extensible development environment
  • 3. Community • There is a thriving community behind extension development • There are extensions to help you do every imaginable task with web pages (Firefox) and email (Thunderbird) • Powered by the Mozilla platform
  • 4. Contribution • Writing extensions is one of the easiest and most useful ways of contributing to Mozilla • You just have to scratch your own itch, or come up with an idea for your extension • We’ll look into the technical aspect of developing your extension in this presentation
  • 5. Overview • XPCOM & why it is useful • XPConnect & why it is useful • How XPConnect packs punch into Javascript • How you can develop your very own extension in pure Javascript in a matter of hours (You need to know basic JavaScript)
  • 6. XPCOM • Cross Platform Component Object Model • Provides a framework for writing crossplatform, modular software • Provides the abstraction required to write applications that will run on the Mozilla platform across the variety of operating systems that Mozilla supports
  • 7. Components • Core: Type System, Data Structures, Streams • UI: Clipboard, Drag-and-Drop, XUL • Application: Preferences, Profiles, WM • Network: Channels, Protocol Handlers • DOM, Mail and several others • You can even create you own!
  • 8. Interface Description • Language neutral way to specify interfaces to the XPCOM components • The Interface Definition Language used by Mozilla (IDL) is slightly different than the conventional ones • XPCOM initially meant to be used in C++
  • 9. XPConnect • Allows scriptability of XPCOM components • Simple interoperation between XPCOM and languages like Javascript, Perl and Python • Allows transparent access and manipulation of XPCOM objects via the XPIDL definitions
  • 10. Javascript & XPConnect • Javascript run in the Mozilla environment will have access to all XPCOM components • Caveat: Only those components that have interfaces defined in XPIDL will be available • Developing extensions is breeze, assuming you already know Javascript • JS is considerably easier than C++!
  • 11. Development Tools • Best way to work with Javascript is Firefox • Plugins that you will find helpful: • Console • Extension Developer • Firebug • jsLib • XPCOMViewer 2
  • 13. Skeleton of an Extension • Every extension is made up of a set of base files and directory • This hierarchy is zipped to create your .xpi re-distributable extension
  • 14. Visit the Wizard • Don’t waste time in creating these base files and directories • Get your skeleton at • http://ted.mielczarek.org/code/mozilla/ extensionwiz/ • Will generate a zip file containing the base extension code
  • 15. The Code • All the JavaScript code goes into the components/ directory • Put all your other resources - HTML, Images et. al. in content/ • This content will be available as chrome://<name>/content/
  • 16. Power Javascript • Think of Javascript as a language • All XPCOM components are available as regular Javascript OBJECTS
  • 17. The Lifecycle of an XPConnect Object • Every component is uniquely identified by a Contract ID • Usually something like: • • @mozilla.org/network/simple-uri;1 @mozilla.org/consoleservice;1
  • 18. Instantiating a Component • Usually, you will just call the getService method on the component class passing an interface along • Components.classes[“@mozilla.org/ moz/jssubscript-loader;1]. getService(Components.interfaces. mozIJSSubScriptLoader);
  • 20. Logging function jsLog(msg, error) { var consoleService = Components.classes ["@mozilla.org/consoleservice;1"].getService (Components.interfaces.nsIConsoleService); if (error) { consoleService.logStringError(msg); } else { consoleService.logStringMessage(msg); } }
  • 21. Loading other JS files into a given Object function jsImport(obj, fName) { var loader = Components.classes ["@mozilla.org/moz/jssubscript-loader; 1"].getService (Components.interfaces.mozIJSSubScriptLoader); loader.loadSubScript ("file://"+__LOCATION__.parent.path+"/"+fName, obj); }
  • 22. Some Theory • Mozilla introduces the Components object into the Global JS Namespace • Components.classes • Components.interfaces • Components.results • etc...
  • 23. Preventing Clashes • Since everything Javascript is in the global namespace... • ... you need to protect your code by wrapping them suitably into objects • Remember, multiple extensions may run on a single Mozilla instance, and they all share the namespace
  • 24. Resources • Use the XPCOMViewer for offline ready documentation on the various scriptable XPCOM components available to you • eg: Ever felt the need for sockets in Javascript? @mozilla.org/network/sockettransport-service;1
  • 25. Resources (Contd.) • A lot of repetitive tasks and frequently used components in Javascript are available as friendly JS objects via jsLib • Disadvantage: If your code uses jsLib, it becomes a pre-requisite for your extension • Mozilla normally doesn’t allow dependencies between extensions, but it’s Ok in this case
  • 26. Resources (Contd.) • Run XPConnect powered code in Firebug to get instantaneous results (kind of like working in the python interpreter) • Firebug also will give you helpful error messages when something goes wrong. Use the Logger to segregate different types of messages and view them in Console2
  • 27. Resources (Contd.) • Visit XULPlanet for comprehensive online documentation on XPCOM scriptable components: • http://www.xulplanet.com/references/ xpcomref/ • Every serious JS programmer must visit: • http://javascript.crockford.com/
  • 28. Questions? Thank You! Feel free to contact me: <anant@kix.in> http://www.kix.in/ The Web9 Project implements a new protocol handler entirely in Javascript: http://code.kix.in/projects/web9