windingwind / zotero-actions-tags

Customize your Zotero workflow.
GNU Affero General Public License v3.0
1.83k stars 48 forks source link

[Bug] The same script code can work in 'run javascript', but doesn't work ideally in zotero action tag plugin #394

Closed xjtusjtu closed 2 weeks ago

xjtusjtu commented 2 weeks ago

Is there an existing issue for this?

Are you using the latest Zotero and the latest plugin?

Environment

Describe the bug

My same code - inserting the specified content at the current note cursor - executes fine in run javascript (inserts the specified content once), but the script wrapped in the zotero action tags plugin, which executes (using shortcuts) inserts it twice. Could this be due to the better notes plugin, or the action tags plugin? (I'd like to raise an issue, but I don't know where to do it)

let localeditor;
switch (Zotero_Tabs.selectedType) {
    case "reader":
        localeditor = ZoteroContextPane.activeEditor;
        break;
    case "library":
        localeditor = ZoteroPane.itemPane._noteEditor;
        break;
    case "note":
        localeditor = Zotero.BetterNotes?.api.workspace
            .getWorkspaceByTabID()
            ?.querySelector("#BetterNotes-editor-main");
        break;
}
console.log(localeditor);
console.log(localeditor._item);
console.log(localeditor._item.isNote());
let currentItem = localeditor._item;
let noteID = currentItem.id; // 获取笔记的note ID
let noteContent = currentItem.getNote(); // 获取笔记的内容
const localeditor2 = Zotero.BetterNotes.api.editor.getEditorInstance(currentItem.id);

Zotero.BetterNotes.api.editor.insert(
    localeditor2,
    content = "xxxx",
    position = "cursor"
);

return `Note ID: ${noteID}, Note Content: ${noteContent}`;

image

Debug Output

Submit…

Clear

Submitted with Debug ID 📋

1727587436070 addons.webextension.jasminum@linxzh.com WARN Loading extension 'jasminum@linxzh.com': Reading manifest: Warning processing version: version must be a version string consisting of at most 4 integers of at most 9 digits without leading zeros, and separated with dots

1727587438214 addons.xpi-utils WARN disabling legacy extension zoteroupdateifs@redleafnew.me

1727587438214 addons.xpi-utils WARN disabling legacy extension zoteroif@qnscholar

[JavaScript Error: "uncaught exception: undefined"]

[JavaScript Error: "uncaught exception: undefined"]

[JavaScript Error: "uncaught exception: undefined"]

[JavaScript Error: "uncaught exception: undefined"]

[JavaScript Error: "uncaught exception: undefined"]

[JavaScript Error: "uncaught exception: undefined"]

[JavaScript Error: "uncaught exception: undefined"]

[JavaScript Error: "uncaught exception: undefined"]

[JavaScript Error: "uncaught exception: undefined"]

[JavaScript Error: "uncaught exception: undefined"]

[JavaScript Error: "TypeError: emptyListMessage is null" {file: "chrome://zotero/content/standalone/standalone.js" line: 687}] ZoteroStandalone</this.updateAddonsPane/<@chrome://zotero/content/standalone/standalone.js:687:7

[JavaScript Error: "Passing a URI to Zotero.File.getContentsAsync() is deprecated -- use Zotero.HTTP.request() instead"]

[JavaScript Error: "Passing a URI to Zotero.File.getContentsAsync() is deprecated -- use Zotero.HTTP.request() instead"]

appName => Zotero, version => 7.0.5 (x64), os => Windows 10.0 19045, locale => en-US, extensions => Zotero Debug Bridge (53.0, extension), Jasminum (1.0.0-16, extension), Zotero Attanger (1.2.6, extension), Translate for Zotero (2.0.1, extension), Better Notes for Zotero (2.0.8, extension), Actions and Tags for Zotero (2.0.2, extension), Magic for Zotero (1.8.3, extension), Ethereal Reference (1.1.3, extension), Awesome GPT (1.2.6, extension), Ethereal Style (5.0.3, extension), Linter for Zotero (1.18.7, extension), Zotero Update IFs (0.95.6, extension, disabled), Zotero IF Pro Max (4.7.3, extension, disabled), Easier Citation (0.4.0, extension, disabled), Wandering File (0.2.8, extension, disabled), Tara (1.0.0-8, extension, disabled), MarkDB-Connect (0.1.1, extension, disabled), Del Item With Attachment (0.3.3, extension, disabled), Better BibTex for Zotero (6.7.238, extension, disabled), PDF Figure (0.2.4, extension, disabled)

Anything else?

No response

xjtusjtu commented 2 weeks ago

经尝试,需要把代码放在 if (item) {...} 当中,但是仍然不清楚为什么不放在这里面就会重复执行。。。

windingwind commented 2 weeks ago

When selecting multiple items in the library, the action will be triggered once for all items (items=[...], item=undefined) and then one by one for each item (items=[], item=...). You can use the items variable to get the selected items array and avoid duplicate executions.

请仔细阅读文档

xjtusjtu commented 2 weeks ago

感谢!