also an Arxiv Tiny Tool

基于Arxiv Tiny Tool 修改. 使用blob下载以自动重命名.

  1. // ==UserScript==
  2. // @name also an Arxiv Tiny Tool
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.0
  5. // @description Based on Arxiv Tiny Tool. Fix download and rename function through downloading with blob.
  6. // @description:zh-CN 基于Arxiv Tiny Tool 修改. 使用blob下载以自动重命名.
  7. // @author hiboy & ChatGPT & Chris
  8. // @match *://arxiv.org/*
  9. // @match *://cn.arxiv.org/*
  10. // @match *://xxx.itp.ac.cn/*
  11. // @icon https://t2.gstatic.com/faviconV2?client=SOCIAL&type=FAVICON&fallback_opts=TYPE,SIZE,URL&url=http://arxiv.org&size=64
  12. // @grant GM_xmlhttpRequest
  13. // @grant GM_addStyle
  14. // @license MIT
  15. // ==/UserScript==
  16.  
  17. (function() {
  18. 'use strict';
  19. var newTitle = '';
  20. const paperId = window.location.pathname.match(/\/(\d+\.\d+)/)[1];
  21.  
  22. function getCurrentTime() {
  23. const now = new Date();
  24. const hour = now.getHours().toString().padStart(2, '0');
  25. const minute = now.getMinutes().toString().padStart(2, '0');
  26. const second = now.getSeconds().toString().padStart(2, '0');
  27. const millisecond = now.getMilliseconds().toString().padStart(3, '0');
  28. return `${hour}:${minute}:${second}.${millisecond}`;
  29. }
  30.  
  31.  
  32. function modifyTitle() {
  33.  
  34. var apiUrl = 'https://export.arxiv.org/api/query?id_list=' + paperId;
  35. var maxUpdateMinute = 5;
  36.  
  37. // 定时器,初始时间间隔
  38. let intervalTime = 1000;
  39.  
  40. let intervalId = setInterval(updateTabTitle, intervalTime);
  41. function updateTabTitle() {
  42.  
  43. if (!newTitle) {
  44. console.log('Fetching paper title for ' + paperId);
  45. GM_xmlhttpRequest({
  46. method: 'GET',
  47. url: apiUrl,
  48. onload: function(response) {
  49. var parser = new DOMParser();
  50. var xmlDoc = parser.parseFromString(response.responseText, 'text/xml');
  51. var title = xmlDoc.querySelector('entry>title').textContent;
  52. newTitle = '[' + paperId + '] ' + title;
  53. console.log(`[${getCurrentTime()}]`,'Title modified for ' + newTitle);
  54. }
  55. });
  56. } else {
  57. console.log(`[${getCurrentTime()}]`,'set title to', newTitle);
  58. document.title = '';
  59. document.title = newTitle;
  60. }
  61.  
  62. clearInterval(intervalId);
  63.  
  64. // 每执行一次翻倍间隔时间
  65. intervalTime *= 2;
  66. // 如果间隔时间在maxUpdateMinute分钟内,则设置新的定时器
  67. if (intervalTime < maxUpdateMinute * 60 * 1000) {
  68. intervalId = setInterval(updateTabTitle, intervalTime);
  69. }
  70. }
  71.  
  72. }
  73.  
  74. function createArxivButton() {
  75. var btn = document.createElement("button");
  76. btn.setAttribute('style', 'position:absolute;' +
  77. 'z-index:1000;' +
  78. 'right:135px;' +
  79. 'top:12px;' +
  80. 'height:36px;' +
  81. 'padding:5px 10px;' +
  82. 'background-color:#323639;' +
  83. 'border-radius:50%;' +
  84. 'border:none;' +
  85. 'color:#ffffff;' +
  86. 'font-size:16px;' +
  87. 'font-weight:500;' +
  88. 'text-transform:uppercase;' +
  89. 'letter-spacing:0.5px;' +
  90. 'text-align:center;' +
  91. 'vehical-align: middle;' +
  92. 'cursor:pointer;' +
  93. // 'box-shadow:0px 2px 4px rgba(0,0,0,0.25);' +
  94. 'transition:background-color 0.2s ease-in-out,box-shadow 0.2s ease-in-out;' +
  95. ':hover{background-color:#1b1d1f;rgba(0,0,0,0.5);}');
  96. btn.setAttribute('id', "btn");
  97. btn.innerText = "arixv";
  98. btn.onclick = function() {
  99. var home_url = `https://arxiv.org/abs/${paperId}`;
  100. window.open(home_url);
  101. };
  102. btn.onmouseover = function() {
  103. this.style.backgroundColor = "#424649";
  104. };
  105. btn.onmouseout = function() {
  106. this.style.backgroundColor = "#323639";
  107. };
  108. document.body.appendChild(btn);
  109.  
  110. }
  111. function openPdfInNewTab() {
  112. // assume the first link is pdf
  113. var pdfLink = document.querySelector('.full-text ul li a');
  114. pdfLink.target = "_blank";
  115. }
  116.  
  117. function addCnPdfLink() {
  118. // Get the PDF download link
  119. const pdfLink = document.querySelector('.abs-button.download-pdf');
  120.  
  121. if (pdfLink){
  122. // Construct cnPDF download link
  123. const cnPDFLink = `http://xxx.itp.ac.cn/pdf/${paperId}.pdf`;
  124.  
  125. // Insert cnPDF download link after the PDF download link
  126. const newLinkHTML = `<li><a href="${cnPDFLink}" class="abs-button download-cnPDF" target="_blank">PDF(cn)</a></li>`;
  127. pdfLink.insertAdjacentHTML('afterend', newLinkHTML);
  128. console.log("[arxiv-cnPDF] Link added successfully");
  129. }
  130. }
  131.  
  132. function addHtml5Link() {
  133. // Get the PDF download link
  134. var pdfLink = document.querySelector('.full-text ul li');
  135. if (pdfLink){
  136. // Construct HTML5 download link
  137. const newLink = window.location.href.replace('arxiv','ar5iv');
  138.  
  139. // Insert HTML5 download link before the PDF download link
  140. const newLinkHTML = `<li><a href="${newLink}" class="abs-button html5" target="_blank">HTML5</a></li>`;
  141. pdfLink.insertAdjacentHTML('beforebegin', newLinkHTML);
  142. console.log("[arxiv-HTML5] Link added successfully");
  143. }
  144. }
  145. // 下载url(解决跨域a.download不生效问题)
  146. function downloadFile(url, fileName) {
  147. const x = new XMLHttpRequest()
  148. x.open("GET", url, true)
  149. x.responseType = 'blob'
  150. x.onload = function(e) {
  151. const url = window.URL.createObjectURL(x.response)
  152. const a = document.createElement('a')
  153. a.href = url
  154. a.target = '_blank'
  155. a.download = fileName
  156. a.click()
  157. a.remove()
  158. }
  159. x.send()
  160. }
  161.  
  162. function addDownloadLink() {
  163. // Get the PDF download link
  164. var pdfLink = document.querySelector('.full-text ul li');
  165. if (pdfLink){
  166. const title = document.getElementsByClassName("title mathjax")[0].innerText;
  167. const yearMeta = document.querySelector('meta[name="citation_date"]');
  168. const year = yearMeta.getAttribute('content').split('/')[0];
  169. const filename = title + " " + paperId + ".pdf";
  170. var download_url = window.location.href.replace('/abs/','/pdf/');
  171. var link = document.createElement("button");
  172. link.id = "rename_download";
  173. link.textContent = "Download Renamed PDF";
  174. // link.setAttribute("href", download_url);
  175. // link.setAttribute("download", filename);
  176. link.onclick = function() {
  177. downloadFile(download_url, filename);
  178. };
  179.  
  180. var li = document.createElement("li");
  181. li.append(link);
  182. pdfLink.append(li);
  183. console.log("[arxiv-download] Link added successfully");
  184. }
  185. }
  186.  
  187. console.log('Script started');
  188. if (window.location.href.indexOf('/pdf/') !== -1) {
  189. createArxivButton();
  190. modifyTitle();
  191. } else if (window.location.href.indexOf('/abs/') !== -1) {
  192. openPdfInNewTab();
  193. addDownloadLink();
  194. addCnPdfLink();
  195. addHtml5Link();
  196. }
  197.  
  198. })();
  199.