// ==UserScript==
// @name Simple Google Scholar Sci-Hub Access
// @namespace SimpleScholarAccess
// @version 1.0.0
// @description Adds Sci-Hub buttons to Google Scholar results and handles DOI links
// @author Bui Quoc Dung
// @include https://scholar.google.*/scholar?*
// @match *://*/*
// @license AGPL-3.0-or-later
// @downloadURL none
// ==/UserScript==
const SCIHUB_URL = 'https://sci-hub.ru/';
const LIBGEN_URL = 'https://libgen.li/index.php?req=';
function addButtons() {
const results = document.querySelectorAll('#gs_res_ccl_mid .gs_r.gs_or.gs_scl');
results.forEach(result => {
const titleLink = result.querySelector('.gs_rt a');
let buttonContainer = result.querySelector('.gs_or_ggsm');
if (!buttonContainer) {
const div = document.createElement('div');
div.className = 'gs_ggs gs_fl';
div.innerHTML = '
';
result.insertBefore(div, result.firstChild);
buttonContainer = div.querySelector('.gs_or_ggsm');
const sciHubLink = document.createElement('a');
sciHubLink.textContent = 'Sci-Hub';
sciHubLink.addEventListener('click', () => window.open(SCIHUB_URL + titleLink.href));
buttonContainer.appendChild(sciHubLink);
const libGenLink = document.createElement('a');
libGenLink.textContent = ' LibGen';
libGenLink.addEventListener('click', () => window.open(LIBGEN_URL + encodeURIComponent(titleLink.textContent)));
buttonContainer.appendChild(libGenLink);
}
});
}
function convertDOIToSciHub(doiURL) {
return `${SCIHUB_URL}${doiURL.replace('https://doi.org/', '')}`;
}
function handleDOILinks() {
document.querySelectorAll('a[href^="http://dx.doi.org/"], a[href^="https://doi.org/"]').forEach(link => {
link.addEventListener('click', (event) => {
event.preventDefault();
window.location.href = convertDOIToSciHub(link.href);
});
});
}
addButtons();
handleDOILinks();
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (mutation.addedNodes.length) {
addButtons();
handleDOILinks();
}
});
});
observer.observe(document.body, {
childList: true,
subtree: true
});