Arxiv Tiny Tool

add some userful link while open arxiv website and replace page title with paper title in arxiv pdf

  1. // ==UserScript==
  2. // @name Arxiv Tiny Tool
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.1
  5. // @description add some userful link while open arxiv website and replace page title with paper title in arxiv pdf
  6. // @description:zh-CN 在打arxiv页面添加了一些有用的链接,用论文的标题替换arxiv pdf 页面的标题
  7. // @author hiboy & ChatGPT
  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. function insertLink(link, text){
  117. let ul = document.querySelector('div.full-text ul');
  118.  
  119. if (ul){
  120. // Insert cnPDF download link after the PDF download link
  121. const li = document.createElement("li");
  122. const a = document.createElement("a");
  123. a.href = link;
  124. a.textContent = text;
  125. li.appendChild(a);
  126. ul.insertBefore(li, ul.children[2]);
  127. }
  128. }
  129.  
  130. function addCnPdfLink() {
  131. // Get the PDF download link
  132. // const pdfLink = document.querySelector('.abs-button.download-pdf');
  133. let a = document.querySelector('div.full-text ul li a.abs-button');
  134. const line = document.createTextNode(" | ");
  135. a.textContent = 'PDF';
  136. const a1 = a.cloneNode();
  137. a1.textContent = '国内';
  138. a1.href = `http://xxx.itp.ac.cn/pdf/${paperId}.pdf`
  139. a.parentNode.appendChild(line);
  140. a.parentNode.appendChild(a1);
  141. console.log("[arxiv-cnPDF] Link added successfully");
  142. }
  143.  
  144. function addHtml5Link() {
  145. const newLink = window.location.href.replace('arxiv','ar5iv');
  146. insertLink(newLink, 'html5');
  147. }
  148.  
  149. function addDownloadLink() {
  150. // Get the PDF download link
  151. var ul = document.querySelector('.full-text ul');
  152. if (ul){
  153. const title = document.getElementsByClassName("title mathjax")[0].innerText;
  154. const yearMeta = document.querySelector('meta[name="citation_date"]');
  155. const year = yearMeta.getAttribute('content').split('/')[0];
  156. const filename = title + " " + year + ".pdf";
  157. var download_url = window.location.href.replace('/abs/','/pdf/');
  158. if (!download_url.endsWith('.pdf')){
  159. download_url = download_url + '.pdf';
  160. }
  161. var link = document.createElement("a");
  162. link.id = "rename_download";
  163. link.textContent = "Download";
  164. link.href = download_url
  165. link.download = filename;
  166.  
  167. var li = document.createElement("li");
  168. li.append(link);
  169. ul.insertBefore(li, ul.children[2]);
  170. console.log("[arxiv-download] Link added successfully");
  171. }
  172. }
  173.  
  174. console.log('Script started');
  175. if (window.location.href.indexOf('/pdf/') !== -1) {
  176. createArxivButton();
  177. modifyTitle();
  178. } else if (window.location.href.indexOf('/abs/') !== -1) {
  179. // openPdfInNewTab();
  180. addDownloadLink();
  181. addHtml5Link();
  182. addCnPdfLink();
  183.  
  184. }
  185.  
  186. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址