Plugin preference pane ReferenceError: pref is not defined

119 views
Skip to first unread message

Annabelle Wiegart

unread,
Nov 30, 2023, 4:24:39 AM11/30/23
to zotero-dev
Hi,
I'm trying to set up a preference pane for my Zotero 7 plugin. I followed the example on https://www.zotero.org/support/dev/zotero_7_for_developers. Here are the relevant code lines of the bootstrap startup function:

        // register preference pane
        Zotero.PreferencePanes.register({
            pluginID: 'uzb-holdi...@zb.uzh.ch',
            src: rootURI + 'prefs.xhtml',
            scripts: [rootURI + 'prefs.js'],
            // stylesheets: ['prefs.css'],
        });

And here my prefs.xhtml:

<vbox onload="uzb-holding-checker_Preferences.init()">
    <groupbox>
        <label><html:h2>Alma SRU Endpoint</html:h2></label>
        <html:input type="text" preference="extensions.zotero.uzb-holding-checker.almaSruEndpoint"/>
    </groupbox>
    <groupbox>
        <label><html:h2>Alma SRU Version</html:h2></label>
        <html:input type="text" preference="extensions.zotero.uzb-holding-checker.almaSruVersion"/>
    </groupbox>
</vbox>

The preferences tab is added, but its content is not displayed. I'm getting this error message:
JavaScript Error: "ReferenceError: pref is not defined" {file: "jar:file:///C:/Users/wieg/AppData/Roaming/Zotero/Zotero/Profiles/rgc2u01v.default/extensions/uzb-holdi...@zb.uzh.ch.xpi!/prefs.js" line: 1

What am I doing wrong? How can I fix this issue?


Thanks a lot in advance for your help.

XY Wong

unread,
Nov 30, 2023, 4:32:50 AM11/30/23
to zotero-dev
Please do not manually load or add non `pref(...)` calls to the `$rootURI/prefs.js` on Zotero 7. It's automatically executed by Zotero on plugin load to set the default pref key-values.

1. You should put the `uzb-holding-checker_Preferences.init` definition somewhere else. Not the `prefs.js`.
2. You should not load the `rootURI + 'prefs.js'` here (or anywhere).

BTW, the naming of variables is not correct in JavaScript. `uzb-holding-checker_Preferences` is not a valid JS variable name. (Your IDE should be showing warning or errors).

XY Wong

unread,
Nov 30, 2023, 4:41:13 AM11/30/23
to zotero-dev
(Just looked into your code)

Your `prefs.js` is OK, ignore suggestion 1;

I don't find where `uzb-holding-checker_Preferences` is, but you'll need to load the script where it's into the preference window. The main window and prefs window are in two different scopes.

Annabelle Wiegart

unread,
Nov 30, 2023, 4:52:46 AM11/30/23
to zotero-dev
Thank you for your quick reply! Okay, I erroneously assumed that  `uzb-holding-checker_Preferences.init()` was some kind of Zotero-internal method to access my plugin's preferences. By removing the onload attribute and commenting out the reference to prefs.js, I get the the two input boxes displayed in the preference pane:

        // register preference pane
        Zotero.PreferencePanes.register({
            pluginID: 'uzb-holdi...@zb.uzh.ch',
            src: rootURI + 'prefs.xhtml',
            // scripts: [rootURI + 'preferences.js'],
            // stylesheets: ['prefs.css'],
        });
(bootstrap.js)

<vbox>
    <groupbox>
        <label><html:h2>Alma SRU Endpoint</html:h2></label>
        <html:input type="text" preference="extensions.zotero.uzb-holding-checker.almaSruEndpoint"/>
    </groupbox>
    <groupbox>
        <label><html:h2>Alma SRU Version</html:h2></label>
        <html:input type="text" preference="extensions.zotero.uzb-holding-checker.almaSruVersion"/>
    </groupbox>
</vbox>
(prefs.xhtml)

Now, I guess I need a .js script that is executed onload and that saves the user input to the preferences?

Annabelle Wiegart

unread,
Nov 30, 2023, 4:58:05 AM11/30/23
to zotero-dev
In general, it would be very helpful to have a complete in Make it Red. In the bootstrap startup function, a preference pane is registered, but preferences.js (and preferences.xhtml) is not present in the repository: https://github.com/zotero/make-it-red/blob/5a7ee1be2f147a327220c1e5a4129d6c6169999c/src-2.0/bootstrap.js. The rest of Make it Red was very helpful!

XY Wong

unread,
Nov 30, 2023, 5:03:16 AM11/30/23
to zotero-dev
> I guess I need a .js script that is executed onload

Yes.

> and that saves the user input to the preferences?

No. The `preference` attribute will automatically be binded to pref values. They are auto-save.

Sorry, but I guess the make-it-red is not designed to cover every corner of plugin development. It's a clean and good start point, but for more complex usage, you may want to see other plugins' implementation for reference.

Reply all
Reply to author
Forward
0 new messages