Skip to content

Commit

Permalink
Added support for image in notes - Base64 url
Browse files Browse the repository at this point in the history
  • Loading branch information
frianasoa committed Dec 5, 2023
1 parent 8517014 commit e4ec34b
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 16 deletions.
3 changes: 3 additions & 0 deletions bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var Ui;
var Format;
var Utils
var Filter;
var Image;

const ANNOTATION = 1;
const ATTACHMENT = 3;
Expand Down Expand Up @@ -205,6 +206,7 @@ async function startup({ id, version, resourceURI, rootURI = resourceURI.spec })
// setDefaultPrefs(rootURI);
}

Services.scriptloader.loadSubScript(rootURI + 'core/image.js');
Services.scriptloader.loadSubScript(rootURI + 'core/filter.js');
Services.scriptloader.loadSubScript(rootURI + 'core/utils.js');
Services.scriptloader.loadSubScript(rootURI + 'core/zenotes.js');
Expand All @@ -226,6 +228,7 @@ async function startup({ id, version, resourceURI, rootURI = resourceURI.spec })
ZeNotes.Menu = Menu;
ZeNotes.Utils = Utils;
ZeNotes.Filter = Filter;
ZeNotes.Image = Image;

ZeNotes.Data = Data;
ZeNotes.Database = Database;
Expand Down
2 changes: 1 addition & 1 deletion content/settings/display.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
<tr><td>Highlight opacity</td><td><input type="range" min="0" max="255" value="255" id="zn-bg-slider" onchange="Zotero_Preferences.ZeNotes.setpreference(event, 'bg-opacity');" oninput="Zotero_Preferences.ZeNotes.updateopacity(event);"/></td><td class='highlight' style='background-color: #FFFF00; height: 1.1em;' id="zn-bg-sample">Lorem ipsum</td></tr>
<tr><td>Header size</td><td><input id="zn-header-size" type="range" value="100" min="20" max="1500" onchange="Zotero_Preferences.ZeNotes.setpreference(event, 'header-size');" oninput="Zotero_Preferences.ZeNotes.updatecolumnwidth(event);"/></td><td><input id="zn-header-size-val" /></td></tr>
<tr><td>Column width (for vertical)</td><td><input id="zn-column-width" type="range" value="100" min="20" max="1500" onchange="Zotero_Preferences.ZeNotes.setpreference(event, 'column-width');" oninput="Zotero_Preferences.ZeNotes.updatedisplay(event, 'zn-column-width-val');"/></td><td><input id="zn-column-width-val" /></td></tr>
<tr><td>Filter html</td><td colspan="2"><input id="zn-html-filter" type="text" onchange="Zotero_Preferences.ZeNotes.setpreference(event, 'html-filter');" style="width:100%;" placeholder="Comma separeted CSS selectors"/></td><td></td></tr>
<tr><td>Filter html</td><td><input id="zn-html-filter" type="text" onchange="Zotero_Preferences.ZeNotes.setpreference(event, 'html-filter');" style="width:100%;" placeholder="Comma separeted CSS selectors"/></td><td><input id="zn-html-filter-replacement" type="text" onchange="Zotero_Preferences.ZeNotes.setpreference(event, 'html-filter-replacement');" style="width:100%;" placeholder="Replace with"/></td></tr>
</table>
</div>
2 changes: 2 additions & 0 deletions content/settings/preferences.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,8 @@ Zotero_Preferences.ZeNotes = {
Zotero_Preferences.ZeNotes.loadpreferences();
Zotero_Preferences.ZeNotes.initopacity();
Zotero_Preferences.ZeNotes.loadpreference("html-filter", "zn-html-filter");
Zotero_Preferences.ZeNotes.loadpreference("html-filter-replacement", "zn-html-filter-replacement");

Zotero_Preferences.ZeNotes.loadpreference("load-on-change", "zn-reload-on-change");
Zotero_Preferences.ZeNotes.loadpreference("vertical-table", "zn-vertical-table");

Expand Down
13 changes: 11 additions & 2 deletions core/filter.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Filter = {
apply(txt, selectors = ""){
apply(txt, selectors = "", replacement=""){
this.replacement = replacement
txt = Filter.legacy(txt);
txt = Filter.userdefined(txt, selectors);
return txt;
Expand Down Expand Up @@ -43,13 +44,21 @@ Filter = {
{
if(this.validselector(selector))
{
html.querySelectorAll(selector).forEach(e => e.parentNode.removeChild(e));
if(this.replacement!="")
{
html.querySelectorAll(selector).forEach(e => e.outerHTML=this.replacement);
}
else
{
html.querySelectorAll(selector).forEach(e => e.parentNode.removeChild(e));
}
}
}

var data = html.innerHTML.split("<br>").join("<br/>").split("<hr>").join("<hr/>");
return data;
},

validselector(s){
var document = Zotero.getMainWindow().document;
try {document.createDocumentFragment().querySelector(s);} catch(e) {return false}
Expand Down
13 changes: 8 additions & 5 deletions core/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Format = {
var tagged_items = [];
for(item of items)
{
var notes = Format.notes(item);
var notes = await Format.notes(item);
lines.push(await Format.formatitem(item, notes));
}

Expand Down Expand Up @@ -160,9 +160,10 @@ Format = {
return filenames;
},

notes(item){
async notes(item){
var values = []
var selectors = Zotero.ZeNotes.Prefs.get("html-filter");
var replacement = Zotero.ZeNotes.Prefs.get("html-filter-replacement");

if(![ANNOTATION, ATTACHMENT, NOTE].includes(item.itemTypeID))
{
Expand Down Expand Up @@ -195,7 +196,7 @@ Format = {
{
var notetext = "";
var tags = note.getTags();
tags.forEach(tag=>{
for(tag of tags){
if(!Object.keys(values).includes(tag.tag))
{
values[tag.tag] = "";
Expand All @@ -222,11 +223,13 @@ Format = {
else
{
note_ = note.getNote();
note_ = Zotero.ZeNotes.Filter.apply(note_, selectors);
note_ = Zotero.ZeNotes.Filter.apply(note_, selectors, replacement);
note_ = await Zotero.ZeNotes.Image.render(note_, item);

notetext+=note_+ "<span class='notekey'>"+note.id+"</span><hr/>";
}
values[tag.tag]+= notetext;
})
};
}
}
return values;
Expand Down
36 changes: 36 additions & 0 deletions core/image.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
Image = {
async render(txt, item)
{
if(!txt.includes("<img"))
{
return txt;
}

var html = null;
if (Zotero.platformMajorVersion >= 102) {
var parser = new DOMParser();
html = parser.parseFromString(txt, "text/html").body;
}
else {
const parser = Components.classes['@mozilla.org/xmlextras/domparser;1'].createInstance(Components.interfaces.nsIDOMParser);
html = parser.parseFromString(txt, 'text/html').documentElement;
}

for(img of html.querySelectorAll("img")) {
img.style.width = "99%";
if(img.dataset.attachmentKey)
{
let attachment = Zotero.Items.getByLibraryAndKey(item.libraryID, img.dataset.attachmentKey);
if(attachment)
{
var dataURI = await attachment.attachmentDataURI
img.setAttribute('src', dataURI);
img.removeAttribute('data-attachment-key');
img.removeAttribute('height');
img.removeAttribute('width');
}
}
}
return Promise.resolve(html.innerHTML);
},
}
2 changes: 1 addition & 1 deletion install.rdf
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Description about="urn:mozilla:install-manifest">
<em:id>[email protected]</em:id>
<em:name>ZeNotes</em:name>
<em:version>0.4.4</em:version>
<em:version>0.4.5</em:version>
<em:multiprocessCompatible>true</em:multiprocessCompatible>
<em:updateURL>https://raw.githubusercontent.com/frianasoa/zenotes/main/zenote-update.json</em:updateURL>
<em:homepageURL>https://github.com/frianasoa/zenotes</em:homepageURL>
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"manifest_version": 2,
"name": "Ze Notes",
"version": "0.4.4",
"version": "0.4.5",
"description": "Advanced notes manager",
"homepage_url": "https://github.com/frianasoa/zenotes",
"author": "Fanantenana Rianasoa Andriariniaina",
Expand Down
6 changes: 3 additions & 3 deletions zenote-update.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
"[email protected]": {
"updates": [
{
"version": "0.4.4",
"update_link": "https://github.com/frianasoa/Ze-Notes/releases/download/v0.4.4/zenotes-v0.4.4.xpi",
"update_hash": "sha256:fe43946a005d6ffb568cf4c2340b0da1b2f2f878707a7e66126e6e9b225f9924",
"version": "0.4.5",
"update_link": "https://github.com/frianasoa/Ze-Notes/releases/download/v0.4.5/zenotes-v0.4.5.xpi",
"update_hash": "sha256:5e8ae08c461e1ebbb580f98adaaacc864e13c498e7e0d8df8a63030c54b51885",
"applications": {
"gecko": {
"strict_min_version": "60.0"
Expand Down
6 changes: 3 additions & 3 deletions zenote-update.rdf
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
<rdf:Seq>
<rdf:li>
<rdf:Description>
<ns1:version>0.4.4</ns1:version>
<ns1:version>0.4.5</ns1:version>
<ns1:targetApplication>
<rdf:Description>
<ns1:id>[email protected]</ns1:id>
<ns1:minVersion>5.0.0</ns1:minVersion>
<ns1:maxVersion>6.*</ns1:maxVersion>
<ns1:updateLink>https://github.com/frianasoa/Ze-Notes/releases/download/v0.4.4/zenotes-v0.4.4.xpi</ns1:updateLink>
<ns1:updateLink>https://github.com/frianasoa/Ze-Notes/releases/download/v0.4.5/zenotes-v0.4.5.xpi</ns1:updateLink>
</rdf:Description>
</ns1:targetApplication>

Expand All @@ -20,7 +20,7 @@
<ns1:id>[email protected]</ns1:id>
<ns1:minVersion>4.999</ns1:minVersion>
<ns1:maxVersion>6.*</ns1:maxVersion>
<ns1:updateLink>https://github.com/frianasoa/Ze-Notes/releases/download/v0.4.4/zenotes-v0.4.4.xpi</ns1:updateLink>
<ns1:updateLink>https://github.com/frianasoa/Ze-Notes/releases/download/v0.4.5/zenotes-v0.4.5.xpi</ns1:updateLink>
</rdf:Description>
</ns1:targetApplication>

Expand Down

0 comments on commit e4ec34b

Please sign in to comment.