SlideShare une entreprise Scribd logo
1  sur  4
Télécharger pour lire hors ligne
Firefox Extension Development
Tutorial

Table of contents
 1   Overview............................................................................................................................2
 2   JavaScript Basics................................................................................................................2
 3   Working with the DOM..................................................................................................... 3
 4   Further Reading..................................................................................................................4




                          Copyright © 2005 LCC 3401 Firefox Group All rights reserved.
Firefox Extension Development Tutorial




1. Overview
All of the guts of your extension will be written in JavaScript. If you already know
JavaScript than you are good to go! If you are new to JavaScript it is highly recommended
that you read through the next section and the supporting documentation to get used to the
syntax.
All of your JavaScript code files should go in the content directory of your extension. This is
where the XUL files go as well, so they will be able to easily reference the code. Just insert
the following line in an XUL file that needs to run functions from your files...

             <script type="application/x-javascript"
src="chrome://hpsched/content/mycode.js" />

Make sure you use the chrome URI so that Firefox can find the file correctly.

2. JavaScript Basics
JavaScript is a fairly straight forward scripting language. Let's jump right into some sample
code:

             /*
              * Save the Schedules List to the
"extensions.hpsched.schedules" preference.
              * This is called by the pref's system when the GUI element is
altered.
              */
             function saveSchedulesList() {
                var schedulesList =
document.getElementById("schedulesList").childNodes;
                var prefString = "";
                    for (var i=0; i < schedulesList.length; i++) {
                      var columns = schedulesList[i].childNodes[0].childNodes;
                      var str = columns[0].getAttribute("label") + ".ITEM."
                              + columns[1].getAttribute("label") + ".ITEM."
                              + columns[2].getAttribute("label") + ".ITEM."
                              + columns[3].getAttribute("label");
                      if(prefString == "")
                        prefString = str;
                      else
                        prefString += ".NEXT." + str;
                    }
                    /* return the new prefString to be stored by pref system */
                    return prefString;
                }



                                                                                                 Page 2
                    Copyright © 2005 LCC 3401 Firefox Group All rights reserved.
Firefox Extension Development Tutorial




The first line defines a function named 'saveSchedulesList' that brings in zero arguments. The
next two lines create and initialize two variables. Notice that JavaScript is a typeless
language, just type var to let the parser know you are declaring a variable. The for-loop and if
statement looks just like what you would expect in C or Java. You will also notice that you
do not have to state that this function is (or is not) returning a value. If you would like to
return a value, just use the common 'return x;' statement. If no return statement is provided
than the function returns (void), which is ignored by the calling function.
Debugging JavaScript is also quite easy: just use the 'alert()' function call. Whenever Firefox
is running code and sees a call such as 'alert("I am here!");', Firefox will display a graphical
error box with the given message. This is a wonderful way to determine if a certain area of
code is being executed, what the current value of some variable is (i.e. alert(myVar)), and
more. Use alert(), it is your friend!
Please see the Further Reading section for links to some wonderful JavaScript tutorials.


3. Working with the DOM
JavaScript is very common in web development because it provides functionality to easily
work with the DOM (Document Object Module). All HTML files on the net are rendered by
the browser by first building a tree. Each node in the tree corresponds to a tag in the HTML
file. Firefox's XUL files work the same way, all elements are nodes in a large tree (per file).
The top level element is called the 'document'.
There is one very important JavaScript function that you will need to know to be able to
work with the XUL elements in your GUI. It allows you to access the object itself, using its
ID. For example, when the Home Page Scheduler wants to load the saved schedules into the
tree element that displays them to the user, it need to get access to the tree object itself. The
tree's ID is schedulesTree.

              <var schedulesTree =
document.getElementById("schedulesTree");

This returns the object representing the tree element. At this point we can get and set various
attributes related to the tree object. For example, to get the currently selected index we just
use 'schedulesTree.currentIndex'.
To see a list of attributes and methods defined for the various element types just look them
up in the XUL element reference mentioned earlier: http://xulplanet.com/references/elemref/.
You can learn all about the tree element, for example, here:
http://xulplanet.com/references/elemref/ref_tree.html.



