FUEL/0.2/Plan: Difference between revisions

Added bookmarks examples, fixed up the browser examples, removed the database examples.
No edit summary
(Added bookmarks examples, fixed up the browser examples, removed the database examples.)
 
Line 4: Line 4:


=== Browser ===
=== Browser ===
* Wrapper around tabbed browser "gBrowser" - Browser
* Wrapper around browser - BrowserTab
** Browser holds a collection of BrowserTab
* Access browser tabs
* Access browser tabs
* Access web content in a tab
* Access web content in a tab
* Handle tab-related events


'''Example:'''
'''Example:'''


Open, and activate, a new tab
Open, and activate, a new tab
   App.browser.open("http://google.com/").active = true;
   Application.browser.open("http://google.com/").active = true;


Open, and activate, a tab just after the current tab
Open, and activate, a tab just after the current tab
   var tab = App.browser.open("http://google.com/");
   var tab = Application.browser.open("http://google.com/");
   App.browser.insertBefore( tab, App.browser.activeTab.next );
   Application.browser.insertBefore( tab, Application.browser.activeTab.next );
   tab.active = true;
   tab.active = true;


or:
or:
   var tab = App.browser.open("http://google.com/");
   var tab = Application.browser.open("http://google.com/");
   tab.index = App.browser.activeTab + 1;
   tab.index = Application.browser.activeTab + 1;
   tab.active = true;
   tab.active = true;


Move the active tab to be the first tab
Move the active tab to be the first tab
   App.browser.insertBefore( App.browser.activeTab, App.browser.tabs[0] );
   Application.browser.insertBefore(
    Application.browser.activeTab,
    Application.browser.tabs[0]
  );


or:
or:
   App.browser.activeTab.index = 0;
   Application.browser.activeTab.index = 0;


Close the active tab
Close the active tab
   App.browser.activeTab.close();
   Application.browser.activeTab.close();


Do something when the active tab loads
Do something when the active tab loads
   App.browser.activeTab.events.addListener( "load", function(){
   Application.browser.activeTab.events.addListener( "load", function(){
     this.query("#foo div")
     this.query("#foo div")
   });
   });


Change the URL in the active tab  
Change the URL in the active tab  
   App.browser.activeTab.url = "http://mozilla.org/";
   Application.browser.activeTab.url = "http://mozilla.org/";


Close all Google-related tabs
Close all Google-related tabs
   App.browser.tabs.forEach(function(tab){
   Application.browser.tabs.forEach(function(tab){
     if ( tab.url.match(/google/) )
     if ( tab.url.match(/google/) )
       tab.remove();
       tab.remove();
Line 50: Line 51:
Re-use a tab, or open a new one
Re-use a tab, or open a new one
   var url = "http://google.com/";
   var url = "http://google.com/";
   App.browser.tabs.some(function(tab){
   Application.browser.tabs.some(function(tab){
     if ( tab.url == url )
     if ( tab.url == url )
       return tab.active = true;
       return tab.active = true;
   }) || App.browser.open( url ).active = true;
   }) || Application.browser.open( url ).active = true;


Stop the user from opening any new tabs
Stop the user from opening any new tabs
   App.browser.events.addListener( "TabOpen", function(e){
   Application.browser.events.addListener( "TabOpen", function(e){
     e.target.close();
     e.target.close();
   });
   });
Line 66: Line 67:
'''Example:'''
'''Example:'''


   var b = Application.bookmarks.all;
Log the title of all top-level bookmarks:
 
   Application.bookmarks.all.forEach(function(cur){
  while ( b.hasNext() ) {
  var cur = b.next();
   console.log( "title", cur.title );
   console.log( "title", cur.title );
  }
 
  Bookmarks.add({
    title: "Foo Bar",
    url: "http://mozilla.org/"
   });
   });


=== Database (SQLite) ===
Add a new bookmark:
* Attach to an existing SQLite DB
  Application.bookmarks.add("Mozilla", "http://mozilla.org/");
* Create DB
* Perform a basic query
* Get JS-object results back


'''Example:'''
Remove all bookmarks that point to Google:
  Application.bookmarks.all.forEach(function(cur){
    if ( cur.url.match(/google.com/) )
      cur.remove();
  });


   var db = new Database("myfile.db");
Add a new bookmark, if one doesn't already exist:
    
   var url = "http://google.com/";
  var q = db.prepare("SELECT * FROM foo WHERE foo = ?;");
   Application.bookmarks.all.some(function(cur){
  q.execute( "bar" );
     if ( cur.url == url )
 
      return true;
  while ( q.hasNext() ) {
  }) || Application.bookmarks.add( "Google", url );
     var row = q.next();
    console.log( "foo", row.foo );
    console.log( "username", row.username );
  }
132

edits