Greasy Fork

Greasy Fork is available in English.

Simple Google Scholar Sci-Hub Access

Adds Sci-Hub buttons to Google Scholar results and handles DOI links

当前为 2025-01-27 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==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
// ==/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 = '<div class="gs_ggsd"><div class="gs_or_ggsm"></div></div>';
            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
});