ScienceDirect下载

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

当前为 2023-11-24 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name ScienceDirect Download
  3. // @name:zh-CN ScienceDirect下载
  4. // @namespace tampermonkey.com
  5. // @version 3.1.5
  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.  
  108. if (domain == 'www.sciencedirect.com') {
  109. let access = null;
  110. document.addEventListener('DOMContentLoaded', (event) => {
  111. console.log('DOM加载完成.');
  112. let linkid = document.head.getElementsByTagName('meta')[0].content;
  113. let titile = document.title.replace(' - ScienceDirect', '');
  114. GM_setValue(linkid, titile);
  115. try {
  116. access = document.querySelector("#mathjax-container > div.accessbar-sticky > div:nth-child(2) > div > ul > li.RemoteAccess > a").href.split('login')[1];
  117. }
  118. catch(e){
  119. if (e instanceof TypeError) {
  120. access = document.querySelector("#mathjax-container > div.accessbar-sticky > div:nth-child(2) > div > ul > li.ViewPDF > a").href;
  121. // statements to handle this very common expected error
  122. } else {
  123. console.log(e); // re-throw the error unchanged
  124. }
  125. }
  126. let doi = document.getElementsByClassName('anchor doi anchor-default')[0].href.split('org')[1];
  127. GM_setValue('access', access);
  128. let types = 'download';
  129. let new_url = "https://www.sciencedirect.com/science/article/pii/" + linkid + "/pdfft?isDTMRedir=true"
  130. if (GM_getValue('access')) {
  131. userDefinedBaseURL = GM_getValue('userDefinedBaseURL');
  132. new_url = userDefinedBaseURL + doi;
  133. types = 'scihub'
  134. }
  135. let Container = document.createElement('div');
  136. let s = window.screen.width / 1920;
  137. let left = "250px";
  138. let top = "20px";
  139. if (s < 0.5) {
  140. left = (100 * s).toString() + "px";
  141. top = (18 + 10 / s).toString() + "px";
  142. }
  143. console.log(left);
  144. Container.id = "sp-ac-container";
  145. Container.style.position = "fixed";
  146. Container.style.left = left;
  147. Container.style.top = top;
  148. Container.style['z-index'] = "2";
  149. Container.innerHTML = `<button title="Click to download" class="button1" onclick="window.open('${new_url}')">${types}</button>
  150. <style>
  151. .button1 {
  152. -webkit-transition-duration: 0.4s;
  153. transition-duration: 0.4s;
  154. padding: 1.5px 6px;
  155. text-align: center;
  156. background-color: #f5f5f5;
  157. color: rgb(243, 109, 33);
  158. border: 0.5px rgb(134, 218, 209);
  159. border-radius: 9px;
  160. font-family: NexusSans,Arial,Helvetica,Lucida Sans Unicode,Microsoft Sans Serif,Segoe UI Symbol,STIXGeneral,Cambria Math,Arial Unicode MS,sans-serif!important;
  161. }
  162. .button1:hover {
  163. background-color: rgb(134, 218, 209);;;
  164. color: red;
  165. }
  166. </style>`;
  167. document.body.appendChild(Container);
  168.  
  169. });
  170. }
  171. if (domain == 'scholar.cnki.net') {
  172. window.onload = function () {
  173. if (document.URL.includes('/Detail/index/')) {
  174. 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: ', '')
  175. let new_url2 = userDefinedBaseURL + '/' + doi2
  176. console.log(userDefinedBaseURL)
  177. let Container2 = document.createElement('p');
  178. Container2.style.position = "fixed";
  179. Container2.id = "sp-ac-container";
  180. Container2.style.top = "120px";
  181. Container2.style['z-index'] = "2";
  182. Container2.innerHTML = `<button title="Click to download" class="button1" onclick="window.open('${new_url2}')">scihub</button>
  183. <style>
  184. .button1 {
  185. -webkit-transition-duration: 0.4s;
  186. -webkit-text-size-adjust: 100%;
  187. transition-duration: 0.4s;
  188. width:80px;
  189. height:50px;
  190. padding: 1.5px 6px;
  191. text-align: center;
  192. background-color: #506698;
  193. color: white;
  194. border: 0.5px rgb(134, 218, 209);
  195. border-radius: 8px;
  196. font-family: NexusSans,Arial,Helvetica,Lucida Sans Unicode,Microsoft Sans Serif,Segoe UI Symbol,STIXGeneral,Cambria Math,Arial Unicode MS,sans-serif!important;
  197. }
  198. .button1:hover {
  199. background-color: rgb(134, 218, 209);;;
  200. color: rgb(243, 109, 33);
  201. }
  202. </style>`;
  203. document.getElementsByClassName('detail_detail-main__11Hij')[0].append(Container2)
  204. }
  205. }
  206. }
  207. if (domain == 'pdf.sciencedirectassets.com') {
  208. pdf_scidirect()
  209. }
  210. if (domain == 'sci-hub.ee') {
  211. pdf_scihub_ee()
  212. }
  213. })();