Readpaper Helper

Readpaper website enhancements

目前為 2024-08-26 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name Readpaper Helper
  3. // @namespace Terrasse
  4. // @version 1.1.0
  5. // @description Readpaper website enhancements
  6. // @author You
  7. // @match https://readpaper.com/paper/*
  8. // @icon https://www.google.com/s2/favicons?sz=64&domain=readpaper.com
  9. // @require https://code.jquery.com/jquery-3.7.0.min.js
  10. // @grant GM_setValue
  11. // @grant GM_getValue
  12. // @grant GM_download
  13. // @grant GM_openInTab
  14. // @run-at document-idle
  15. // ==/UserScript==
  16.  
  17. setTimeout(function () {
  18. 'use strict';
  19. var title = $('h1').text().trim();
  20. var info_row = $('.info-row');
  21.  
  22. // Google Scholar Button
  23. var scholar_link = `https://scholar.google.com/scholar?q=${title}`;
  24. var scholar_item = `
  25. <div class="share" id="rh_scholar"><span style="display:flex;align-items:center;">
  26. <i aria-hidden="true" class="aiknowledge-icon icon-chrome"></i>
  27. <a href="${scholar_link}">Scholar</a>
  28. </span></div>`;
  29. scholar_item = $(scholar_item);
  30. scholar_item.css('color', '#1f71e0').css('margin-left', '12px');
  31. info_row.append(scholar_item);
  32.  
  33. // Direct PDF Button
  34. var pdf_item = `
  35. <div class="share" id="rh_pdf"><span style="display:flex;align-items:center;">
  36. <i aria-hidden="true" class="aiknowledge-icon icon-file-pdf-fill"></i>
  37. <a href="#">Fetch PDF</a>
  38. </span></div>`;
  39. pdf_item = $(pdf_item);
  40. pdf_item.css('color', '#1f71e0').css('margin-left', '12px');
  41.  
  42. pdf_item.click(function () {
  43. var apiKey = GM_getValue('rh_key', null);
  44. if (apiKey == null || apiKey.length != 40) {
  45. apiKey = prompt('Enter API Key:');
  46. if (apiKey.length != 40) {
  47. alert('Invalid API Key');
  48. return;
  49. }
  50. GM_setValue('rh_key', apiKey);
  51. }
  52.  
  53. var settings = {
  54. "url": "https://google.serper.dev/scholar",
  55. "method": "POST",
  56. "timeout": 0,
  57. "headers": {
  58. "X-API-KEY": apiKey,
  59. "Content-Type": "application/json"
  60. },
  61. "data": JSON.stringify({
  62. "q": title,
  63. }),
  64. };
  65.  
  66. $.ajax(settings).done(function (response) {
  67. console.log(response);
  68.  
  69. try {
  70. var link = response.organic[0].link;
  71. if (link.indexOf('.pdf') != -1) {
  72. console.log(`direct link: ${link}`);
  73. } else if (link.indexOf('/abs/') != -1) {
  74. console.log(`redirecting rule '/abs/': ${link}`);
  75. link = link.replace('/abs/', '/pdf/');
  76. } else {
  77. console.log(`unknown link type: ${link}`);
  78. GM_openInTab(link, {
  79. active: true,
  80. insert: true,
  81. setParent: true
  82. });
  83. return;
  84. }
  85. GM_download({
  86. url: link,
  87. name: title + '.pdf',
  88. saveAs: true
  89. });
  90. } catch (error) {
  91. alert(`Failed to fetch PDF: ${error}`);
  92. }
  93. });
  94. });
  95.  
  96. info_row.append(pdf_item);
  97. }, 1000);