Created
March 27, 2020 23:59
-
-
Save freshyill/a3af65648a71c57a4af894cef0cbccb0 to your computer and use it in GitHub Desktop.
Get library from Zotero
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function getZotero(req) { | |
let nextBtn = document.getElementById("loadNext"); | |
let prevBtn = document.getElementById("loadPrev"); | |
let content = document.getElementById("content"); | |
let links = {}; | |
await axios.get(req).then(function(response) { | |
content.innerHTML = ''; | |
let collection = response.data; | |
let respLinks = response.headers.link.split(","); | |
for (item of respLinks) { | |
let link = /<([^>]+)>;\s+rel="([^"]+)"/ig.exec(item); | |
links[link[2]]=link[1]; | |
} | |
if (links.prev) { | |
prevBtn.disabled = false; | |
} else { | |
prevBtn.disabled = true; | |
} | |
if (links.next) { | |
nextBtn.disabled = false; | |
} else { | |
nextBtn.disabled = true; | |
} | |
async function prevHandler() { | |
prevBtn.removeEventListener("click", prevHandler, false); | |
nextBtn.removeEventListener("click", nextHandler, false); | |
if (links.prev) { | |
await getZotero(links.prev); | |
} | |
} | |
async function nextHandler() { | |
prevBtn.removeEventListener("click", prevHandler, false); | |
nextBtn.removeEventListener("click", nextHandler, false); | |
if (links.next) { | |
await getZotero(links.next); | |
} | |
} | |
nextBtn.addEventListener("click", nextHandler, false); | |
prevBtn.addEventListener("click", prevHandler, false); | |
Object.entries(collection).forEach(([key, item]) => { | |
let itemTitle = item.data.title; | |
let itemLink = item.data.url; | |
let itemCreator = item.meta.creatorSummary ? item.meta.creatorSummary : "—"; | |
let itemDate = item.data.dateAdded ? item.data.dateAdded : "—"; | |
let itemJournal = item.data.journalAbbreviation ? item.data.journalAbbreviation : "—"; | |
let itemPub = item.data.publicationTitle ? item.data.publicationTitle : ""; | |
let itemPlace = item.data.place ? item.data.place : ""; | |
let itemMeeting = item.data.meetingName ? item.data.meetingName : ""; | |
let pubCol = ''; | |
if (item.data.presentationType) { | |
pubCol = `${itemMeeting}, ${itemPlace}`; | |
} | |
else { | |
pubCol = itemPub; | |
} | |
content.innerHTML += ` | |
<tr> | |
<td><a href="${itemLink}">${itemTitle}</a></td> | |
<td>${itemCreator}</td> | |
<td>${pubCol}</td> | |
<td>${moment(itemDate).format("YYYY-MM")}</td> | |
</tr> | |
`; | |
}); | |
}) | |
.catch(function(){ | |
content.innerHTML += `<tr><td colspan="4">An error occurred loading links.</td></tr>` | |
}); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<table class="table table-striped zotero-table" id="zotero-table"> | |
<thead class="thead-dark"> | |
<tr> | |
<th>Title</th> | |
<th>Author(s)</th> | |
<th>Publication</th> | |
<th>Date</th> | |
</tr> | |
</thead> | |
<tbody id="content" class="zotero-content"> | |
</tbody> | |
</table> | |
<p class="prev-next"> | |
<button id="loadPrev" class="prev-next-btn" disabled>Previous</button> | |
<button id="loadNext" class="prev-next-btn" disabled>Next</button> | |
</p> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.19.1/axios.min.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script> | |
<script src="/js/zotero.js"></script> | |
<script> | |
getZotero('https://api.zotero.org/groups/2226351/collections/HAV9WGEW/items?itemType=-attachment&sort=dateAdded&direction=desc&limit=50'); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment