// ==UserScript== // @name Google Scholar to free PDFs // @namespace ScholarToSciHub // @version 1.11 // @description Adds Sci-Hub, LibGen, LibSTC and Anna's Archive buttons to Google Scholar results // @author Bui Quoc Dung // @match https://scholar.google.*/* // @license AGPL-3.0-or-later // @grant GM_xmlhttpRequest // @downloadURL none // ==/UserScript== const SCIHUB_URL = 'https://www.tesble.com/'; const LIBGEN_URL = 'https://libgen.li/index.php?req='; const ANNA_URL = 'https://annas-archive.org/scidb/'; const ANNA_CHECK_URL = 'https://annas-archive.org/search?index=journals&q='; const LIBSTC_BASE_URL = 'https://hub.libstc.cc/'; const SCINET_URL = 'https://sci-net.xyz/'; const DOI_REGEX = /\b(10\.\d{4,}(?:\.\d+)*\/(?:(?!["&'<>])\S)+)\b/gi; function updateLink(span, textContent, href, isNo = false) { const link = document.createElement('a'); link.textContent = textContent; link.href = href; link.target = '_blank'; link.rel = 'noreferrer noopener'; link.style.fontSize = '15px'; if (isNo) link.style.color = 'gray'; link.innerHTML = textContent.replace('[PDF]', '[PDF]'); span.replaceWith(link); } function fetchDOI(titleLink, callback) { GM_xmlhttpRequest({ method: 'GET', url: titleLink.href, onload: res => { const matches = res.responseText.match(DOI_REGEX); callback(matches && matches.length ? matches[0] : null); }, onerror: () => callback(null) }); } function addLoadingIndicator(container) { const span = document.createElement('div'); span.textContent = 'Loading...'; span.style.marginBottom = '4px'; span.style.color = 'gray'; span.style.fontSize = '15px'; container.appendChild(span); return span; } function checkLibGenPDF(title, span) { GM_xmlhttpRequest({ method: 'GET', url: LIBGEN_URL + encodeURIComponent(title), onload: res => { const doc = new DOMParser().parseFromString(res.responseText, 'text/html'); const hasPDF = !!doc.querySelector('.table.table-striped'); updateLink(span, hasPDF ? '[PDF] LibGen' : '[No] LibGen', LIBGEN_URL + encodeURIComponent(title), !hasPDF); }, onerror: () => updateLink(span, '[No] LibGen', LIBGEN_URL + encodeURIComponent(title), true) }); } function checkSciHubPDF(url, span) { GM_xmlhttpRequest({ method: 'GET', url: SCIHUB_URL + url, onload: res => { const hasPDF = /iframe|pdf|embed/i.test(res.responseText); updateLink(span, hasPDF ? '[PDF] Sci-Hub' : '[No] Sci-Hub', SCIHUB_URL + url, !hasPDF); }, onerror: () => updateLink(span, '[No] Sci-Hub', SCIHUB_URL + url, true) }); } function checkAnnaPDF(doi, span) { const checkUrl = ANNA_CHECK_URL + encodeURIComponent(doi); GM_xmlhttpRequest({ method: 'GET', url: checkUrl, onload: res => { const doc = new DOMParser().parseFromString(res.responseText, 'text/html'); const hasPDF = !!doc.querySelector('.mt-4.uppercase.text-xs.text-gray-500'); updateLink(span, hasPDF ? '[PDF] Anna' : '[No] Anna', ANNA_URL + doi, !hasPDF); }, onerror: () => updateLink(span, '[No] Anna', ANNA_URL + doi, true) }); } function checkLibSTCPDF(doi, span) { const url = LIBSTC_BASE_URL + doi + '.pdf'; GM_xmlhttpRequest({ method: 'HEAD', url: url, onload: res => { const isPDF = res.status === 200 && res.responseHeaders.toLowerCase().includes('application/pdf'); updateLink(span, isPDF ? '[PDF] LibSTC' : '[No] LibSTC', url, !isPDF); }, onerror: () => updateLink(span, '[No] LibSTC', url, true) }); } function checkSciNetPDF(doi, span) { const sciNetUrl = SCINET_URL + doi; GM_xmlhttpRequest({ method: 'GET', url: sciNetUrl, onload: function(response) { const hasPDF = /iframe|pdf|embed/.test(response.responseText); updateLink(span, hasPDF ? '[PDF] Sci-net' : '[No] Sci-net', sciNetUrl, !hasPDF); }, onerror: function() { updateLink(span, '[No] Sci-net', sciNetUrl, true); } }); } function addButtons() { document.querySelectorAll('#gs_res_ccl_mid .gs_r.gs_or.gs_scl').forEach(result => { const titleLink = result.querySelector('.gs_rt a'); const yearElement = result.querySelector('.gs_a'); if (!titleLink || !yearElement) return; let buttonContainer = result.querySelector('.gs_or_ggsm'); if (!buttonContainer) { const div = document.createElement('div'); div.className = 'gs_ggs gs_fl'; div.innerHTML = '