Skip to content

Commit

Permalink
Open WorldCat: Fully fix detection
Browse files Browse the repository at this point in the history
__NEXT_DATA__ won't change when navigating to a /title/ page from a
search, so just fall back to 'book' when we can't find an item type.

Fixes zotero#2912
AbeJellinek committed Nov 1, 2022
1 parent d2ea0da commit 23b2f42
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions Open WorldCat.js
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@
"inRepository": true,
"translatorType": 12,
"browserSupport": "gcsibv",
"lastUpdated": "2022-10-27 21:05:53"
"lastUpdated": "2022-11-01 19:40:33"
}

/*
@@ -118,12 +118,13 @@ const RECORD_MAPPING = {
};

function detectWeb(doc, url) {
if (url.includes('/title/') && doc.querySelector('#__NEXT_DATA__')) {
let nextData = doc.querySelector('#__NEXT_DATA__');
if (url.includes('/title/') && nextData) {
try {
return getItemType(JSON.parse(text(doc, '#__NEXT_DATA__')).props.pageProps.record);
return getItemType(JSON.parse(nextData.textContent).props.pageProps.record);
}
catch (e) {
Z.monitorDOMChanges(doc.body, { childList: true });
return 'book';
}
}
else if (getSearchResults(doc, true)) {
@@ -132,6 +133,7 @@ function detectWeb(doc, url) {
else if (url.includes('/search?')) {
Z.monitorDOMChanges(doc.body, { childList: true, subtree: true });
}

return false;
}

@@ -180,8 +182,12 @@ async function doWeb(doc, url) {
}

async function scrape(doc, url = doc.location.href) {
let record = JSON.parse(text(doc, '#__NEXT_DATA__')).props.pageProps.record;
if (!url.includes('/' + record.oclcNumber)) {
let record = null;
try {
record = JSON.parse(text(doc, '#__NEXT_DATA__')).props.pageProps.record;
}
catch (e) {}
if (!record || !url.includes('/' + record.oclcNumber)) {
Zotero.debug('__NEXT_DATA__ is stale; requesting page again');
doc = await requestDocument(url);
record = JSON.parse(text(doc, '#__NEXT_DATA__')).props.pageProps.record;

0 comments on commit 23b2f42

Please sign in to comment.