ScienceDirect下载

避免跳转在线pdf,可直接下载ScienceDirect文献到本地,支持自定义文件名

当前为 2022-10-13 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name ScienceDirect Download
  3. // @name:zh-CN ScienceDirect下载
  4. // @namespace tampermonkey.com
  5. // @version 3.1.1
  6. // @license MIT
  7. // @description Avoid jumping to online pdf,and directly download ScienceDirect literature to local,Support custom file names.
  8. // @description:zh-CN 避免跳转在线pdf,可直接下载ScienceDirect文献到本地,支持自定义文件名
  9. // @match *://www.sciencedirect.com/*
  10. // @match *://pdf.sciencedirectassets.com/*
  11. // @match *://sci-hub.ee/*
  12. // @grant GM_setValue
  13. // @grant GM_getValue
  14. // @grant GM.xmlHttpRequest
  15. // @grant GM_registerMenuCommand
  16. // @connect sciencedirectassets.com
  17. // @connect sci-hub.ee
  18. // @connect bban.top
  19. // @run-at document-start
  20. // ==/UserScript==
  21.  
  22. // global letiables
  23. // const defaultBaseURLs = ['http://sci-hub.ren', 'https://sci-hub.se', 'https://sci-hub.ee', 'https://sci-hub.shop', 'https://sci-hub.ren', 'https://sci-hub.st'];
  24. // let defaultBaseURL = defaultBaseURLs[Math.floor(Math.random() * defaultBaseURLs.length)];
  25. let defaultBaseURL = 'https://sci-hub.ee';
  26.  
  27. // Initialize configuration page
  28.  
  29. function getBlob(url, cb) {
  30. GM.xmlHttpRequest({
  31. method: "GET",
  32. url: url,
  33. responseType: 'blob',
  34. onload: function (response) {
  35. cb(response.response);
  36. }
  37. })
  38. }
  39.  
  40. function saveAs(blob, filename) {
  41. if (window.navigator.msSaveOrOpenBlob) {
  42. navigator.msSaveBlob(blob, filename);
  43. } else {
  44. let link = document.createElement('a');
  45. let body = document.querySelector('body');
  46. console.log(blob)
  47. link.href = window.URL.createObjectURL(blob);
  48. link.download = filename;
  49. // fix Firefox
  50. link.style.display = 'none';
  51. body.appendChild(link);
  52. link.click();
  53. body.removeChild(link);
  54. window.URL.revokeObjectURL(link.href);
  55. }
  56. }
  57.  
  58. function download(url, filename) {
  59. getBlob(url, function (blob) {
  60. saveAs(blob, filename);
  61. });
  62. }
  63. (function () {
  64. 'use strict';
  65. if (GM_getValue('userDefinedBaseURL') == null) {
  66. GM_setValue('userDefinedBaseURL', defaultBaseURL)
  67. }
  68. let userDefinedBaseURL = ''
  69. GM_registerMenuCommand(`Customize your scihub address`, () => {
  70. userDefinedBaseURL = prompt("scihub address", defaultBaseURL);
  71. if (userDefinedBaseURL) {
  72. GM_setValue('userDefinedBaseURL', userDefinedBaseURL);
  73. location.reload();
  74. }
  75. });
  76. let domain = document.domain;
  77. if (domain == 'pdf.sciencedirectassets.com') {
  78. let url = document.URL + '&download=true';
  79. console.log(url);
  80. let title = document.URL.split("/")[5].split("-")[2];
  81. try {
  82. let id = document.URL.split("/")[5].split("-")[2]
  83. title = GM_getValue(id)
  84. } catch (err) {
  85. console.log("err_message" + err.message);
  86. }
  87. // let html_url = "https://www.sciencedirect.com/science/article/pii/" + document.URL.split("/")[5].split("-")[2]
  88. let ret = prompt('Type your filename and click confirm to download!', title);
  89. if (ret !== null && ret != '') {
  90. let filename = ret + '.pdf';
  91. download(url, filename);
  92. }
  93. }
  94. if (domain == 'sci-hub.ee') {
  95. let doi2 = document.title.split(' | ')[document.title.split(' | ').length - 1]
  96. try { doi2 = doi2.replace('(', '%2528').replace(')', '%2529') }
  97. catch (err) { }
  98. let url2 = "https://sci.bban.top/pdf/" + doi2 + ".pdf?download=true";
  99. console.log(url2);
  100. let title2 = document.title.split('Sci-Hub | ')[1].replace(' | ', ' _ ');
  101. let ret2 = prompt('Type your filename and click confirm to download!', title2);
  102. if (ret2 !== null && ret2 != '') {
  103. let filename2 = ret2 + '.pdf';
  104. download(url2, filename2);
  105. }
  106. }
  107. if (domain == 'www.sciencedirect.com') {
  108. document.addEventListener('DOMContentLoaded', (event) => {
  109. console.log('DOM加载完成.');
  110. let linkid = document.head.getElementsByTagName('meta')[0].content;
  111. let titile = document.title.replace(' - ScienceDirect', '');
  112. GM_setValue(linkid, titile);
  113. let access = document.querySelector("#mathjax-container > div.sticky-outer-wrapper > div > div.accessbar > ul > li:nth-child(1) > a").href.split('login')[1];
  114. let doi = document.getElementsByClassName('doi')[0].href.split('org')[1];
  115. GM_setValue('access', access);
  116. let types = 'download';
  117. if (GM_getValue('access')) {
  118. userDefinedBaseURL = GM_getValue('userDefinedBaseURL');
  119. new_url = userDefinedBaseURL + doi;
  120. types = 'scihub';
  121. } else {
  122. let new_url = "https://www.sciencedirect.com/science/article/pii/" + linkid + "/pdfft?isDTMRedir=true"
  123. };
  124. let Container = document.createElement('div');
  125. let s = window.screen.width / 1920;
  126. let left = "250px";
  127. let top = "28px";
  128. if (s < 0.5) {
  129. left = (100 * s).toString() + "px";
  130. top = (18 + 10 / s).toString() + "px";
  131. }
  132. console.log(left);
  133. Container.id = "sp-ac-container";
  134. Container.style.position = "fixed";
  135. Container.style.left = left;
  136. Container.style.top = top;
  137. Container.style['z-index'] = "2";
  138. Container.innerHTML = `<button title="Click to download" class="button1" onclick="window.open('${new_url}')">${types}</button>
  139. <style>
  140. .button1 {
  141. -webkit-transition-duration: 0.4s;
  142. transition-duration: 0.4s;
  143. padding: 1.5px 6px;
  144. text-align: center;
  145. background-color: #f5f5f5;
  146. color: rgb(243, 109, 33);
  147. border: 0.5px rgb(134, 218, 209);
  148. border-radius: 9px;
  149. font-family: NexusSans,Arial,Helvetica,Lucida Sans Unicode,Microsoft Sans Serif,Segoe UI Symbol,STIXGeneral,Cambria Math,Arial Unicode MS,sans-serif!important;
  150. }
  151. .button1:hover {
  152. background-color: rgb(134, 218, 209);;;
  153. color: red;
  154. }
  155. </style>`;
  156. document.body.appendChild(Container);
  157.  
  158. });
  159. }
  160. })();