Skip to content

Instantly share code, notes, and snippets.

@ptschandl
Created July 25, 2020 15:25
Show Gist options
  • Save ptschandl/2033b1c380528ca962596a2f19aa3320 to your computer and use it in GitHub Desktop.
Save ptschandl/2033b1c380528ca962596a2f19aa3320 to your computer and use it in GitHub Desktop.
List recent PubMed Entries on a website without server code, implemented with vanilla JS.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="keywords" content="HTML5, JavaScript, PubMed, API">
<meta name="description" content="List recent PubMed Entries with vanilla JS.">
<title>Recent PubMed Entries</title>
</head>
<body>
<h1>Recent publications (PubMed)</h1>
<ul id="literature_list" style="max-width: 100%; list-style: square inside;">
<li id="loading_indicator">Loading citations...</li>
</ul>
<!--
Author: Philipp Tschandl
License: CC-BY 2.0 (https://creativecommons.org/licenses/by/2.0/)
-->
<script>
async function getPubMedData(url, author) {
const data = await fetch(`https://eutils.ncbi.nlm.nih.gov/
entrez/eutils/esearch.fcgi?
db=pubmed&term=`+String(author)+`[au]&usehistory=n&sort=date`)
.then(response => response.text())
.then(str => (new window.DOMParser()).parseFromString(str, "text/xml"));
return data;
}
async function getPubMedCitations(PMIDList) {
var citation = await fetch(`https://eutils.ncbi.nlm.nih.gov/
entrez/eutils/esummary.fcgi?
db=pubmed&retmode=json&tool=vidir_client_pubmedlist&id=`+String(PMIDList))
.then(response => response.text())
.then(str => JSON.parse(str))
.then(json_arr => json_arr["result"]);
return citation;
}
async function main() {
const data = await getPubMedData(URL, "tschandl+p");
var pmid_list = Array();
for (let pubmed_item of data.getElementsByTagName("Id")) {
pmid_list.push(pubmed_item.innerHTML);
}
const metadata = await getPubMedCitations(pmid_list.join(","));
loading_indicator = document.getElementById("loading_indicator");
loading_indicator.remove();
pmid_list.forEach((el) => {
var current_metadata = metadata[el];
var list_item_node = document.createElement("li");
list_item_node.style["font-family"] = "Lato, sans-serif";
list_item_node.style["padding-bottom"] = "1em";
multi_author_indicator = " et al. ";
if (Object.keys(current_metadata["authors"]).length < 2) {
multi_author_indicator = ". "
}
var first_author = document.createElement("b");
first_author.innerHTML = current_metadata["authors"][0]["name"] + multi_author_indicator;
list_item_node.appendChild(first_author);
var article_title = document.createTextNode(current_metadata["title"]+" ");
list_item_node.appendChild(article_title);
var journal = document.createElement("i");
journal.innerHTML = current_metadata["source"] + " (" +
current_metadata["pubdate"] + ") - ";
journal.style["color"] = "grey";
list_item_node.appendChild(journal);
var linktextnode = document.createTextNode("PMID: "+String(el));
var hyperlink = document.createElement("a");
hyperlink.title = "PMID: "+String(el);
hyperlink.href = "https://pubmed.ncbi.nlm.nih.gov/"+String(el)+"/";
hyperlink.target = "_blank";
hyperlink.appendChild(linktextnode);
list_item_node.appendChild(hyperlink);
document.getElementById("literature_list").appendChild(list_item_node);
})
var reference_node = document.createElement("li");
reference_node.innerHTML = "'Automated PubMed Reference List' - code by Philipp Tschandl"
reference_node.style["color"] = "grey";
document.getElementById("literature_list").appendChild(reference_node);
}
main();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment