From 4b622f074dafcf8cb3720990ca855b54a55e708d Mon Sep 17 00:00:00 2001 From: Martynas Bagdonas Date: Tue, 23 Feb 2021 15:33:51 +0200 Subject: [PATCH] Add function to export note with dataURI images --- chrome/content/zotero/xpcom/data/notes.js | 25 +++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/chrome/content/zotero/xpcom/data/notes.js b/chrome/content/zotero/xpcom/data/notes.js index f4ad5c4cf2..19424c36fe 100644 --- a/chrome/content/zotero/xpcom/data/notes.js +++ b/chrome/content/zotero/xpcom/data/notes.js @@ -120,6 +120,31 @@ Zotero.Notes = new function() { await Zotero.Notifier.trigger('refresh', 'item', idsToRefresh); }); }; + + this.getExportableNote = async function(item) { + if (!item.isNote()) { + throw new Error('Item is not a note'); + } + var note = item.getNote(); + + var parser = Components.classes["@mozilla.org/xmlextras/domparser;1"] + .createInstance(Components.interfaces.nsIDOMParser); + var doc = parser.parseFromString(note, 'text/html'); + + var nodes = doc.querySelectorAll('img[data-attachment-key]'); + for (var node of nodes) { + var attachmentKey = node.getAttribute('data-attachment-key'); + if (attachmentKey) { + var attachment = Zotero.Items.getByLibraryAndKey(item.libraryID, attachmentKey); + if (attachment && attachment.parentID == item.id) { + var dataURI = await attachment.attachmentDataURI; + node.setAttribute('src', dataURI); + } + } + node.removeAttribute('data-attachment-key'); + } + return doc.body.innerHTML; + }; }; if (typeof process === 'object' && process + '' === '[object process]') {