arXiv Download Rename

It can directly modify the downloaded file name to the thesis name by default and conform to the Windows file naming standard. Will generate a hyperlink to download the paper on the search page as well as the individual paper screen, click on it. [modified version 2024-5-24].

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         arXiv Download Rename
// @name:en    arXiv downloaded PDF files, automatically renames them to paper titles
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  It can directly modify the downloaded file name to the thesis name by default and conform to the Windows file naming standard. Will generate a hyperlink to download the paper on the search page as well as the individual paper screen, click on it. [modified version 2024-5-24].
// @author       TeruhaHirayama
// @match        https://arxiv.org/abs/*
// @grant        none
// @license      AGPL License
// ==/UserScript==

(function() {
    'use strict';

    // 检查当前页面是否为论文摘要页面
    if (window.location.pathname.includes('/abs/')) {
        // 获取论文标题
        var papertitle = document.querySelector("#abs > h1").innerText.trim();
        // 获取论文发布时间
        var papertime = document.querySelector(".dateline").innerText.replace("[Submitted on ", "").replace("]", "").trim();
        // 获取作者列表
        var authors = document.querySelectorAll('.authors > a');
        var authorNames = Array.from(authors, author => author.innerText.trim());

        // 格式化作者名称,如果作者多于一个,则只保留第一个并添加 "et al."
        var formattedAuthors = authorNames.length > 1 ? authorNames[0] + ", et al." : authorNames.join(", ");

        var safeTitle = papertitle.replace(':', ':').replace('/', ' OR ').replace('"', '“');
        // 构建最终的文件名
        var downloadName = '[' + papertime + '] ' + formattedAuthors + ' - ' + safeTitle + '.pdf';

        // 获取PDF下载链接
        var downloadPath = document.querySelector(".download-pdf").href;

        // 添加一个下载按钮
        addDownloadButton(downloadPath, downloadName);
    }

    // 添加下载按钮的函数
    function addDownloadButton(downloadPath, downloadName) {
        var button = document.createElement("a");
        button.id = "downloadPaper";
        button.textContent = "下载论文(重命名)";
        button.style.display = 'block'; // 可选,为了确保按钮可见
        button.href = downloadPath;
        button.download = downloadName;
        button.addEventListener('click', function(event) {
            event.preventDefault();
            fetchPDF(downloadPath, downloadName);
        });
        document.querySelector("#abs-outer > div.extra-services > div.full-text").appendChild(button);
    }

    // 下载PDF文件的函数
    function fetchPDF(url, filename) {
        fetch(url)
            .then(response => response.blob())
            .then(blob => {
                const objUrl = URL.createObjectURL(blob);
                const a = document.createElement('a');
                a.href = objUrl;
                a.download = filename;
                document.body.appendChild(a);
                a.click();
                document.body.removeChild(a);
                URL.revokeObjectURL(objUrl);
            })
            .catch(error => console.error('Error fetching PDF:', error));
    }
})();