ScienceDirect下载

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

当前为 2023-01-17 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name ScienceDirect Download
  3. // @name:zh-CN ScienceDirect下载
  4. // @namespace tampermonkey.com
  5. // @version 3.1.3
  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. // @match *://scholar.cnki.net/*
  13. // @grant GM_setValue
  14. // @grant GM_getValue
  15. // @grant GM.xmlHttpRequest
  16. // @grant GM_registerMenuCommand
  17. // @connect sciencedirectassets.com
  18. // @connect bban.top
  19. // @run-at document-start
  20. // ==/UserScript==
  21.  
  22. // global variables
  23. var defaultBaseURL = 'https://sci-hub.ee';
  24.  
  25. // Initialize configuration page
  26.  
  27. function getBlob(url, cb) {
  28. GM.xmlHttpRequest({
  29. method: "GET",
  30. url: url,
  31. responseType: 'blob',
  32. onload: function (response) {
  33. cb(response.response);
  34. }
  35. })
  36. }
  37.  
  38. function saveAs(blob, filename) {
  39. if (window.navigator.msSaveOrOpenBlob) {
  40. navigator.msSaveBlob(blob, filename);
  41. } else {
  42. let link = document.createElement('a');
  43. let body = document.querySelector('body');
  44. console.log(blob)
  45. link.href = window.URL.createObjectURL(blob);
  46. link.download = filename;
  47. // fix Firefox
  48. link.style.display = 'none';
  49. body.appendChild(link);
  50. link.click();
  51. body.removeChild(link);
  52. window.URL.revokeObjectURL(link.href);
  53. }
  54. }
  55.  
  56. function download(url, filename) {
  57. getBlob(url, function (blob) {
  58. saveAs(blob, filename);
  59. });
  60. }
  61.  
  62. function pdf_scihub_ee() {
  63. let doi = document.title.split(' | ')[document.title.split(' | ').length - 1]
  64. try { doi = doi.replace('(', '%2528').replace(')', '%2529') } catch (err) { }
  65. let title = document.title.split('Sci-Hub | ')[1].replace(' | ', ' _ ');
  66. let ret = prompt('Type your filename and click confirm to download!', title);
  67. let url = "https://sci.bban.top/pdf/" + doi + ".pdf?download=true"
  68. if (ret !== null && ret != '') {
  69. let filename = ret + '.pdf';
  70. download(url, filename);
  71. }
  72. }
  73.  
  74. function pdf_scidirect() {
  75. let url = document.URL + '&download=true';
  76. console.log(url);
  77. let title = document.URL.split("/")[5].split("-")[2];
  78. try {
  79. var id = document.URL.split("/")[5].split("-")[2]
  80. title = GM_getValue(id)
  81. } catch (err) {
  82. console.log("err_message" + err.message);
  83. }
  84. // var html_url = "https://www.sciencedirect.com/science/article/pii/" + document.URL.split("/")[5].split("-")[2]
  85. let ret = prompt('Type your filename and click confirm to download!', title);
  86. if (ret !== null && ret != '') {
  87. let filename = ret + '.pdf';
  88. download(url, filename);
  89. }
  90. }
  91.  
  92.  
  93. (function () {
  94. 'use strict';
  95. if (GM_getValue('userDefinedBaseURL') == null) {
  96. GM_setValue('userDefinedBaseURL', defaultBaseURL)
  97. }
  98. var userDefinedBaseURL = GM_getValue('userDefinedBaseURL')
  99. GM_registerMenuCommand(`Customize your scihub address`, () => {
  100. userDefinedBaseURL = prompt("customize scihub address,e.g.>>" + defaultBaseURL, defaultBaseURL);
  101. if (userDefinedBaseURL) {
  102. GM_setValue('userDefinedBaseURL', userDefinedBaseURL);
  103. location.reload();
  104. }
  105. });
  106. var domain = document.domain;
  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. let new_url = "https://www.sciencedirect.com/science/article/pii/" + linkid + "/pdfft?isDTMRedir=true"
  118. if (GM_getValue('access')) {
  119. userDefinedBaseURL = GM_getValue('userDefinedBaseURL');
  120. new_url = userDefinedBaseURL + doi;
  121. types = 'scihub'
  122. }
  123. let Container = document.createElement('div');
  124. let s = window.screen.width / 1920;
  125. let left = "250px";
  126. let top = "28px";
  127. if (s < 0.5) {
  128. left = (100 * s).toString() + "px";
  129. top = (18 + 10 / s).toString() + "px";
  130. }
  131. console.log(left);
  132. Container.id = "sp-ac-container";
  133. Container.style.position = "fixed";
  134. Container.style.left = left;
  135. Container.style.top = top;
  136. Container.style['z-index'] = "2";
  137. Container.innerHTML = `<button title="Click to download" class="button1" onclick="window.open('${new_url}')">${types}</button>
  138. <style>
  139. .button1 {
  140. -webkit-transition-duration: 0.4s;
  141. transition-duration: 0.4s;
  142. padding: 1.5px 6px;
  143. text-align: center;
  144. background-color: #f5f5f5;
  145. color: rgb(243, 109, 33);
  146. border: 0.5px rgb(134, 218, 209);
  147. border-radius: 9px;
  148. font-family: NexusSans,Arial,Helvetica,Lucida Sans Unicode,Microsoft Sans Serif,Segoe UI Symbol,STIXGeneral,Cambria Math,Arial Unicode MS,sans-serif!important;
  149. }
  150. .button1:hover {
  151. background-color: rgb(134, 218, 209);;;
  152. color: red;
  153. }
  154. </style>`;
  155. document.body.appendChild(Container);
  156.  
  157. });
  158. }
  159. if (domain == 'scholar.cnki.net') {
  160. window.onload = function () {
  161. if (document.URL.includes('/Detail/index/')) {
  162. let doi2 = document.querySelector("#__next > div > div.detail_detail-main__11Hij > div.detail_content__3IojM > div.detail_content-left__2vUAX > div > div.detail_doc__20q8z > div:nth-child(1) > div.detail_doc-doi__VX6o2.detail_doc-item__2l-2B").textContent.replace('DOI: ', '')
  163. let new_url2 = userDefinedBaseURL + '/' + doi2
  164. console.log(userDefinedBaseURL)
  165. let Container2 = document.createElement('p');
  166. Container2.style.position = "fixed";
  167. Container2.id = "sp-ac-container";
  168. Container2.style.top = "120px";
  169. Container2.style['z-index'] = "2";
  170. Container2.innerHTML = `<button title="Click to download" class="button1" onclick="window.open('${new_url2}')">scihub</button>
  171. <style>
  172. .button1 {
  173. -webkit-transition-duration: 0.4s;
  174. -webkit-text-size-adjust: 100%;
  175. transition-duration: 0.4s;
  176. width:80px;
  177. height:50px;
  178. padding: 1.5px 6px;
  179. text-align: center;
  180. background-color: #506698;
  181. color: white;
  182. border: 0.5px rgb(134, 218, 209);
  183. border-radius: 8px;
  184. font-family: NexusSans,Arial,Helvetica,Lucida Sans Unicode,Microsoft Sans Serif,Segoe UI Symbol,STIXGeneral,Cambria Math,Arial Unicode MS,sans-serif!important;
  185. }
  186. .button1:hover {
  187. background-color: rgb(134, 218, 209);;;
  188. color: rgb(243, 109, 33);
  189. }
  190. </style>`;
  191. document.getElementsByClassName('detail_detail-main__11Hij')[0].append(Container2)
  192. }
  193. }
  194. }
  195. if (domain == 'pdf.sciencedirectassets.com') {
  196. pdf_scidirect()
  197. }
  198. if (domain == 'sci-hub.ee') {
  199. pdf_scihub_ee()
  200. }
  201. })();