Page 3
                      Copyright © 2005 LCC 3401 Firefox Group All rights reserved.
Firefox Extension Development Tutorial




4. Further Reading
XUL Element Reference: http://xulplanet.com/references/elemref/ref_tree.html
JavaScript Tutorials and Reference: http://www.w3schools.com/js/default.asp
A great JavaScript Tutorial for the Total Non-Programmer:
http://www.webteacher.com/javascript/




                                                                                              Page 4
                 Copyright © 2005 LCC 3401 Firefox Group All rights reserved.

Contenu connexe

Tendances

Creating a Simple PHP and MySQL-Based Login System
Creating a Simple PHP and MySQL-Based Login SystemCreating a Simple PHP and MySQL-Based Login System
Creating a Simple PHP and MySQL-Based Login System
Azharul Haque Shohan
 
Database presentation
Database presentationDatabase presentation
Database presentation
webhostingguy
 
&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />
tutorialsruby
 
mapserver_install_linux
mapserver_install_linuxmapserver_install_linux
mapserver_install_linux
tutorialsruby
 

Tendances (20)

New: Two Methods of Installing Drupal on Windows XP with XAMPP
New: Two Methods of Installing Drupal on Windows XP with XAMPPNew: Two Methods of Installing Drupal on Windows XP with XAMPP
New: Two Methods of Installing Drupal on Windows XP with XAMPP
 
Creating a Simple PHP and MySQL-Based Login System
Creating a Simple PHP and MySQL-Based Login SystemCreating a Simple PHP and MySQL-Based Login System
Creating a Simple PHP and MySQL-Based Login System
 
Sah
SahSah
Sah
 
Php MySql For Beginners
Php MySql For BeginnersPhp MySql For Beginners
Php MySql For Beginners
 
Database presentation
Database presentationDatabase presentation
Database presentation
 
Php database connectivity
Php database connectivityPhp database connectivity
Php database connectivity
 
veracruz
veracruzveracruz
veracruz
 
PHP and Mysql
PHP and MysqlPHP and Mysql
PHP and Mysql
 
&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />
 
MySQL
MySQLMySQL
MySQL
 
php $_GET / $_POST / $_SESSION
php  $_GET / $_POST / $_SESSIONphp  $_GET / $_POST / $_SESSION
php $_GET / $_POST / $_SESSION
 
Php session 3 Important topics
Php session 3 Important topicsPhp session 3 Important topics
Php session 3 Important topics
 
4.4 PHP Session
4.4 PHP Session4.4 PHP Session
4.4 PHP Session
 
lab56_db
lab56_dblab56_db
lab56_db
 
Sahu
SahuSahu
Sahu
 
Php basics
Php basicsPhp basics
Php basics
 
Moodle 3.3 - API Change Overview #mootieuk17
Moodle 3.3 - API Change Overview #mootieuk17Moodle 3.3 - API Change Overview #mootieuk17
Moodle 3.3 - API Change Overview #mootieuk17
 
Cake PHP 3 Presentaion
Cake PHP 3 PresentaionCake PHP 3 Presentaion
Cake PHP 3 Presentaion
 
Php - Getting good with session
Php - Getting good with sessionPhp - Getting good with session
Php - Getting good with session
 
mapserver_install_linux
mapserver_install_linuxmapserver_install_linux
mapserver_install_linux
 

Similaire à backend

High Performance Ajax Applications 1197671494632682 2
High Performance Ajax Applications 1197671494632682 2High Performance Ajax Applications 1197671494632682 2
High Performance Ajax Applications 1197671494632682 2
Niti Chotkaew
 
High Performance Ajax Applications
High Performance Ajax ApplicationsHigh Performance Ajax Applications
High Performance Ajax Applications
Julien Lecomte
 
Speed up your developments with Symfony2
Speed up your developments with Symfony2Speed up your developments with Symfony2
Speed up your developments with Symfony2
Hugo Hamon
 
Silicon Valley CodeCamp 2008: High performance Ajax with ExtJS and ASP.NET
Silicon Valley CodeCamp 2008: High performance Ajax with ExtJS and ASP.NETSilicon Valley CodeCamp 2008: High performance Ajax with ExtJS and ASP.NET
Silicon Valley CodeCamp 2008: High performance Ajax with ExtJS and ASP.NET
Mats Bryntse
 
