Skip to content

Instantly share code, notes, and snippets.

@franklindyer
Last active January 15, 2024 21:36
Show Gist options
  • Save franklindyer/7d5c0d58054a7429ffdf79ff7665c3a4 to your computer and use it in GitHub Desktop.
Save franklindyer/7d5c0d58054a7429ffdf79ff7665c3a4 to your computer and use it in GitHub Desktop.
Draft Zotero translator for Harvard Case Law
/*
***** BEGIN LICENSE BLOCK *****
Copyright © 2022 YOUR_NAME <- TODO
This file is part of Zotero.
Zotero is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Zotero is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with Zotero. If not, see <http://www.gnu.org/licenses/>.
***** END LICENSE BLOCK *****
*/
function detectWeb(doc, url) {
if (doc.querySelectorAll('.case-container').length > 0) {
return 'case';
}
else if (getSearchResults(doc, true)) {
return 'multiple';
}
return false;
}
function getSearchResults(doc, checkOnly) {
var items = {};
var found = false;
// TODO: adjust the CSS selector
var rows = doc.querySelectorAll('h2 > a.title[href*="/article/"]');
for (let row of rows) {
// TODO: check and maybe adjust
let href = row.href;
// TODO: check and maybe adjust
let title = ZU.trimInternal(row.textContent);
if (!href || !title) continue;
if (checkOnly) return true;
found = true;
items[href] = title;
}
return found ? items : false;
}
async function doWeb(doc, url) {
if (detectWeb(doc, url) == 'multiple') {
let items = await Zotero.selectItems(getSearchResults(doc, false));
if (!items) return;
for (let url of Object.keys(items)) {
await scrape(await requestDocument(url));
}
}
else {
await scrape(doc, url);
}
}
async function scrape(doc, url = doc.location.href) {
// Z.debug(doc.body);
Z.debug(attr(doc, "a[href*='api.case.law/v1/cases/']", 'href'));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment