Arxiv Tiny Tool

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

目前为 2023-05-15 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name Arxiv Tiny Tool
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.0
  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.  
  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.  
  146. function addDownloadLink() {
  147. // Get the PDF download link
  148. var pdfLink = document.querySelector('.full-text ul li');
  149. if (pdfLink){
  150. const title = document.getElementsByClassName("title mathjax")[0].innerText;
  151. const yearMeta = document.querySelector('meta[name="citation_date"]');
  152. const year = yearMeta.getAttribute('content').split('/')[0];
  153. const filename = title + " " + year + ".pdf";
  154. var download_url = window.location.href.replace('/abs/','/pdf/');
  155. if (!download_url.endsWith('.pdf')){
  156. download_url = download_url + '.pdf';
  157. }
  158. var link = document.createElement("a");
  159. link.id = "rename_download";
  160. link.textContent = "Download";
  161. link.setAttribute("href", download_url);
  162. link.setAttribute("download", filename);
  163.  
  164. var li = document.createElement("li");
  165. li.append(link);
  166. pdfLink.append(li);
  167. console.log("[arxiv-download] Link added successfully");
  168. }
  169. }
  170.  
  171. console.log('Script started');
  172. if (window.location.href.indexOf('/pdf/') !== -1) {
  173. createArxivButton();
  174. modifyTitle();
  175. } else if (window.location.href.indexOf('/abs/') !== -1) {
  176. openPdfInNewTab();
  177. addDownloadLink();
  178. addCnPdfLink();
  179. addHtml5Link();
  180. }
  181.  
  182. })();
  183.