Zotero_Tabs/Reader JS script strange behavior

72 views
Skip to first unread message

XY Wong

unread,
Mar 30, 2022, 11:49:28 PM3/30/22
to zotero-dev
Hi, I'm the developer of the PDF Translate add-on.

Some add-on users report an error of right sidebar loading failure. I found it's caused by a strange behavior of js script. Here are two functions of my add-on:

getReader: function () {
return Zotero.Reader.getByTabID(Zotero_Tabs.selectedID);
},
getReaderID: function () {
for (let i = 0; i < Zotero.Reader._readers.length; i++) {
if (Zotero.Reader._readers[i].tabID === Zotero_Tabs.selectedID) {
return i;
}
}
},

They work fine, while in very accident case they behave strange. Some tests in the Run JavaScript Window when these behavior happens:

1. call getReader. the function definition is shown correctly.
2. call getReader(). return undefined(should return a reader object)
3. call Zotero.Reader.getByTabID(Zotero_Tabs.selectedID). return an valid reader object.
4. getReader = function(){return Zotero.Reader.getByTabID(Zotero_Tabs.selectedID);}, then call getReader(). return an valid reader object.

5. call getReaderID. the function definition is shown correctly.
6. call getReaderID(). return undefined(should return a reader index)
7. call the for loop directly. return a valid reader index 1.

8. call Zotero_Tabs.selectedID. return a valid tab id 'tab-Xt9g83mm'

The debug ID is 1978557902.

I think this may be caused by some inside bug of Zotero tabs/reader. Could you please help?

Thanks very much!

Martynas Bagdonas

unread,
Mar 31, 2022, 2:38:54 AM3/31/22
to zotero-dev
Zotero.Reader.getByTabID(Zotero_Tabs.selectedID) can return undefined if the currently selected tab is a library tab. Library tab id is always "zotero-pane" and Zotero_Tabs.selectedIndex is 0.

But keep in mind that tabs functionality is evolving rapidly and this can break at any time.

XY Wong

unread,
Mar 31, 2022, 9:53:20 PM3/31/22
to zotero-dev
Hi,  thanks!

What's really frustrating is the different output of calling function and running code directly, while the tabs selection is not changed, as the 2&3 and 7&8 shown above.

This only happens very accidently. I only encountered once and don't know how to recurrence the problem.

XY Wong

unread,
Apr 9, 2022, 11:51:30 PM4/9/22
to zotero-dev
I figured out why!

I compare the item field 'title' with the reader's '_title'.split(' - '). When the item title includes a ' - ', the wrong split causes this bug. So this is not a Zotero bug. Thanks!

Wishing to see the new APIs of Zotero Reader soon~
Reply all
Reply to author
Forward
0 new messages