Greasy Fork

Greasy Fork is available in English.

Google Scholar to free PDFs

Adds Sci-Hub, LibGen, LibSTC and Anna's Archive buttons to Google Scholar results

当前为 2025-05-29 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==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
// ==/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]', '<b>[PDF]</b>');
    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 = '<div class="gs_ggsd"><div class="gs_or_ggsm"></div></div>';
            result.insertBefore(div, result.firstChild);
            buttonContainer = div.querySelector('.gs_or_ggsm');
        }

        if (buttonContainer.classList.contains('scihub-processed')) return;
        buttonContainer.classList.add('scihub-processed');

        // Hàng 1: Sci-Hub + LibGen
        const row1 = document.createElement('span');
        row1.style.display = 'inline-flex';
        row1.style.gap = '6px';

        const scihubSpan = addLoadingIndicator(row1);
        const libgenSpan = addLoadingIndicator(row1);
        buttonContainer.appendChild(row1);

        // Hàng 2: Anna + SciNet
        const row2 = document.createElement('span');
        row2.style.display = 'inline-flex';
        row2.style.gap = '6px';

        const annaSpan = addLoadingIndicator(row2);
        const scinetSpan = addLoadingIndicator(row2);
        buttonContainer.appendChild(row2);

        // Hàng 3: LibSTC (riêng một hàng)
        const row3 = document.createElement('span');
        row3.style.display = 'flex';  // flex thì tự động block
        row3.style.gap = '6px';
        row3.style.marginTop = '4px';

        const libstcSpan = addLoadingIndicator(row3);
        buttonContainer.appendChild(row3);


        // Kiểm tra PDF và cập nhật nút
        checkSciHubPDF(titleLink.href, scihubSpan);
        checkLibGenPDF(titleLink.textContent, libgenSpan);

        fetchDOI(titleLink, (doi) => {
            if (doi) {
                checkAnnaPDF(doi, annaSpan);
                checkSciNetPDF(doi, scinetSpan);
                checkLibSTCPDF(doi, libstcSpan);
            } else {
                updateLink(annaSpan, '[No] Anna', '#', true);
                updateLink(scinetSpan, '[No] Sci-net', '#', true);
                updateLink(libstcSpan, '[No] LibSTC', '#', true);
            }
        });
    });
}


addButtons();

new MutationObserver((mutations) => {
    mutations.forEach((mutation) => mutation.addedNodes.length && addButtons());
}).observe(document.body, {childList: true, subtree: true});