Google Scholar BibTeX Auto-Copy Pro

拦截BibTeX请求直接复制内容,无需页面跳转

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Google Scholar BibTeX Auto-Copy Pro
// @namespace    http://tampermonkey.net/
// @version      1.2
// @description  拦截BibTeX请求直接复制内容,无需页面跳转
// @match        https://scholar.google.com/scholar*
// @grant        GM_setClipboard
// @grant        GM_xmlhttpRequest
// @connect      scholar.googleusercontent.com
// @description   [自动复制BibTeX并关闭弹窗,无需页面跳转]
// @author        BIT-ljf
// @license       MIT
// @icon          https://scholar.google.com/favicon.ico
// @supportURL    https://github.com/yourname/repo/issues
// ==/UserScript==

(function() {
    'use strict';

    // 拦截BibTeX链接点击事件
    document.addEventListener('click', function(e) {
        const bibtexLink = e.target.closest('a[href*="/scholar.bib?"]');
        if (bibtexLink) {
            e.preventDefault();
            e.stopPropagation();

            // 通过AJAX获取BibTeX内容
            GM_xmlhttpRequest({
                method: "GET",
                url: bibtexLink.href,
                onload: function(res) {
                    if (res.status === 200) {
                        GM_setClipboard(res.responseText);
                        closeModal(); // 关闭引用弹窗
                    }
                }
            });
        }
    });

    // 关闭弹窗的函数
    function closeModal() {
        const closeBtn = document.querySelector('div#gs_cit div[aria-label="关闭"]');
        if (closeBtn) closeBtn.click();
        else window.history.back(); // 备用返回
    }

    // 处理直接打开BibTeX页面的情况(如手动刷新)
    if (window.location.href.includes('/scholar.bib?')) {
        GM_xmlhttpRequest({
            method: "GET",
            url: window.location.href,
            onload: function(res) {
                if (res.status === 200) {
                    GM_setClipboard(res.responseText);
                    window.close(); // 关闭当前标签页(如果是从新标签页打开)
                }
            }
        });
    }
})();