JavaScript Growing Up
JavaScript Growing UpJavaScript Growing Up
JavaScript Growing Up
David Padbury
 

Similaire à backend (20)

High Performance Ajax Applications 1197671494632682 2
High Performance Ajax Applications 1197671494632682 2High Performance Ajax Applications 1197671494632682 2
High Performance Ajax Applications 1197671494632682 2
 
High Performance Ajax Applications
High Performance Ajax ApplicationsHigh Performance Ajax Applications
High Performance Ajax Applications
 
JavaScript Miller Columns
JavaScript Miller ColumnsJavaScript Miller Columns
JavaScript Miller Columns
 
eXo SEA - JavaScript Introduction Training
eXo SEA - JavaScript Introduction TrainingeXo SEA - JavaScript Introduction Training
eXo SEA - JavaScript Introduction Training
 
JavaScript Basics
JavaScript BasicsJavaScript Basics
JavaScript Basics
 
Sencha / ExtJS : Object Oriented JavaScript
Sencha / ExtJS : Object Oriented JavaScriptSencha / ExtJS : Object Oriented JavaScript
Sencha / ExtJS : Object Oriented JavaScript
 
The use of the code analysis library OpenC++: modifications, improvements, er...
The use of the code analysis library OpenC++: modifications, improvements, er...The use of the code analysis library OpenC++: modifications, improvements, er...
The use of the code analysis library OpenC++: modifications, improvements, er...
 
Spine.js
Spine.jsSpine.js
Spine.js
 
Global objects in Node.pdf
Global objects in Node.pdfGlobal objects in Node.pdf
Global objects in Node.pdf
 
Adobe Flex4
Adobe Flex4 Adobe Flex4
Adobe Flex4
 
My 70-480 HTML5 certification learning
My 70-480 HTML5 certification learningMy 70-480 HTML5 certification learning
My 70-480 HTML5 certification learning
 
The Theory Of The Dom
The Theory Of The DomThe Theory Of The Dom
The Theory Of The Dom
 
treeview
treeviewtreeview
treeview
 
treeview
treeviewtreeview
treeview
 
Speed up your developments with Symfony2
Speed up your developments with Symfony2Speed up your developments with Symfony2
Speed up your developments with Symfony2
 
Workshop 23: ReactJS, React & Redux testing
Workshop 23: ReactJS, React & Redux testingWorkshop 23: ReactJS, React & Redux testing
Workshop 23: ReactJS, React & Redux testing
 
Silicon Valley CodeCamp 2008: High performance Ajax with ExtJS and ASP.NET
Silicon Valley CodeCamp 2008: High performance Ajax with ExtJS and ASP.NETSilicon Valley CodeCamp 2008: High performance Ajax with ExtJS and ASP.NET
Silicon Valley CodeCamp 2008: High performance Ajax with ExtJS and ASP.NET
 
JavaScript Growing Up
JavaScript Growing UpJavaScript Growing Up
JavaScript Growing Up
 
Datagrids with Symfony 2, Backbone and Backgrid
Datagrids with Symfony 2, Backbone and BackgridDatagrids with Symfony 2, Backbone and Backgrid
Datagrids with Symfony 2, Backbone and Backgrid
 
Datagrids with Symfony 2, Backbone and Backgrid
Datagrids with Symfony 2, Backbone and BackgridDatagrids with Symfony 2, Backbone and Backgrid
Datagrids with Symfony 2, Backbone and Backgrid
 

Plus de tutorialsruby

&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />
tutorialsruby
 
TopStyle Help &amp; &lt;b>Tutorial&lt;/b>
TopStyle Help &amp; &lt;b>Tutorial&lt;/b>TopStyle Help &amp; &lt;b>Tutorial&lt;/b>
TopStyle Help &amp; &lt;b>Tutorial&lt;/b>
tutorialsruby
 
