Skip to content

Commit

Permalink
DOI: Find DOIs in links, and other improvements
Browse files Browse the repository at this point in the history
Search links in addition to page text, as discussed in the forums [1].

Also:

- Make the order of results match the page
- Possibly fix an issue where some DOIs could be left out if they
  appeared within the same text string (lastMatch should've been
  lastIndex; the former is read-only and non-numeric, so it's not clear
  that this ever worked)
- Simplify the code

[1] https://forums.zotero.org/discussion/comment/350735/#Comment_350735
  • Loading branch information
dstillman committed Mar 13, 2020
1 parent f0c32b0 commit 99a685a
Showing 1 changed file with 60 additions and 41 deletions.
101 changes: 60 additions & 41 deletions DOI.js
Expand Up @@ -9,10 +9,9 @@
"inRepository": true,
"translatorType": 4,
"browserSupport": "gcsibv",
"lastUpdated": "2019-01-27 14:06:07"
"lastUpdated": "2020-03-13 02:38:54"
}


/*
***** BEGIN LICENSE BLOCK *****
Expand All @@ -36,14 +35,6 @@
***** END LICENSE BLOCK *****
*/

// The variables items and selectArray will be filled during the first
// as well as the second retrieveDOIs function call and therefore they
// are defined global.

var items = {};
var selectArray = {};


// builds a list of DOIs
function getDOIs(doc) {
// TODO Detect DOIs more correctly.
Expand All @@ -68,7 +59,7 @@ function getDOIs(doc) {
while (treeWalker.nextNode()) {
if (ignore.includes(treeWalker.currentNode.parentNode.tagName.toLowerCase())) continue;
// Z.debug(node.nodeValue)
DOIre.lastMatch = 0;
DOIre.lastIndex = 0;
while ((m = DOIre.exec(treeWalker.currentNode.nodeValue))) {
DOI = m[0];
if (DOI.endsWith(")") && !DOI.includes("(")) {
Expand All @@ -83,6 +74,27 @@ function getDOIs(doc) {
}
}
}

// FIXME: The test for this (developmentbookshelf.com) fails in Scaffold due
// to a cookie error, though running the code in Scaffold still works
var links = doc.querySelectorAll('a[href]');
for (let link of links) {
DOIre.lastIndex = 0;
let m = DOIre.exec(link.href);
if (m) {
let doi = m[0];
if (doi.endsWith(")") && !doi.includes("(")) {
doi = doi.substr(0, doi.length - 1);
}
if (doi.endsWith("}") && !doi.includes("{")) {
doi = doi.substr(0, doi.length - 1);
}
// only add new DOIs
if (!dois.includes(doi)) {
dois.push(doi);
}
}
}

return dois;
}
Expand All @@ -102,49 +114,51 @@ function detectWeb(doc, url) {
return false;
}

function completeDOIs(_doc) {
// all DOIs retrieved now
// check to see if there is more than one DOI
var numDOIs = Object.keys(selectArray).length;
if (numDOIs == 0) {
throw new Error("DOI Translator: could not find DOI");
}
else {
Zotero.selectItems(selectArray, function (selectedDOIs) {
if (!selectedDOIs) return true;
function retrieveDOIs(dois) {
let items = {};
let numDOIs = dois.length;

for (var DOI in selectedDOIs) {
items[DOI].complete();
}
return true;
});
}
}

function retrieveDOIs(dois, doc) {
let numDois = dois.length;

for (const DOI of dois) {
for (const doi of dois) {
items[doi] = null;

const translate = Zotero.loadTranslator("search");
translate.setTranslator("b28d0d42-8549-4c6d-83fc-8382874a5cb9");

translate.setSearch({ itemType: "journalArticle", DOI: DOI });
translate.setSearch({ itemType: "journalArticle", DOI: doi });

// don't save when item is done
translate.setHandler("itemDone", function (_translate, item) {
selectArray[item.DOI] = item.title;
if (!item.title) {
Zotero.debug("No title available for " + item.DOI);
item.title = "[No Title]";
selectArray[item.DOI] = "[" + item.DOI + "]";
}
items[item.DOI] = item;
});
/* eslint-disable no-loop-func */
translate.setHandler("done", function () {
numDois--;
if (numDois <= 0) {
completeDOIs(doc);
numDOIs--;

// All DOIs retrieved
if (numDOIs <= 0) {
// Check to see if there's at least one DOI
if (!Object.keys(items).length) {
throw new Error("DOI Translator: could not find DOI");
}

// Only show items that resolved successfully
let select = {};
for (let doi in items) {
let item = items[doi];
if (item) {
select[doi] = item.title || "[" + item.DOI + "]";
}
}
Zotero.selectItems(select, function (selectedDOIs) {
if (!selectedDOIs) return;

for (let selectedDOI in selectedDOIs) {
items[selectedDOI].complete();
}
});
}
});

Expand All @@ -158,7 +172,7 @@ function retrieveDOIs(dois, doc) {
function doWeb(doc) {
var dois = getDOIs(doc);
Z.debug(dois);
retrieveDOIs(dois, doc);
retrieveDOIs(dois);
}

/** BEGIN TEST CASES **/
Expand Down Expand Up @@ -187,6 +201,11 @@ var testCases = [
"type": "web",
"url": "https://en.wikipedia.org/wiki/Template_talk:Doi",
"items": "multiple"
},
{
"type": "web",
"url": "https://www.developmentbookshelf.com/action/showPublications",
"items": "multiple"
}
]
/** END TEST CASES **/

0 comments on commit 99a685a

Please sign in to comment.