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
-----------------------------------------------------
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.
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.
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.)
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.
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.