Created
November 20, 2023 10:11
Open In Sci-Hub Userscript
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
// ==UserScript== | |
// @name Open In Sci-Hub | |
// @namespace Violentmonkey Scripts | |
// @match https://sciencedirect.com/* | |
// @match https://link.springer.com/article/* | |
// @match https://ieeexplore.ieee.org/document/* | |
// @match https://www.sciencedirect.com/science/article/* | |
// @match https://www.nature.com/articles/* | |
// @match https://journals.plos.org/plosone/article?id=* | |
// @match https://academic.oup.com/journals/* | |
// @match https://www.jstor.org/stable/* | |
// @match https://pubmed.ncbi.nlm.nih.gov/* | |
// @match https://psycnet.apa.org/* | |
// @match https://pubs.acs.org/doi/* | |
// @grant GM_xmlhttpRequest | |
// @version 0.01 | |
// @author github.com/ifeelagood | |
// @description Attempts to find PDF on Sci-Hub and opens it in a new tab (based on DOI). | |
// ==/UserScript== | |
// TODO implement 404 handling for PDF not found | |
const scihub = "https://sci-hub.se/"; // change to your preferred scihub domain | |
function getDOI() { | |
// This works for 99% of sites. Otherwise, I have seen many other meta tags containing DOI. | |
var doi = document.querySelector('meta[name="citation_doi"]'); | |
if (doi) { return doi.content; } | |
var doi = document.querySelector('a[href*="doi.org"]'); | |
if (doi) | |
{ | |
var matches = doi.href.match(/doi\.org\/(.+)/); | |
if (matches) { return matches[1]; } | |
} | |
return null; | |
} | |
function getPDFUrl(doi) { | |
return new Promise((resolve, reject) => { | |
var url = scihub + doi; | |
// include via | |
GM_xmlhttpRequest({ | |
method: "GET", | |
url: url, | |
onload: function(response) { | |
if (response.status === 200 && response.responseText) { | |
var parser = new DOMParser(); | |
var doc = parser.parseFromString(response.responseText, "text/html"); | |
var pdf = doc.querySelector("#pdf"); | |
if (pdf) { | |
// remove protocol and domain if present via object | |
var pdfUrlObj = new URL(pdf.src); | |
var pdfUrl = scihub + pdfUrlObj.pathname + pdfUrlObj.search; | |
resolve(pdfUrl); | |
} else { | |
reject("PDF not found."); | |
} | |
} else { | |
reject("Error loading page."); | |
} | |
}, | |
onerror: function() { | |
reject("Request failed."); | |
} | |
}); | |
}); | |
} | |
(function() { | |
'use strict'; | |
var doi = getDOI(); | |
console.log(`Found DOI: ${doi}`); | |
if (doi) { | |
getPDFUrl(doi).then(pdfUrl => { | |
console.log(`Found PDF: ${pdfUrl}`); | |
window.open(pdfUrl, "_blank"); | |
}).catch(error => { | |
console.error(error); | |
alert("PDF not found on Sci-Hub :("); | |
}); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment