Created
March 1, 2023 00:15
-
-
Save sneakers-the-rat/c7e4fa782edad83d58b602dc46a70ae4 to your computer and use it in GitHub Desktop.
check citations on crossref
This file contains 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
<!DOCTYPE html> | |
<html lang="en-US"> | |
<head> | |
<link rel="stylesheet" href="styles.css"> | |
</head> | |
<body> | |
<div class="container"> | |
<div class="query"> | |
<h2>Query Crossref Why Dont Ya</h2> | |
<textarea id="query-box" type="text"></textarea> | |
<div id="query-static" class="hidden"></div> | |
</div> | |
</div> | |
</body> | |
<script type="text/javascript" src="crossref.js"></script> | |
</html> |
This file contains 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
let input = document.getElementById('query-box'); | |
let queryItems = document.getElementById('query-static'); | |
input.addEventListener('change', (evt) => { | |
let cites = split_cites(evt.target.value); | |
console.log(cites) | |
replace_cites(cites); | |
query_crossref(); | |
}) | |
function split_cites(text){ | |
return text.split('\n\n'); | |
} | |
function replace_cites(cites){ | |
input.classList.toggle('hidden'); | |
queryItems.classList.toggle('hidden'); | |
cites.forEach((cite) => { | |
let queryDiv = document.createElement('div'); | |
queryDiv.classList.add('query-item'); | |
queryDiv.innerHTML = cite; | |
queryItems.appendChild(queryDiv); | |
}) | |
} | |
function query_crossref(){ | |
let citeBoxes = document.getElementsByClassName('query-item'); | |
let query_base = 'https://api.crossref.org/works?query=' | |
Array.prototype.forEach.call(citeBoxes, (cite) => { | |
let cite_enc = encodeURIComponent(cite.innerHTML); | |
fetch(query_base + cite_enc) | |
.then((res) => res.json()) | |
.then((res) => { | |
console.log(res) | |
if (res.status !== 'ok'){ | |
cite.classList.add('no-results'); | |
} | |
let filteredItems = res.message.items | |
.filter(item => item.score > 80); | |
if (filteredItems.length === 0){ | |
cite.classList.add('no-results') | |
} else { | |
cite.classList.add('results'); | |
filteredItems.forEach((item) => { | |
let itemDiv = document.createElement('div'); | |
itemDiv.classList.add('result-item'); | |
let score = document.createElement('p'); | |
score.textContent = 'score: ' + item.score; | |
let title = document.createElement('p'); | |
title.textContent = 'title: ' + item.title; | |
let doi = document.createElement('p'); | |
doi.textContent = 'doi: ' + item.DOI; | |
itemDiv.appendChild(score); | |
itemDiv.appendChild(title); | |
itemDiv.appendChild(doi); | |
cite.appendChild(itemDiv); | |
}) | |
} | |
}) | |
}) | |
} | |
This file contains 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
body { | |
width: 100%; | |
height: 100%; | |
position: absolute; | |
} | |
.container { | |
width: 100%; | |
height: 100%; | |
} | |
.container>div { | |
width: 100%; | |
height: 100%; | |
} | |
.container div { | |
padding: 1em; | |
box-sizing: border-box; | |
} | |
#query-box { | |
height: 100%; | |
width: 100%; | |
box-sizing: border-box; | |
} | |
.query-item { | |
border: 1px solid black; | |
border-radius: 3px; | |
margin-bottom: 1em; | |
} | |
.result-item { | |
border-left: 1px solid black; | |
padding-top: 0 !important; | |
padding-bottom: 0 !important; | |
margin-bottom: 1em; | |
} | |
.hidden { | |
display:none; | |
} | |
.no-results { | |
border: 1px solid red; | |
} | |
.results { | |
border: 1px solid green; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment