ScienceDirect下载

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

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

  1. // ==UserScript==
  2. // @name ScienceDirect Download
  3. // @name:zh-CN ScienceDirect下载
  4. // @namespace tampermonkey.com
  5. // @version 2.0
  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. // @grant none
  12. // @run-at document-start
  13. // ==/UserScript==
  14.  
  15. /**
  16. * 获取 blob
  17. * @param {String} url 目标文件地址
  18. * @return {cb}
  19. */
  20. function getBlob(url, cb) {
  21. var xhr = new XMLHttpRequest();
  22. xhr.open('GET', url, true);
  23. xhr.responseType = 'blob';
  24. xhr.onload = function () {
  25. if (xhr.status === 200) {
  26. cb(xhr.response);
  27. }
  28. };
  29. xhr.send();
  30. }
  31.  
  32. /**
  33. * 保存
  34. * @param {Blob} blob
  35. * @param {String} filename 想要保存的文件名称
  36. */
  37. function saveAs(blob, filename) {
  38. if (window.navigator.msSaveOrOpenBlob) {
  39. navigator.msSaveBlob(blob, filename);
  40. } else {
  41. var link = document.createElement('a');
  42. var body = document.querySelector('body');
  43. link.href = window.URL.createObjectURL(blob);
  44. link.download = filename;
  45. // fix Firefox
  46. link.style.display = 'none';
  47. body.appendChild(link);
  48. link.click();
  49. body.removeChild(link);
  50. window.URL.revokeObjectURL(link.href);
  51. };
  52. }
  53.  
  54. /**
  55. * 下载
  56. * @param {String} url 目标文件地址
  57. * @param {String} filename 想要保存的文件名称
  58. */
  59. function download(url, filename) {
  60. getBlob(url, function (blob) {
  61. saveAs(blob, filename);
  62. });
  63. };
  64. (function () {
  65. 'use strict';
  66. var domain = document.domain;
  67. if (domain == 'pdf.sciencedirectassets.com') {
  68. var url = document.URL + '&download=true';
  69. console.log(url);
  70. var title = document.URL.split("/")[5].split("-")[2];
  71. // var html_url = "https://www.sciencedirect.com/science/article/pii/" + document.URL.split("/")[5].split("-")[2]
  72. var ret = prompt('请输入文件名,点击确认下载', title);
  73. if (ret !== null && ret != '') {
  74. var filename = ret + '.pdf';
  75. download(url, filename);
  76. };
  77. };
  78. if (domain == 'www.sciencedirect.com') {
  79. document.addEventListener("DOMContentLoaded", DOM_ContentReady);
  80. function DOM_ContentReady() {
  81. // get rawlink
  82. var head = document.head;
  83. // creat newlink
  84. var linkid = head.getElementsByTagName('meta')[0].content;
  85. if (linkid) {
  86. var new_url = "https://www.sciencedirect.com/science/article/pii/" + linkid + "/pdfft?isDTMRedir=true";
  87. let Container = document.createElement('div');
  88. Container.id = "sp-ac-container";
  89. Container.style.position = "fixed";
  90. Container.style.left = "300px";
  91. Container.style.top = "28px";
  92. Container.style['z-index'] = "999999";
  93. Container.innerHTML = `<button title="Tips:Copy the title of the article before downloading" class="button1" id="download" onclick="window.location.href='${new_url}'")>download</button>
  94. <style>
  95. .button1 {
  96. -webkit-transition-duration: 0.4s;
  97. transition-duration: 0.4s;
  98. padding: 2px 16px;
  99. text-align: center;
  100. background-color: green;
  101. color: white;
  102. border: 1px solid #4CAF50;
  103. border-radius:5px;
  104. }
  105. .button1:hover {
  106. background-color: #4CAF50;
  107. color: red;
  108. }
  109. </style>`;
  110. document.body.appendChild(Container);
  111. //绑定按键点击功能
  112. // Container.onclick = function () {
  113. // // alert("你好");
  114. // downPlan(url);
  115. // return;
  116. // };
  117. // console.log(url);
  118. }
  119. }
  120. };
  121. })()