Skip to content

Commit

Permalink
First attempt at a version for Zotero 7. Seems to work!
Browse files Browse the repository at this point in the history
  • Loading branch information
dtod committed May 15, 2024
1 parent eb6d104 commit 9260dc9
Show file tree
Hide file tree
Showing 10 changed files with 220 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ ZoteroPreview.xpi.bak
ZoteroPreview.xpi.failed
ZoteroPreview.xpi.2022-07-18-10-23.bak
ZoteroPreview.xpi.*.bak
ZoteroPreview7.xpi.*.bak
ZoteroPreview7.xpi.*.bak
ZoteroAbstractCleaner7.xpi.*.bak
Binary file added ZoteroAbstractCleaner7.xpi
Binary file not shown.
9 changes: 9 additions & 0 deletions ZoteroAbstractCleaner7/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Zotero Abstract Cleaner
=======================

This open source project is for fixing line endings in abstracts in Zotero, for example, after copying and pasting from Adobe Acrobat. It adds a menu item in the right-click contextual menu for items. This means that you can select multiple items and apply this function to all of them.

# Installing

To install Zotero Abstract Cleaner, download the latest version of Zotero Abstract Cleaner from the Releases section of this GitHub repo. In **Zotero** , open Tools -> Add-ons. Then drag the Zotero Abstract Cleaner .xpi file onto the Zotero Add-ons window.

41 changes: 41 additions & 0 deletions ZoteroAbstractCleaner7/ZoteroAbstractCleaner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
Zotero.ZoteroAbstractCleaner = {

id: null,
version: null,
rootURI: null,
initialized: false,

init({ id, version, rootURI })
{
if(this.initialized) return;

this.id = id;
this.version = version;
this.rootURI = rootURI;
this.initialized = true;
},

// when you copy from some formats e.g. pdf, the line endings are crap
// added as an overlay.xul menu item
fixAbstract: async function(){
var items = Zotero.getActiveZoteroPane().getSelectedItems();
for (var x=0; x<items.length; x++){
var itemID = items[x].id;
var item = Zotero.Items.get(itemID);
var abstract = item.getField('abstractNote');
//Zotero.debug(abstract);
// bit more tedious but preserves paragraphs
abstract = abstract.replace(/\r/gm," ");
abstract = abstract.replace(/\n\n/gm,"<br>");
abstract = abstract.replace(/\n/gm," ");
abstract = abstract.replace(/<br>/gm,"\n\n");
abstract = abstract.replace(/ +/g," ");
//Zotero.debug(abstract);
item.setField('abstractNote',abstract);
await item.saveTx();
}
},

};


18 changes: 18 additions & 0 deletions ZoteroAbstractCleaner7/ZoteroAbstractCleaner7-updates.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"addons": {
"[email protected]": {
"updates": [
{
"version": "0.1",
"update_link": "https://raw.githubusercontent.com/dcartertod/zotero-plugins/main/ZoteroAbstractCleaner7.xpi",
"applications": {
"zotero": {
"strict_min_version": "6.999"
}
}
}
]
}
}
}

44 changes: 44 additions & 0 deletions ZoteroAbstractCleaner7/bootstrap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
if (typeof Zotero == 'undefined')
{
var Zotero;
}

function log(msg) {
Zotero.debug("Zotero Abstract Cleaner 0.1: " + msg);
}

function install() {
log("Installed ");
}

async function startup({ id, version, rootURI }) {
log("Starting " + version);
Zotero.debug("Starting " + version);

Zotero.debug("load sub script " + version);
Services.scriptloader.loadSubScript(rootURI + 'ZoteroAbstractCleaner.js');
Services.scriptloader.loadSubScript(rootURI + 'zoteroabstractcleaner_menu.js');

Zotero.ZoteroAbstractCleaner.init({ id, version, rootURI });
Zotero.ZoteroAbstractCleaner.Menus.init();

}

