ScienceDirect下载

避免跳转在线pdf,可直接下载ScienceDirect文献到本地

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

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