DOI-Unlock

DOI-Unlock with CTRL+left click on any DOI on any page will turn to 3 buttons, which are linked to Sci-Hub, Lib-Gen and Z-Lib

目前为 2021-03-25 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name DOI-Unlock
  3. // @namespace https://greasyfork.org/en/users/738719-szmegma
  4. // @version 1.0.5
  5. // @description DOI-Unlock with CTRL+left click on any DOI on any page will turn to 3 buttons, which are linked to Sci-Hub, Lib-Gen and Z-Lib
  6. // @author szmegma
  7. // @match http*://*/*
  8. // @require https://code.jquery.com/jquery-latest.min.js
  9. // @grant GM_addStyle
  10. // @icon data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIAAAAyCAMAAAAp4XiDAAACAVBMVEUAAADYiwvZpxGjo6OkpKSkpKTZnxCjo6OioqLZoxDZmw/Zlg7ZlA7Zjgyjo6OkpKSlpaWjo6PZkQ2kpKSkpKTZmg7YkQ2kpKSjo6Ojo6Ojo6Ojo6Ojo6OoqKixsbHapxLZnQ/YpxGjo6PZjAykpKTZpxDZjQzViwmjo6PZpRHZpRHLpTjYjgzZjgykpKTVphujo6PZphHWphGjo6OlpaX///8AAAC9vb3/pAD/vgb/uwb/uQXjkgn/oQDqsQ3/tgS7u7v/rwO8vLylpaX/tAT/pQGtra3/qwKqqqr/5a//68Kurq7/rAO2trb3twj3oAL//fqzs7Onp6f3rQXuqgMDAQD/+/P/7MH/uSX2sAb/qAT3pAT/sAOfbwL/9N7/7sn/35v/vTv3swepdAP0pgKfaQAoGwAJBgD/89j/137Or1P/y1H/uxX3qASfcwP4ogJaQgKyeQHYjABtSAAbEwD/8M24uLisrKz/6Kr/1nf/0HT/1XD/ymn/xFn/wlHGp0z/x0KfgD//xi+fdBv/uBDnsA7/qw7vtAvrlwb/swTmnQSIXQTNjgLKhwHmlQClawD//PX/9uH/79H/6rj/56//5Kf/1or/2oT/1G7/xE//v0T/vEP7uiD/tCDbnATBjQTzqgOfcQKNZAJ0UgFbQAHwnACTXwB7UQBCKwA0JQAYEADQdliYAAAANXRSTlMAcZ3+jxn1bzn19fX19IpiFvf1kgn19fLguJtyQA8E/PWSeWZUPTwb69nY1MzKwaelWVlLKPS7cy4AAAKKSURBVEjH3dLnW9pAHMBxW9TWAUirdYJbu3dSq21RSMCKgAyRaYuIA2hBcO9aZ+vo3nu3f2V/B3l44LnkOt+03xeBJPdJ7u5Jxl+psEK6h0taIf8JkFVWrG5PppadPPsjIS1u16vOJVPpJ2TlZFEuMgFIS6+WkMQZkT7+bK6EHldLCQsvMcGQVp1ZzWXWtcIF00G58ELMKjSR0rq9XHWl6nF4qVl4OaU6uJ8+9Uz0FNNxQVKsh9slitRLCjRXvUwhIOQimLmuLP1imQ5WJyoQIAVxsj/9YqYGkax/jRTKJjQacw2B4FVIJJKaQn5C7D8g9dW7+TqyEAgEFk5U1+PidOXKJUIrlacwcnjtPLG1QxjJMZCJIYeHNBMjEkckEnmdNnxpcnKJh2T3tXBZaZq+3pJS8xBF3TVk/woJUdALPqLkihNlaj0UNdbHQxwMw7zyvPNZEXkLJ8rFJzPBZQYKTQ2HHDipcjDWD90wevA9HDwME+ylINtjJcM8tNnuO6owkmt585VO5tmYobh6ljcewNGSy0NiNDQwkCCLFNQ71gXHq+7L/KTo1iBNj2663ZujiFyhqDtBt9sOv112IeKDkSNt0Aj8uQlPH0YndnjPFJAOSxFG8m7TdHc/GtUPm3ADiLcNNQQzQ+RangDphJKkE0Ug+WhiPi2UnBg6QRObFiJo+d+2tNottHw/LNv2VKt9mVg+EFc+TlyfUzbZ/yy+yffim+wUIAdc4S90Mr9zmuLqWE2QfTgJs8ZY/IOJIcKy84kP5pGTZREJ8xAjy7LbO592tpui0ehHOHF6Z2fnV1nIOzf33IgTsbGJ2O8QMUaOrV8ktn4UIw3iC8TEDRlYjbW7CNU2ZvxB3wGnnimlJ4uJUAAAAABJRU5ErkJggg==
  11. // ==/UserScript==
  12.  
  13. //================================================== settings
  14.  
  15. //variables
  16. const
  17. regex = '(10\.[0-9]{4,}(?:\.[0-9]+)*/(?:(?![%"&\'<>#? ])\\S)+)',
  18. doiRegex = new RegExp('(?:' + regex + ')', 'i'),
  19. //doiRegex = new RegExp(regex),
  20. btn1 = document.createElement('input'),
  21. btn2 = document.createElement('input'),
  22. btn3 = document.createElement('input'),
  23. libgenURL = 'http://libgen.li/scimag/index.php?s=',
  24. zlibURL = 'https://booksc.org/s/?q=',
  25. scihubURL = 'https://sci-hub.do/',
  26. style = "color:orange;font-size:1.5rem;line-height:2;padding:5px;background:black;text-shadow: 1px 1px 0 black, 1px -1px 0 black, -1px 1px 0 black, -1px -1px 0 black";
  27.  
  28. //================================================== find DOI
  29. function findDOI(element){
  30. if($(element).is("[href*='org/10.']"))
  31. return $(element).attr("href").split('org/')[1].trim();
  32. if($(element).text().match(doiRegex)!==null)
  33. return $(element).text().match(doiRegex)[0].trim();
  34. return false;
  35. }
  36.  
  37. //================================================== button click to navigate the selected site
  38. function unlockDOI(){
  39. if($(this).is("[data-doi]")){
  40. if(this.id=='scihub')
  41. window.open(scihubURL+$(this).attr('data-doi'));
  42. if(this.id=='zlib')
  43. window.open(zlibURL+$(this).attr('data-doi'));
  44. if(this.id=='libgen')
  45. window.open(libgenURL+$(this).attr('data-doi'));
  46. }
  47. }
  48.  
  49. $(document).ready(function(){
  50.  
  51. //================================================== set attributes for all buttons
  52. btn1.setAttribute("value", "SCI-HUB");
  53. btn1.setAttribute("type", "button");
  54. btn1.setAttribute("id", "scihub");
  55. btn1.setAttribute("class", "DOI-unlock-button");
  56.  
  57. btn2.setAttribute("value", "Z-LIB");
  58. btn2.setAttribute("type", "button");
  59. btn2.setAttribute("id", "zlib");
  60. btn2.setAttribute("class", "DOI-unlock-button");
  61.  
  62. btn3.setAttribute("value", "LIB-GEN");
  63. btn3.setAttribute("type", "button");
  64. btn3.setAttribute("id", "libgen");
  65. btn3.setAttribute("class", "DOI-unlock-button");
  66.  
  67. //================================================== append buttons to the body and acivate them
  68. document.getElementsByTagName('body')[0].append(btn1,btn2,btn3);
  69. document.getElementById("scihub").addEventListener("click", unlockDOI, false);
  70. document.getElementById("zlib").addEventListener("click", unlockDOI, false);
  71. document.getElementById("libgen").addEventListener("click", unlockDOI, false);
  72.  
  73. //================================================== remove all inspected class and DOI attribute from buttons
  74. //remove all button attributes
  75. $('input').removeAttr('data-doi');
  76.  
  77. //remove all highlights
  78. [].forEach.call(document.querySelectorAll('*'), function(a){
  79. a.classList.remove('DOI-unlock-inspected');
  80. });
  81.  
  82. //================================================== if BACK or FORWARD button has been clicked
  83. if(window.history && window.history.pushState) {
  84. window.history.pushState('forward', null, '');
  85. $(window).on('popstate', function(){
  86.  
  87. //remove all button attributes
  88. $('input').removeAttr('data-doi');
  89.  
  90. //remove all highlights
  91. [].forEach.call(document.querySelectorAll('*'), function(a){
  92. a.classList.remove('DOI-unlock-inspected');
  93. });
  94. });
  95. }
  96.  
  97. //================================================== find DOI press CTRL key and hold + click on DOI
  98. $(document).on('click', function(e){
  99.  
  100. //if NOT the DOI-unlock-button has been clicked
  101. if(!$(e.target).hasClass('DOI-unlock-button')){
  102.  
  103. //remove all button attributes
  104. $('input').removeAttr('data-doi');
  105.  
  106. //remove all highlights
  107. [].forEach.call(document.querySelectorAll('*'), function(a){
  108. a.classList.remove('DOI-unlock-inspected');
  109. });
  110. }
  111.  
  112. //if CTRL key has pressed
  113. if(e.ctrlKey){
  114.  
  115. //prevent the default function of the cliked element
  116. e.preventDefault();
  117.  
  118. //clear console
  119. console.clear();
  120.  
  121. //if clicked element returned valid DOI
  122. if(findDOI(e.target)){
  123.  
  124. //add DOI attribute to the buttons
  125. $('input.DOI-unlock-button').attr('data-doi',findDOI(e.target));
  126.  
  127. //add highlight to the clicked element
  128. e.target.classList.add("DOI-unlock-inspected");
  129. }
  130. }
  131. });
  132.  
  133. //================================================== style the buttons
  134. GM_addStyle(`
  135. #scihub{
  136. left:0 !important;
  137. }
  138. #zlib{
  139. left:100px !important;
  140. }
  141. #libgen{
  142. left:200px !important;
  143. }
  144. .DOI-unlock-button{
  145. z-index:99999999;
  146. width:100px !important;
  147. height:30px !important;
  148. background:black !important;
  149. color:white !important;
  150. cursor:pointer;
  151. display:none !important;
  152. position:fixed !important;
  153. bottom:0 !important;
  154. border:none !important;
  155. box-shadow:0 0 0 1px white inset;
  156. margin:0 !important;
  157. padding:0 !important;
  158. font-size:inherit !important;
  159. text-align:center !important;
  160. }
  161. .DOI-unlock-inspected{
  162. background:yellow !important;
  163. color:black !important;
  164. font-size:130% !important;
  165. padding:5px !important;
  166. }
  167. *[data-doi]{
  168. display:block !important;
  169. }
  170. `);
  171. });