function shutdown()
{
log('ZotMoov: Shutting down');
// Zotero.ZoteroAbstractCleaner.destroy();
Zotero.ZoteroAbstractCleaner.Menus.destroy();

Zotero.ZoteroAbstractCleaner = null;
}

function uninstall()
{
log('ZotMoov: Uninstalled');
// Zotero.ZoteroAbstractCleaner.destroy();
Zotero.ZoteroAbstractCleaner.Menus.destroy();

Zotero.ZoteroAbstractCleaner = null;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ZoteroAbstractCleaner-abstract = Fix Line Endings in Abstract
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ZoteroAbstractCleaner-abstract = Fix Line Endings in Abstract
15 changes: 15 additions & 0 deletions ZoteroAbstractCleaner7/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"manifest_version": 2,
"name": "ZoteroAbstractCleaner",
"version": "0.1",
"description": "Clean up line endings for Zotero 7.",
"homepage_url": "https://github.com/dcartertod/zotero-plugins",
"applications": {
"zotero": {
"id": "[email protected]",
"update_url": "https://raw.githubusercontent.com/dcartertod/zotero-plugins/main/ZoteroAbstractCleaner7/ZoteroAbstractCleaner7-updates.json",
"strict_min_version": "6.999",
"strict_max_version": "7.0.*"
}
}
}
89 changes: 89 additions & 0 deletions ZoteroAbstractCleaner7/zoteroabstractcleaner_menu.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// ZoteroAbstractCleaner
// Indebted to Zotmoov code written by Wiley Yu

Components.utils.import('resource://gre/modules/Services.jsm');

Zotero.ZoteroAbstractCleaner.Menus = {
_store_added_elements: [],
_zac_menu_item: null,

_window_listener:
{
onOpenWindow: function(a_window)
{
let dom_window = a_window.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindow);
dom_window.addEventListener('load', function()
{
dom_window.removeEventListener('load', arguments.callee, false);
if (dom_window.document.documentElement.getAttribute('windowtype') != 'navigator:browser') return;
Zotero.ZoteroAbstractCleaner.Menus._store_added_elements = []; // Clear tracked elements since destroyed by closed window
Zotero.ZoteroAbstractCleaner.Menus._zac_menu_item = null;
Zotero.ZoteroAbstractCleaner.Menus._init();
}, false);
}
},

_getWindow()
{
let enumerator = Services.wm.getEnumerator('navigator:browser');
while (enumerator.hasMoreElements())
{
let win = enumerator.getNext();
if (!win.ZoteroPane) continue;
return win;
}
},

init()
{
this._init();
Services.wm.addListener(this._window_listener);
},

_init()
{
let win = this._getWindow();
let doc = win.document;

// Menu separator
let menuseparator = doc.createXULElement('menuseparator');

// ZAC Menu item
this._zac_menu_item = doc.createXULElement('menuitem');
this._zac_menu_item.id = 'zoteroabstractcleaner-menuitem';
this._zac_menu_item.setAttribute('data-l10n-id', 'ZoteroAbstractCleaner-abstract');
this._zac_menu_item.addEventListener('command', function()
{
Zotero.ZoteroAbstractCleaner.fixAbstract();
});

let zotero_itemmenu = doc.getElementById('zotero-itemmenu');

zotero_itemmenu.appendChild(menuseparator);
zotero_itemmenu.appendChild(this._zac_menu_item);

this._store_added_elements.push(menuseparator, this._zac_menu_item);

// Enable localization
win.MozXULElement.insertFTLIfNeeded('ZoteroAbstractCleaner.ftl');
},

destroy()
{
this._destroy();
Services.wm.removeListener(this._window_listener);
},

_destroy()
{
let doc = this._getWindow().document;
for (let element of this._store_added_elements)
{
if (element) element.remove();
}
doc.querySelector('[href="ZoteroAbstractCleaner.ftl"]').remove();

this._store_added_elements = [];
this._zac_menu_item = null;
}
}

0 comments on commit 9260dc9

Please sign in to comment.