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

  1. Greg | November 19, 2008 at 7:52 pm | Permalink

    Hi,
    these posts are really interesting.
    I am trying to do the following:
    I want a script which pulls out information about the selected item in zotero, uses this information to rename a pdf file an my computer, and finally add a linked attachment to the zotero item referring to this renamed file.
    Can you help me with the part which involves Zotero? I have basically two questions;
    1) How can I define variables for the last name of the first three creators and the year of the zotero item?
    I learned from your post how to get the selected item as an object:
    var selectedItems = ZoteroPane.getSelectedItems();
    I also figured out how to get the different creators as an object:
    var author = selectedItems[0].getCreator(1);
    I do not, however, know how to get the last name. I was also unable to get the year of the publication. Can you help me about this?
    2) The second thing I am not sure about is how to add an attachment to an specific item given that I have a string variable which contains the location of the file.

    Thanks!!!

  2. yee | November 20, 2008 at 9:31 am | Permalink

    Hi Greg,

    I'll write a more thorough answer later, but try the following:

    // get Zotero, ZoteroPane
    var Zotero = chromeWindow.Zotero;
    var ZoteroPane = chromeWindow.ZoteroPane;

    //get first selected item and output its title
    var items = ZoteroPane.getSelectedItems();
    var item = items[0];
    output ('title: ' + item.getField('title'));

    // creators — get the first creator's last name
    var creators = item.getCreators();
    creators[0].lastName;

    // date

    var date = item.getField('date');
    date;

    // attach file
    // see http://groups.google.com/group/zotero-dev/browse_thread/thread/319c69f7882fe171#

    var fileName = 'D:\\Document\\Zotero\\chickenfoot\\greg.answers.js'
    var file = Components.classes["@mozilla.org/file/local;1"].
    createInstance(Components.interfaces.nsILocalFile);
    file.initWithPath(fileName);

    Zotero.Attachments.linkFromFile(file, item.id);

  3. Greg | November 20, 2008 at 3:37 pm | Permalink

    Hi,

    thanks a lot for your help!
    I have already made a lot of progress. Title and authors work well. I also managed to truncate the title after certain chars and select only the authors (not the editors).
    I still have, however, some problems with the date. I do get it but it is formatted in very different ways. Sometimes it is just the year (which I want), sometime it is something like "2007-05-19", and sometimes it is "October 2007". Do you know whether it is possible to get only the year? I guess I would be able to write some code which allows me to extract the year from the different formats. But this is not very elegant and might cause problems if the date is formatted in a unexpected way.

    Thanks again,
    Greg

    ps: I will try to add an attachment later when I have done the other stuff…

  4. exvion | September 17, 2009 at 10:09 am | Permalink

    Hi.
    I try last script. In Firefox 3.5 it work incorrect. Items add in root My library but in collection "Saved from tabs".

    How i repair it?

  5. Raymond Yee | September 17, 2009 at 10:18 am | Permalink

    Unfortunately, the script never worked completely for me — sometimes it worked and sometimes it didn't — and I never got to the bottom of it. If I figure out how to get this script to work reliably, I'll post here.

Post a Comment

You must be logged in to post a comment.