Skip to content

adding all Firefox tabs to Zotero using Chickenfoot

We can now build on what was presented in the previous blog post: Adding items to Zotero with Chickenfoot.

The end goal is to loop through all the tabs and add Each of them as items into Zotero. So we need to figure out how to access the tabs. I would do so here by presenting a series of code fragments that you can try out using Chickenfoot and Zotero.

But let's first work through some code to manipulate tabs in Firefox, drawn from two reference documents:

  • tabbrowser – MDC: a reference to the tabbrowser object
  • Tabbed browser – MDC has some nice sample code, which seems to work best in Firefox 3.x, — something seemed to fail in Firefox 2.x (I should put an example here)

For example, one example of code doesn't work in 2.x — using addTab:

var tabBrowser = getTabBrowser(chromeWindow);
tabBrowser.addTab("http://yahoo.com");

Since I've had some problems using Firefox 2.x, I recommend running the following code fragments in Firefox 3.x.

First, let's get the tabbrowser object:

var tabBrowser = getTabBrowser(chromeWindow);
tabBrowser;

You can show the URL of the currently selected tab:

var tabBrowser = getTabBrowser(chromeWindow);
tabBrowser.selectedBrowser.contentWindow.location;

The following code advances selects the tab to the right of the currently tab — and loops around if needed:

var tabBrowser = getTabBrowser(chromeWindow);
tabBrowser.mTabContainer.advanceSelectedTab(1, true);

We can write out the current number of open tabs:

var tabBrowser = getTabBrowser(chromeWindow);
output(tabBrowser.browsers.length);

Here's code to select each tab in turn and output its location:

var tabBrowser = getTabBrowser(chromeWindow);
output(tabBrowser.browsers.length);
for (var i=0; i < tabBrowser.browsers.length; i++) {
  tabBrowser.mTabContainer.advanceSelectedTab(1, true);
  output(tabBrowser.selectedBrowser.contentWindow.location);
}

how to create a new collection in Zotero:

var Zotero = chromeWindow.Zotero;
var new_Collection = Zotero.Collections.add("testing");
new_Collection;

Here's code to create a new collection and add an item to it. (In my tries, the code crashes in FF 2.0.x but seems to work in FF 3.0.3):

var Zotero = chromeWindow.Zotero;
var ZoteroPane = chromeWindow.ZoteroPane;

var new_Collection = Zotero.Collections.add("testing");
var new_item = ZoteroPane.addItemFromPage(new_Collection.id);

Here's code that pulls it altogether — it loops through the tabs and adds the document of each tab to Zotero in a new collection:

var Zotero = chromeWindow.Zotero;
var ZoteroPane = chromeWindow.ZoteroPane;
var tabBrowser = getTabBrowser(chromeWindow);

var new_Collection = Zotero.Collections.add("Saved from tabs");
output(tabBrowser.browsers.length);

for (var i=0; i < tabBrowser.browsers.length; i++) {
  tabBrowser.mTabContainer.advanceSelectedTab(1, true);
  output(tabBrowser.selectedBrowser.contentWindow.location);
  var new_item = ZoteroPane.addItemFromPage(new_Collection.id);
}

{ 5 } Comments