Skip to content

Commit

Permalink
Open WorldCat: Fix saving from detail page after single search result
Browse files Browse the repository at this point in the history
The page markup is invalid, causing the <link> to be interpreted as part
of <body> rather than <head>, so look for just <link> (and switch to
attr()).

(attr()/text() apparently aren't included in search translators, so we
have to bundle that for now.)
  • Loading branch information
dstillman committed May 7, 2021
1 parent 1d1ce61 commit 37ef369
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions Open WorldCat.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
"lastUpdated": "2017-03-19 23:26:57"
}

// attr()/text() v2
// eslint-disable-next-line
function attr(docOrElem,selector,attr,index){var elem=index?docOrElem.querySelectorAll(selector).item(index):docOrElem.querySelector(selector);return elem?elem.getAttribute(attr):null;}function text(docOrElem,selector,index){var elem=index?docOrElem.querySelectorAll(selector).item(index):docOrElem.querySelector(selector);return elem?elem.textContent:null;}

/**
* Gets Zotero item from a WorldCat icon src
*/
Expand Down Expand Up @@ -241,9 +245,9 @@ function doWeb(doc, url) {
// Seems like some single search results redirect to the item page,
// but the URL is still a search URL. Grab cannonical URL from meta tag
// to extract the OCLC ID
var canonicalURL = ZU.xpath(doc, '/html/head/link[@rel="canonical"][1]')[0];
let canonicalURL = attr(doc, "link[rel='canonical']", "href");
if (canonicalURL) {
oclcID = extractOCLCID(canonicalURL.href);
oclcID = extractOCLCID(canonicalURL);
}
}
if (!oclcID) throw new Error("WorldCat: Failed to extract OCLC ID from URL: " + url);
Expand Down Expand Up @@ -343,14 +347,17 @@ function fetchIDs(isbns, ids, callback) {
}
//but sometimes we have single items
else {
var canonicalURL = ZU.xpathText(doc, '/html/head/link[@rel="canonical"]/@href');
let canonicalURL = attr(doc, "link[rel='canonical']", "href");
if (canonicalURL) {
oclcID = extractOCLCID(canonicalURL);
if (!oclcID) throw new Error("WorldCat: Failed to extract OCLC ID from URL: " + url);
scrape([oclcID]);
} else {
Z.debug("No search results found for ISBN " + isbn);
oclcID = extractOCLCID(canonicalURL);
if (!oclcID) {
throw new Error("WorldCat: Failed to extract OCLC ID from URL: " + url);
}
scrape([oclcID]);
}
else {
Z.debug("No search results found for ISBN " + isbn);
}
}
},
function() {
Expand Down

0 comments on commit 37ef369

Please sign in to comment.