Semantic Scholar Citation Badge Generator

自动生成Semantic Scholar上一篇文章的引用数牌,可以粘贴到md文档中使用。生成按钮在我文章界面左上角。

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         Semantic Scholar Citation Badge Generator
// @namespace    http://tampermonkey.net/
// @version      2.0
// @description  自动生成Semantic Scholar上一篇文章的引用数牌,可以粘贴到md文档中使用。生成按钮在我文章界面左上角。
// @match        https://www.semanticscholar.org/*
// @grant        GM_setClipboard
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    // 检查当前页面是否为指定的文章页面
    const isPaperPage = /^https:\/\/www\.semanticscholar\.org\/paper\/.+\/[0-9a-f]+$/.test(window.location.href);

    // 如果不是指定的文章页面,则退出
    if (!isPaperPage) {
        return;
    }

    // 获取当前页面 URL 的最后一段字符串
    const urlLastPart = window.location.pathname.split('/').pop();


    // 生成 API URL
    const apiUrl = `[![](https://img.shields.io/badge/dynamic/json?label=citation&query=citationCount&url=https://api.semanticscholar.org/graph/v1/paper/${urlLastPart}?fields=citationCount)](https://www.semanticscholar.org/paper/${urlLastPart})`;

    // 创建要保存到剪贴板的字符串
    const clipboardStr = apiUrl;

    // 添加按钮到页面
    const button = document.createElement('button');
    button.textContent = 'Generate Citation Badge';
    button.style.position = 'fixed';
    button.style.top = '100px';
    button.style.left = '20px';
    button.addEventListener('click', () => {
        GM_setClipboard(clipboardStr);
        button.textContent = 'Citation Badge Copied!';
        window.setTimeout(() => {
            button.textContent = 'Generate Citation Badge';
        }, 1000); // 1秒钟后将按钮文本更改回 "Generate Citation Badge"
    });

    // 将按钮添加到页面
    document.body.appendChild(button);
})();