Skip to content

Instantly share code, notes, and snippets.

@Mearman
Created October 24, 2023 02:10
Show Gist options
  • Save Mearman/cfc08f51c33591134f81c7f947e6f49c to your computer and use it in GitHub Desktop.
Save Mearman/cfc08f51c33591134f81c7f947e6f49c to your computer and use it in GitHub Desktop.
OpenAlex BibTeX Bookmarklet
javascript: (function () {
function fetchBibTeX(url) {
fetch(url)
.then((response) => response.json())
.then((data) => {
const authors = data.authorships
.map((author) => author.raw_author_name)
.join(" and ");
const title = data.title;
const journal = data.primary_location.source.display_name;
const year = data.publication_year;
const volume = data.biblio.volume;
const pages = `${data.biblio.first_page}--${data.biblio.last_page}`;
const doi = data.ids.doi.split("/").pop();
const bibtex = `
@article{${authors.replace(/ /g, "")}${year},
author = {${authors}},
title = {${title}},
journal = {${journal}},
year = {${year}},
volume = {${volume}},
pages = {${pages}},
doi = {${doi}}
}`;
navigator.clipboard.writeText(bibtex).then(
function () {
alert("BibTeX entry copied to clipboard");
},
function () {
prompt(
"Failed to copy BibTeX entry to clipboard. You can copy it manually from here:",
bibtex
);
}
);
});
}
const currentUrl = window.location.href;
const openAlexUrlPattern = /https:\/\/api\.openalex\.org\/works\/(\w+)/;
const match = openAlexUrlPattern.exec(currentUrl);
if (match) {
fetchBibTeX(currentUrl);
} else {
const workId = prompt("Enter the OpenAlex Work ID:");
if (workId) {
const url = `https://api.openalex.org/works/${workId}`;
fetchBibTeX(url);
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment