windingwind / zotero-actions-tags

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

[Bug] Script Error - redeclaration of let linkTextField #367

Closed richardkaplan closed 1 month ago

richardkaplan commented 1 month ago

Is there an existing issue for this?

Are you using the latest Zotero and the latest plugin?

Environment

Describe the bug

I have created an a Copy Item Link action bsed upon the source here updated 1/22/24: https://github.com/windingwind/zotero-actions-tags/discussions/115

I highlight an item and select Tools - Trigger Action - Copy Item Link

I get this error message:

Script Error:redeclaration of let linkTextField

Debug Output

(3)(+0000006): [Actions and Tags for Zotero] Script Error {}

(4)(+0000000): Beginning DB transaction YWFTpdx6

(4)(+0000005): Item 13503 has not changed

(4)(+0000001): Committed DB transaction YWFTpdx6

(3)(+0000000): [Actions and Tags for Zotero] applyAction {"event":0,"operation":4,"data":"// @author windingwind, garth74\n// @link https://github.com/windingwind/zotero-actions-tags/discussions/115\n// @usage Select an item in the library and press the assigned shortcut keys\n// @update Mon, 22 Jan 2024 00:10:18 GMT (by new Date().toGMTString())\n\n// EDIT THESE SETTINGS\n\n/ @type {string} Name of the field to use as the link text. To use the citation key, set this to \"citationKey\". */\nlet linkTextField = \"title\";\n\n/* @type {'html' | 'md' | 'plain'} What type of link to create. /\nlet linkType = \"html\";\n\n/ @type {boolean} If true, make the link specific to the currently selected collection. */\nlet useColl = false;\n\n/ @type {boolean} If true, use Better Notes zotero://note link when the selected item is a note. */\nlet useNoteLink = false;\n\n/* @type {'select' | 'open-pdf' | 'auto'} Action of link/\nlet linkAction = \"auto\"; // auto = open-pdf for PDFs and annotations, select for everything else\n\n// END OF EDITABLE SETTINGS\n\n// For efficiency, only execute once for all selected items\nif (item) return;\nitem = items[0];\nif (!item && !collection) return \"[Copy Zotero Link] item is empty\";\n\nif (collection) {\n linkAction = \"select\";\n useColl = true;\n}\n\nif (linkAction === \"auto\") {\n if (item.isPDFAttachment() || item.isAnnotation()) {\n linkAction = \"open-pdf\";\n } else {\n linkAction = \"select\";\n }\n}\n\nconst uriParts = [];\nlet uriParams = \"\";\n\nlet targetItem = item;\nif (linkAction === \"open-pdf\") {\n uriParts.push(\"zotero://open-pdf\");\n if (item.isRegularItem()) {\n targetItem = (await item.getBestAttachments()).find((att) =>\n att.isPDFAttachment()\n );\n } else if (item.isAnnotation()) {\n targetItem = item.parentItem;\n // If the item is an annotation, we want to open the PDF at the page of the annotation\n let pageIndex = 1;\n try {\n pageIndex = JSON.parse(item.annotationPosition).pageIndex + 1;\n } catch (e) {\n Zotero.warn(e);\n }\n uriParams = ?page=${pageIndex}&annotation=${item.key};\n }\n} else {\n uriParts.push(\"zotero://select\");\n if (item?.isAnnotation()) {\n targetItem = item.parentItem;\n }\n}\n\nif (!targetItem && !collection) return \"[Copy Zotero Link] item is invalid\";\n\n// Get the link text using the link_text_field argument\nlet linkText;\nif (collection) {\n // When collection is truthy, this script was triggered in the collection menu.\n // Use collection name if this is a collection link\n linkText = collection.name;\n} else if (item.isAttachment()) {\n // Try to use top-level item for link text\n linkText = Zotero.Items.getTopLevel([item])[0].getField(linkTextField);\n} else if (item.isAnnotation()) {\n // Add the annotation text to the link text\n linkText = ${targetItem.getField(linkTextField)}(${\n item.annotationComment || item.annotationText || \"annotation\"\n });\n} else {\n // Use the item's field\n linkText = item.getField(linkTextField);\n}\n\n// Add the library or group URI part (collection must go first)\nlet libraryType = (collection || item).library.libraryType;\nif (libraryType === \"user\") {\n uriParts.push(\"library\");\n} else {\n uriParts.push(\n groups/${Zotero.Libraries.get((collection || item).libraryID).groupID}\n );\n}\n\n// If useColl, make the link collection specific\nif (useColl) {\n // see https://forums.zotero.org/discussion/73893/zotero-select-for-collections\n let coll = collection || Zotero.getActiveZoteroPane().getSelectedCollection();\n\n // It's possible that a collection isn't selected. When that's the case,\n // this will fall back to the typical library behavior.\n\n // If a collection is selected, add the collections URI part\n if (!!coll) uriParts.push(collections/${coll.key});\n}\n\nif (!collection) {\n // Add the item URI part\n uriParts.push(items/${targetItem.key});\n}\n\n// Join the parts together\nlet uri = uriParts.join(\"/\");\n\n// Add the URI parameters\nif (uriParams) {\n uri += uriParams;\n}\n\nif (useNoteLink && item?.isNote() && Zotero.BetterNotes) {\n uri = Zotero.BetterNotes.api.convert.note2link(item);\n}\n\n// Format the link and copy it to the clipboard\nconst clipboard = new Zotero.ActionsTags.api.utils.ClipboardHelper();\nif (linkType == \"html\") {\n clipboard.addText(<a href=\"${uri}\">${linkText}</a>, \"text/unicode\");\n} else if (linkType == \"md\") {\n clipboard.addText([${linkText}](${uri}), \"text/unicode\");\n} else {\n clipboard.addText(uri, \"text/unicode\");\n}\n\nclipboard.addText(<a href=\"${uri}\">${linkText}</a>, \"text/html\");\n\nclipboard.copy();\n\nreturn [Copy Zotero Link] link ${uri} copied.;// @author windingwind, garth74\n// @link https://github.com/windingwind/zotero-actions-tags/discussions/115\n// @usage Select an item in the library and press the assigned shortcut keys\n// @update Mon, 22 Jan 2024 00:10:18 GMT (by new Date().toGMTString())\n\n// EDIT THESE SETTINGS\n\n/ @type {string} Name of the field to use as the link text. To use the citation key, set this to \"citationKey\". */\nlet linkTextField = \"title\";\n\n/ @type {'html' | 'md' | 'plain'} What type of link to create. */\nlet linkType = \"html\";\n\n/* @type {boolean} If true, make the link specific to the currently selected collection. /\nlet useColl = false;\n\n/ @type {boolean} If true, use Better Notes zotero://note link when the selected item is a note. */\nlet useNoteLink = false;\n\n/* @type {'select' | 'open-pdf' | 'auto'} Action of link/\nlet linkAction = \"auto\"; // auto = open-pdf for PDFs and annotations, select for everything else\n\n// END OF EDITABLE SETTINGS\n\n// For efficiency, only execute once for all selected items\nif (item) return;\nitem = items[0];\nif (!item && !collection) return \"[Copy Zotero Link] item is empty\";\n\nif (collection) {\n linkAction = \"select\";\n useColl = true;\n}\n\nif (linkAction === \"auto\") {\n if (item.isPDFAttachment() || item.isAnnotation()) {\n linkAction = \"open-pdf\";\n } else {\n linkAction = \"select\";\n }\n}\n\nconst uriParts = [];\nlet uriParams = \"\";\n\nlet targetItem = item;\nif (linkAction === \"open-pdf\") {\n uriParts.push(\"zotero://open-pdf\");\n if (item.isRegularItem()) {\n targetItem = (await item.getBestAttachments()).find((att) =>\n att.isPDFAttachment()\n );\n } else if (item.isAnnotation()) {\n targetItem = item.parentItem;\n // If the item is an annotation, we want to open the PDF at the page of the annotation\n let pageIndex = 1;\n try {\n pageIndex = JSON.parse(item.annotationPosition).pageIndex + 1;\n } catch (e) {\n Zotero.warn(e);\n }\n uriParams = ?page=${pageIndex}&annotation=${item.key};\n }\n} else {\n uriParts.push(\"zotero://select\");\n if (item?.isAnnotation()) {\n targetItem = item.parentItem;\n }\n}\n\nif (!targetItem && !collection) return \"[Copy Zotero Link] item is invalid\";\n\n// Get the link text using the link_text_field argument\nlet linkText;\nif (collection) {\n // When collection is truthy, this script was triggered in the collection menu.\n // Use collection name if this is a collection link\n linkText = collection.name;\n} else if (item.isAttachment()) {\n // Try to use top-level item for link text\n linkText = Zotero.Items.getTopLevel([item])[0].getField(linkTextField);\n} else if (item.isAnnotation()) {\n // Add the annotation text to the link text\n linkText = ${targetItem.getField(linkTextField)}(${\n item.annotationComment || item.annotationText || \"annotation\"\n });\n} else {\n // Use the item's field\n linkText = item.getField(linkTextField);\n}\n\n// Add the library or group URI part (collection must go first)\nlet libraryType = (collection || item).library.libraryType;\nif (libraryType === \"user\") {\n uriParts.push(\"library\");\n} else {\n uriParts.push(\n groups/${Zotero.Libraries.get((collection || item).libraryID).groupID}\n );\n}\n\n// If useColl, make the link collection specific\nif (useColl) {\n // see https://forums.zotero.org/discussion/73893/zotero-select-for-collections\n let coll = collection || Zotero.getActiveZoteroPane().getSelectedCollection();\n\n // It's possible that a collection isn't selected. When that's the case,\n // this will fall back to the typical library behavior.\n\n // If a collection is selected, add the collections URI part\n if (!!coll) uriParts.push(collections/${coll.key});\n}\n\nif (!collection) {\n // Add the item URI part\n uriParts.push(items/${targetItem.key});\n}\n\n// Join the parts together\nlet uri = uriParts.join(\"/\");\n\n// Add the URI parameters\nif (uriParams) {\n uri += uriParams;\n}\n\nif (useNoteLink && item?.isNote() && Zotero.BetterNotes) {\n uri = Zotero.BetterNotes.api.convert.note2link(item);\n}\n\n// Format the link and copy it to the clipboard\nconst clipboard = new Zotero.ActionsTags.api.utils.ClipboardHelper();\nif (linkType == \"html\") {\n clipboard.addText(<a href=\"${uri}\">${linkText}</a>, \"text/unicode\");\n} else if (linkType == \"md\") {\n clipboard.addText([${linkText}](${uri}), \"text/unicode\");\n} else {\n clipboard.addText(uri, \"text/unicode\");\n}\n\nclipboard.addText(<a href=\"${uri}\">${linkText}</a>, \"text/html\");\n\nclipboard.copy();\n\nreturn [Copy Zotero Link] link ${uri} copied.;","shortcut":"","enabled":true,"menu":"Copy Item Link","name":"Copy Item Link","showInMenu":{"item":false,"collection":false,"tools":true,"reader":false,"readerAnnotation":false}} {"itemID":13503,"triggerType":"menu"}

Anything else?

No response