hacg script

enter something useful

  1. // ==UserScript==
  2. // @name hacg script
  3. // @namespace http://your.homepage/
  4. // @version 0.2.1
  5. // @description enter something useful
  6. // @author You
  7. // @require https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/1.5.8/clipboard.min.js
  8. // @include *://*.hacg.*/*
  9. // @include *://hacg.*/*
  10. // ==/UserScript==
  11.  
  12. /* jshint esnext: true */
  13.  
  14. // Your code here...
  15. (function () {
  16. // body...
  17. var $ = jQuery;
  18.  
  19. var tool = $('<div class="cbj-hacg-tool"></div>');
  20. $('body').append(tool);
  21.  
  22. tool.css({
  23. backgroundImage: "url('/favicon.ico')",
  24. backgroundSize: '100% 100%',
  25. cursor: 'pointer',
  26. position: 'fixed',
  27. right: '20px',
  28. bottom: '20px',
  29. width: '20px',
  30. height: '20px',
  31. backgroundColor: 'white'
  32. });
  33.  
  34. var content = $('article').html() + $('#comments .commentList').html();
  35. content = content.replace('本站不提供下载', '');
  36. var clipboard;
  37. tool.click(function () {
  38. var links = match(content, /[A-Za-z0-9]{40}/g).map(m => `magnet:?xt=urn:btih:${m}`);
  39. var specialLinks = match(content, /[A-Z2-9]{32}/g).map(m => `magnet:?xt=urn:btih:${decodeBase32(m)}`);
  40. var baiduReg = /(\/s\/\w+) (\w+)/g;
  41. var result, baiduLinks = [];
  42. while ((result = baiduReg.exec(content)) !== null) {
  43. result = baiduReg.exec(content);
  44. baiduLinks.push(`<a href="http://pan.baidu.com${result[1]}">${result[1]}</a> ${result[2]}`);
  45. }
  46.  
  47. if ($('.cbj-abstract').length === 0) {
  48. buildAbstract(links.concat(baiduLinks), specialLinks);
  49. } else {
  50. clipboard.destroy();
  51. $('.cbj-abstract').remove();
  52. }
  53. });
  54.  
  55. function match(content, reg) {
  56. var result = content.match(reg);
  57. return result || [];
  58. }
  59.  
  60. function buildAbstract(links, specialLinks) {
  61. links = [...new Set(links)];
  62. var icon = 'https://cdnjs.cloudflare.com/ajax/libs/foundicons/3.0.0/svgs/fi-page-copy.svg';
  63. var list = links.map((m, i) => `<li><span>${m}</span><button class="copy"><img src="${icon}" alt="Copy to clipboard"></button></li>`).join('');
  64. var specialList = specialLinks.map((m, i) => `<li><span style='color: red'>${m}</span><button class="copy"><img src="${icon}" alt="Copy to clipboard"></button></li>`).join('');
  65. var div = $(`<div class="cbj-abstract"><ul>${list}${specialList}</ul></div>`);
  66. $('.entry-content').prepend(div);
  67.  
  68. var buttonStyle = {
  69. position: 'relative',
  70. display: 'inline-block',
  71. margin: '4px 8px',
  72. height: '22px',
  73. padding: '0',
  74. fontSize: '13px',
  75. fontWeight: 'bold',
  76. lineHeight: '20px',
  77. color: '#333',
  78. whiteSpace: 'nowrap',
  79. verticalAlign: 'middle',
  80. cursor: 'pointer',
  81. backgroundColor: '#eee',
  82. backgroundImage: 'linear-gradient(#fcfcfc, #eee)',
  83. border: '1px solid #d5d5d5',
  84. borderRadius: '3px'
  85. };
  86. $('button.copy').css(buttonStyle);
  87. $('button.copy img').css({
  88. width: '20px',
  89. height: '20px'
  90. });
  91. div.find('li').css('background-color', 'rgb(50,50,50)');
  92. clipboard = new Clipboard('button.copy', {
  93. target: button => button.previousSibling
  94. });
  95. }
  96.  
  97. function decodeBase32(text) {
  98. var base32 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567".split("");
  99. var result = "";
  100. for (var i = 0; i < text.length; i++) {
  101. var t = base32.indexOf(text[i]);
  102. var e1, e2, e3, e4, e5;
  103. var temp;
  104. switch (i % 4) {
  105. case 0:
  106. e1 = t >> 1;
  107. temp = (t & 0b1) << 3;
  108. break;
  109. case 1:
  110. e2 = temp | t >> 2;
  111. temp = (t & 0b11) << 2;
  112. break;
  113. case 2:
  114. e3 = temp | t >> 3;
  115. temp = (t & 0b111) << 1;
  116. break;
  117. case 3:
  118. e4 = temp | t >> 4;
  119. e5 = t & 0b1111;
  120. result = result + [e1, e2, e3, e4, e5].map(x => x.toString(16)).join("");
  121. break;
  122. }
  123. }
  124. return result;
  125. }
  126.  
  127. })();