Zotero plugin only in one window

33 views
Skip to first unread message

psimon

unread,
Dec 29, 2009, 4:42:32 AM12/29/09
to zotero-dev
Hello,
I am working on LyX plugin for Zotero (http://forums.zotero.org/
discussion/10406/lyz-zotero-and-lyx-integration) and I am fairly new
to javascript or mozilla.
My problem is that the plugin only works in one window. When I open
new Firefox window, the plugin does not appear. Could you give me a
hint what am I doing wrong?
Many thanks
Petr

lyz.js -------------------------------------------------------------
Zotero.Lyz = {
prefs: null,
DB: null,
createUI: function() {
var parentn = document.getElementById("zotero-items-
pane").firstChild;
var lyzb = document.createElement("toolbarbutton");
lyzb.setAttribute("id", "lyz-menu-button");
var siblingn = document.getElementById("zotero-tb-advanced-search");
parentn.insertBefore(lyzb, siblingn);
parentn.insertBefore(document.createElement
("toolbarseparator"),siblingn);
document.loadOverlay("chrome://lyz/content/lyz-menu.xul", null);
},

init: function () {
this.DB = new Zotero.DBConnection("lyz");
var sql;
if (!this.DB.tableExists('docs')) {
sql = "CREATE TABLE docs (id INTEGER PRIMARY KEY, doc TEXT, bib
TEXT)"
this.DB.query(sql);
sql = "CREATE TABLE keys (id INTEGER PRIMARY KEY, key TEXT, bib
TEXT, zid TEXT)"
this.DB.query(sql);
}
//set up preferences
this.prefs = Components.classes["@mozilla.org/preferences-service;
1"].
getService(Components.interfaces.nsIPrefService);
this.prefs = this.prefs.getBranch("extensions.lyz.");

},
// the rest of functions
}
window.addEventListener('load', function(e) { Zotero.Lyz.createUI
(); }, false);
--- end of lyz.js
-----------------------------------------------------

include.js
-------------------------------------------------------------
if (!Zotero.Lyz) {
const lyzLoader = Components.classes["@mozilla.org/moz/jssubscript-
loader;1"]
.getService(Components.interfaces.mozIJSSubScriptLoader);
lyzLoader.loadSubScript("chrome://lyz/content/lyz.js");
Zotero.Lyz.init();
}
--- end of include.js
-----------------------------------------------------

Dan Stillman

unread,
Dec 29, 2009, 5:15:13 AM12/29/09
to zoter...@googlegroups.com
On 12/29/09 4:42 AM, psimon wrote:
> I am working on LyX plugin for Zotero (http://forums.zotero.org/
> discussion/10406/lyz-zotero-and-lyx-integration) and I am fairly new
> to javascript or mozilla.
> My problem is that the plugin only works in one window. When I open
> new Firefox window, the plugin does not appear. Could you give me a
> hint what am I doing wrong?
>

Your createUI() method is only being called (via the chrome window load
handler) in the first window, since lyz.js is only executed once, when
Zotero.Lyz doesn't exist. Since lyz.js is only creating a core object
and doesn't (as far as I can tell) have any window-specific properties,
loading it only once is good, but you still need the load handler in
every window. It looks like you could move the load handler to
include.js, below the conditional, to achieve that.

psimon

unread,
Dec 29, 2009, 6:26:50 AM12/29/09
to zotero-dev

when I put the loader in include.js like this:

if (!Zotero.Lyz) {
const lyzLoader = Components.classes["@mozilla.org/moz/jssubscript-
loader;1"]
.getService(Components.interfaces.mozIJSSubScriptLoader);
lyzLoader.loadSubScript("chrome://lyz/content/lyz.js");
Zotero.Lyz.init();
}

window.addEventListener('load', function(e) { Zotero.Lyz.createUI
(); }, false);

The button gets created (but no menu) and it's inserted into the first
window.

Message has been deleted
Message has been deleted

Dan Stillman

unread,
Dec 29, 2009, 7:48:59 PM12/29/09
to zoter...@googlegroups.com

The problem is that you're keeping central state (which most Firefox
extensions don't do, but you're piggybacking on Zotero's core object)
but trying to do window-specific things. If you're storing things in a
core object, you need to pass the relevant window/document to the
functions. So pass document to createUI() and pass window to settings()
(and do the same for anything else that needs a window) and change the
methods to take those parameters, and then it should work.

(No need in this case, but an alternative for things like opening
dialogs is to use nsIWindowMediator to get the most recent browser window.)

psimon

unread,
Dec 29, 2009, 8:34:54 PM12/29/09
to zotero-dev

Thanks. I'll look into it. Basically I have started by dissecting
Lytero plugin and I didn't pay much attention to what I was doing.

psimon

unread,
Dec 29, 2009, 9:46:35 PM12/29/09
to zotero-dev
I have just tried, but since I keep the central state I would need to
pass to window all around to all dialogs and something actually
introduces new bugs, e.g. I can't close sidebar and who knows what
else.
I will keep the plugin limited to single window, at least for the
moment, I doubt there is a reason to use two instances of Zotero at
the same time when writing.
Btw. is there another way how to insert my button into zotero menu? I
have tried in chrome.manifest, but it didn't work.
Many thanks

Dan Stillman

unread,
Dec 29, 2009, 10:11:41 PM12/29/09
to zoter...@googlegroups.com
On 12/29/09 9:46 PM, psimon wrote:
> I have just tried, but since I keep the central state I would need to
> pass to window all around to all dialogs and something actually
> introduces new bugs, e.g. I can't close sidebar and who knows what
> else.
>

You don't need to pass anything around for dialogs. You can just use
nsIWindowMediator:

var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
.getService(Components.interfaces.nsIWindowMediator);
var win = wm.getMostRecentWindow("navigator:browser");

> I will keep the plugin limited to single window, at least for the
> moment, I doubt there is a reason to use two instances of Zotero at
> the same time when writing.
>

Up to you, of course, but people frequently open more than one Firefox
window.

> Btw. is there another way how to insert my button into zotero menu? I
> have tried in chrome.manifest, but it didn't work.
>

You should be able to use a single overlay (rather than two) registered
via chrome.manifest. I don't have time to help with this, but see
https://www.zotero.org/trac/browser/plugins/helloworld/trunk/chrome/content/helloworldzotero/overlay.xul
from the sample plugin for an example.

https://developer.mozilla.org/en/XUL/Attribute/insertbefore

psimon

unread,
Dec 30, 2009, 1:28:14 AM12/30/09
to zotero-dev
Dan, thanks a lot for you help. It's exactly the limited time that
makes me ask sooner than I should.
Reply all
Reply to author
Forward
0 new messages