The Art Institute of Atlanta IMD 210 Fundamentals of Scripting &lt;b>...&lt;/b>
The Art Institute of Atlanta IMD 210 Fundamentals of Scripting &lt;b>...&lt;/b>The Art Institute of Atlanta IMD 210 Fundamentals of Scripting &lt;b>...&lt;/b>
The Art Institute of Atlanta IMD 210 Fundamentals of Scripting &lt;b>...&lt;/b>
tutorialsruby
 
&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />
tutorialsruby
 
&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />
tutorialsruby
 
Standardization and Knowledge Transfer – INS0
Standardization and Knowledge Transfer – INS0Standardization and Knowledge Transfer – INS0
Standardization and Knowledge Transfer – INS0
tutorialsruby
 
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa0602690047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
tutorialsruby
 
BloggingWithStyle_2008
BloggingWithStyle_2008BloggingWithStyle_2008
BloggingWithStyle_2008
tutorialsruby
 
BloggingWithStyle_2008
BloggingWithStyle_2008BloggingWithStyle_2008
BloggingWithStyle_2008
tutorialsruby
 
cascadingstylesheets
cascadingstylesheetscascadingstylesheets
cascadingstylesheets
tutorialsruby
 
cascadingstylesheets
cascadingstylesheetscascadingstylesheets
cascadingstylesheets
tutorialsruby
 
Winter%200405%20-%20Advanced%20Javascript
Winter%200405%20-%20Advanced%20JavascriptWinter%200405%20-%20Advanced%20Javascript
Winter%200405%20-%20Advanced%20Javascript
tutorialsruby
 

Plus de tutorialsruby (20)

&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />
 
TopStyle Help &amp; &lt;b>Tutorial&lt;/b>
TopStyle Help &amp; &lt;b>Tutorial&lt;/b>TopStyle Help &amp; &lt;b>Tutorial&lt;/b>
TopStyle Help &amp; &lt;b>Tutorial&lt;/b>
 
The Art Institute of Atlanta IMD 210 Fundamentals of Scripting &lt;b>...&lt;/b>
The Art Institute of Atlanta IMD 210 Fundamentals of Scripting &lt;b>...&lt;/b>The Art Institute of Atlanta IMD 210 Fundamentals of Scripting &lt;b>...&lt;/b>
The Art Institute of Atlanta IMD 210 Fundamentals of Scripting &lt;b>...&lt;/b>
 
&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />
 
&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />
 
Standardization and Knowledge Transfer – INS0
Standardization and Knowledge Transfer – INS0Standardization and Knowledge Transfer – INS0
Standardization and Knowledge Transfer – INS0
 
xhtml_basics
xhtml_basicsxhtml_basics
xhtml_basics
 
xhtml_basics
xhtml_basicsxhtml_basics
xhtml_basics
 
xhtml-documentation
xhtml-documentationxhtml-documentation
xhtml-documentation
 
xhtml-documentation
xhtml-documentationxhtml-documentation
xhtml-documentation
 
CSS
CSSCSS
CSS
 
CSS
CSSCSS
CSS
 
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa0602690047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
 
HowTo_CSS
HowTo_CSSHowTo_CSS
HowTo_CSS
 
HowTo_CSS
HowTo_CSSHowTo_CSS
HowTo_CSS
 
BloggingWithStyle_2008
BloggingWithStyle_2008BloggingWithStyle_2008
BloggingWithStyle_2008
 
BloggingWithStyle_2008
BloggingWithStyle_2008BloggingWithStyle_2008
BloggingWithStyle_2008
 
cascadingstylesheets
cascadingstylesheetscascadingstylesheets
cascadingstylesheets
 
cascadingstylesheets
cascadingstylesheetscascadingstylesheets
cascadingstylesheets
 
Winter%200405%20-%20Advanced%20Javascript
Winter%200405%20-%20Advanced%20JavascriptWinter%200405%20-%20Advanced%20Javascript
Winter%200405%20-%20Advanced%20Javascript
 

Dernier

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Dernier (20)

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
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024
 
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)
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
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
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 

