arXiv downloaded PDF files, automatically renames them to paper titles

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 2023-12-17].

// ==UserScript==
// @name         arXiv下载PDF文件自动重命名为论文名
// @name:en    arXiv downloaded PDF files, automatically renames them to paper titles
// @namespace    Tampermonkey
// @version      0.1.1
// @description  能够直接把下载的文件名默认修改为论文名,并且符合Windows文件命名规范。会在搜索页面以及单个论文界面产生一个下载论文的超链接,点击即可。[2023-12-17修改版]
// @description:en 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 2023-12-17].
// @author       Aspen138
// @match        *://arxiv.org/abs/*
// @match        *://arxiv.org/search/*
// @icon         https://arxiv.org/favicon.ico
// @grant        none
// @license      AGPL License
// ==/UserScript==


//原作者的CSS选择器有点问题。
//建议装上SelectorGadget浏览器扩展,直接边调试边改代码。



(function() {
    'use strict';
    const url = location.pathname,webTitle = document.title
    var downloadName = '',downloadPath = ''
    var papertitle = '',papertime = ''
    if(url.search('/abs/')!=-1){
        papertitle = document.querySelector("#abs > h1").innerText
        downloadPath = document.querySelector(".download-pdf")
        papertime = document.querySelector(".dateline").innerText.replace("[Submitted on ","").replace("]","")
        downloadName = renamePaperFile(papertitle,papertime)
        addDownloadButton(downloadPath,downloadName,document.querySelector("#abs-outer > div.extra-services > div.full-text"))
    }
    if(url.search('/search/')!=-1){
        var paperlist = document.querySelectorAll("#main-container > div.content > ol > li")
        for(let paper in paperlist){
            papertitle = paperlist[paper].children[1].innerText
            papertime = paperlist[paper].querySelector("p.is-size-7").innerText.split(";")[0].replace("Submitted ","")
            downloadName = renamePaperFile(papertitle,papertime)
            downloadPath = paperlist[paper].children[0].children[0].children[1].children[0].href+'.pdf'
            addDownloadButton(downloadPath,downloadName,paperlist[paper].children[0])
        }
    }

    function addDownloadButton(downloadPath,downloadName,element){
        var button = document.createElement("a"); //创建一个input对象(提示框按钮)
        button.id = "downloadPaper";
        button.textContent = "下载论文(重命名)";
        button.setAttribute("href", downloadPath)
        button.setAttribute("download", downloadName)
        element.append(button);
    }
    function renamePaperFile(name,time){
        var downloadName = name.replace(': ',':')
        downloadName = downloadName.replace(':',':')
        downloadName = downloadName.replace('?','?')
        downloadName = downloadName.replace('/',' OR ')
        downloadName = downloadName.replace('"','“')+'.pdf'
        return '['+time+']'+downloadName
    }
})();