backend

  • 1. Firefox Extension Development Tutorial Table of contents 1 Overview............................................................................................................................2 2 JavaScript Basics................................................................................................................2 3 Working with the DOM..................................................................................................... 3 4 Further Reading..................................................................................................................4 Copyright © 2005 LCC 3401 Firefox Group All rights reserved.
  • 2. Firefox Extension Development Tutorial 1. Overview All of the guts of your extension will be written in JavaScript. If you already know JavaScript than you are good to go! If you are new to JavaScript it is highly recommended that you read through the next section and the supporting documentation to get used to the syntax. All of your JavaScript code files should go in the content directory of your extension. This is where the XUL files go as well, so they will be able to easily reference the code. Just insert the following line in an XUL file that needs to run functions from your files... <script type="application/x-javascript" src="chrome://hpsched/content/mycode.js" /> Make sure you use the chrome URI so that Firefox can find the file correctly. 2. JavaScript Basics JavaScript is a fairly straight forward scripting language. Let's jump right into some sample code: /* * Save the Schedules List to the "extensions.hpsched.schedules" preference. * This is called by the pref's system when the GUI element is altered. */ function saveSchedulesList() { var schedulesList = document.getElementById("schedulesList").childNodes; var prefString = ""; for (var i=0; i < schedulesList.length; i++) { var columns = schedulesList[i].childNodes[0].childNodes; var str = columns[0].getAttribute("label") + ".ITEM." + columns[1].getAttribute("label") + ".ITEM." + columns[2].getAttribute("label") + ".ITEM." + columns[3].getAttribute("label"); if(prefString == "") prefString = str; else prefString += ".NEXT." + str; } /* return the new prefString to be stored by pref system */ return prefString; } Page 2 Copyright © 2005 LCC 3401 Firefox Group All rights reserved.
  • 3. Firefox Extension Development Tutorial The first line defines a function named 'saveSchedulesList' that brings in zero arguments. The next two lines create and initialize two variables. Notice that JavaScript is a typeless language, just type var to let the parser know you are declaring a variable. The for-loop and if statement looks just like what you would expect in C or Java. You will also notice that you do not have to state that this function is (or is not) returning a value. If you would like to return a value, just use the common 'return x;' statement. If no return statement is provided than the function returns (void), which is ignored by the calling function. Debugging JavaScript is also quite easy: just use the 'alert()' function call. Whenever Firefox is running code and sees a call such as 'alert("I am here!");', Firefox will display a graphical error box with the given message. This is a wonderful way to determine if a certain area of code is being executed, what the current value of some variable is (i.e. alert(myVar)), and more. Use alert(), it is your friend! Please see the Further Reading section for links to some wonderful JavaScript tutorials. 3. Working with the DOM JavaScript is very common in web development because it provides functionality to easily work with the DOM (Document Object Module). All HTML files on the net are rendered by the browser by first building a tree. Each node in the tree corresponds to a tag in the HTML file. Firefox's XUL files work the same way, all elements are nodes in a large tree (per file). The top level element is called the 'document'. There is one very important JavaScript function that you will need to know to be able to work with the XUL elements in your GUI. It allows you to access the object itself, using its ID. For example, when the Home Page Scheduler wants to load the saved schedules into the tree element that displays them to the user, it need to get access to the tree object itself. The tree's ID is schedulesTree. <var schedulesTree = document.getElementById("schedulesTree"); This returns the object representing the tree element. At this point we can get and set various attributes related to the tree object. For example, to get the currently selected index we just use 'schedulesTree.currentIndex'. To see a list of attributes and methods defined for the various element types just look them up in the XUL element reference mentioned earlier: http://xulplanet.com/references/elemref/. You can learn all about the tree element, for example, here: http://xulplanet.com/references/elemref/ref_tree.html. Page 3 Copyright © 2005 LCC 3401 Firefox Group All rights reserved.
  • 4. Firefox Extension Development Tutorial 4. Further Reading XUL Element Reference: http://xulplanet.com/references/elemref/ref_tree.html JavaScript Tutorials and Reference: http://www.w3schools.com/js/default.asp A great JavaScript Tutorial for the Total Non-Programmer: http://www.webteacher.com/javascript/ Page 4 Copyright © 2005 LCC 3401 Firefox Group All rights reserved.