hb redeem

激活hb key

当前为 2017-09-18 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name hb redeem
  3. // @namespace steam
  4. // @author 伟大鱼塘
  5. // @description 激活hb key
  6. // @include https://www.humblebundle.com/home/keys
  7. // @match https://www.humblebundle.com/home/keys
  8. // @version 0.2.3
  9. // @require https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js
  10. // @grant GM_xmlhttpRequest
  11. // @grant GM_addStyle
  12. // ==/UserScript==
  13.  
  14. window.addEventListener('load', () => {
  15. (function($) {
  16.  
  17. //添加相关dom组件,因为HB分页的问题做一下处理
  18.  
  19. const mulbtn = '<button id="redeemselectedsteamkey" style="border:none;display:block;color:#fff;background:rgba(155, 89, 182,0.8);text-align:center;padding:10px;width:60%;margin:auto;margin-top:20px;border-radius:10px;">激活选中游戏</button>';
  20. const allbtn = '<button id="redeemallsteamkey" style="border:none;display:block;color:#fff;background:rgba(52, 73, 94,0.75);text-align:center;padding:10px;width:60%;margin:auto;margin-top:20px;margin-bottom:20px;border-radius:10px;">激活全部游戏</button>';
  21. $('.table-rounder').prepend(mulbtn, allbtn);
  22. let sessionid = null;
  23.  
  24. function redeemFun() {
  25.  
  26. //添加样式
  27. {
  28. const modalstyle = '.redeem-moadal {visibility:hidden;opacity:0;transition: all .4s ease-out 0s;position:fixed;width:300px;height:270px;left:50%;top:50%;transform: translate(-50%,-50%);-webkit-transform: translate(-50%,-50%);background:#fff;z-index:5000;border-radius:15px;text-align:center;} .redeem-moadal p {line-height:1.8;color:#666;letter-spacing:0.5px;} .redeem-moadal p i{font-style:normal;color:#ccc;} #closemodal {visibility;hidden;opacity:0;border: 1px solid rgba(0, 0, 0, 0.3);padding:5px;width:100%;}';
  29. const bgstyle = '.blackbg {visibility:hidden;opacity:0;transition: all .4s ease-out 0s;position:fixed;left:0;top:0;height:100%;width:100%;background:rgba(0,0,0,.5);z-index:4000;} .redeem-moadal.open,.blackbg.open,#closemodal.open {visibility:visible;opacity:1;}';
  30. const gameStyle = 'tr {transition: all .4s ease-out 0s;} .act {background-color:rgba(89, 204, 103,.2) !important;} .failed {background-color:rgba(229, 68, 90,.4) !important;} button {transition: all .4s ease-out 0s;}.redeemsteamkey:hover,.getsessionid:hover {color:#fff !important;background-color:#7A981C !important;} #redeemselectedsteamkey:hover {background:rgba(155, 89, 182,1) !important;} #redeemallsteamkey:hover {background:rgba(88, 94, 100,1) !important;} .unredeemed-keys-table td.redeemer-cell {padding-right:0 !important;}';
  31. GM_addStyle(modalstyle + bgstyle + gameStyle);
  32. }
  33.  
  34. //添加状态框
  35. {
  36. const modal = '<div class="redeem-moadal"><div style="padding-top:30px;"><p>激活总数:<i id="ra">0</i></p><p>激活成功数:<i id="rs" style="color:#80CF35;">0</i></p><p>激活失败数:<i id="rf" style="color:#DD4141;">0</i></p><p class="rdone"><p></div><div style="padding:15px"><button id="closemodal" type="button">关闭窗口</button></div></div>';
  37. const bg = '<div class="blackbg"></div>';
  38. $('body').append(modal, bg);
  39.  
  40. $('#closemodal').on('click', function() {
  41. $('.redeem-moadal,.blackbg').removeClass('open');
  42. $(this).removeClass('open');
  43. });
  44. }
  45.  
  46. const sessioninput = '<tr id="sessionidtr"><td class="platform"><i class="hb hb-key hb-steam" title="Steam"></i></td><td><h4 title="你的Session ID">你的Session ID</h4></td><td class="js-redeemer-cell redeemer-cell"><div class="key-redeemer"><div class="container"><div class="keyfield redeemed"><input id="g_sessionID" type="text" style="width:100%;background: #E9EEE4;border:none;color: #7A981C;font-size:16px;text-align:center;"></div></div></div></td><td style="padding:0;text-align:center;"><button class="getsessionid" style="padding:8px 16px;border:1px solid #7A981C;color:#7A981C;background: #E9EEE4;">获取</button></td></tr>';
  47. $('.unredeemed-keys-table tbody').prepend(sessioninput);
  48.  
  49. $('#sessionidtr').nextAll().on('click', function() {
  50. let that = $(this);
  51. $(this).removeClass('failed');
  52. if (!that.hasClass('act')) {
  53. that.addClass('act');
  54. } else {
  55. that.removeClass('act');
  56. }
  57. });
  58. $.each($('#sessionidtr').nextAll(), (i, e) => {
  59. const btn = '<td style="padding:0;text-align:center;"><button class="redeemsteamkey" style="padding:8px 16px;border:1px solid #7A981C;color:#7A981C;background: #E9EEE4;">激活</button></td>';
  60. $(e).append(btn);
  61. });
  62.  
  63. $('#g_sessionID').on('input', function() {
  64. sessionid = $(this).val();
  65. });
  66. $('.getsessionid').off().on('click', function() {
  67. getSessionID();
  68. });
  69. getSessionID();
  70.  
  71. //单激活
  72.  
  73. $('.redeemsteamkey').on('click', function() {
  74. let that = $(this);
  75. redeem('s', that);
  76. });
  77.  
  78.  
  79. //批量激活
  80.  
  81. $('#redeemselectedsteamkey').on('click', function() {
  82. redeem('m', null, $('.act'));
  83.  
  84. });
  85. //全部激活
  86. $('#redeemallsteamkey').on('click', function() {
  87. redeem('m', null, $('#sessionidtr').nextAll());
  88. });
  89.  
  90. //激活
  91. function redeem(type, that, selector) {
  92. let mark = {
  93. ifsuccess: 0, //异步成功标识
  94. successful: 0, //成功数
  95. failed: 0 //失败数
  96. };
  97. //type参数为判断是否是批量激活,m为多激活,s为单激活
  98. if (type == "m") {
  99. let keys = [];
  100. let position = [];
  101. let i = 1; //循环定时器以及position数组,从1开始,第0次直接执行不通过loop函数
  102. let all = selector.length;
  103. $.each(selector, (i, e) => {
  104. let key = $(e).find('.keyfield').attr('title');
  105. let posi = $(e).index();
  106. position.push(posi);
  107. keys.push(key);
  108. });
  109. if (!sessionid) {
  110. alert('请输入你的Session ID!');
  111. } else if (keys.length) {
  112. $('.rdone').html('');
  113. $('#rs,#rf').html(0);
  114. $('.redeem-moadal,.blackbg').addClass('open');
  115. $('#ra').html(all);
  116. redeemResquest(keys[0], sessionid, mark, all, position[0]);
  117. loopRequest(keys, sessionid, mark, all, position, i);
  118. } else {
  119. alert('请先选择要激活的游戏!');
  120. }
  121. } else {
  122. let key = that.find('.keyfield').attr('title');
  123. let posi = that.parents('tr').index();
  124. if (!sessionid) {
  125. return alert('请输入你的Session ID!');
  126. }
  127. redeemResquest(key, sessionid, mark, 1, posi).then((mark) => {
  128. if (mark.successful)
  129. alert('激活成功!');
  130. else
  131. alert('激活失败!');
  132. }).catch((error) => {
  133. alert(`出错了!错误原因:${error}`);
  134. });
  135. }
  136. }
  137.  
  138. //多激活延迟
  139. function loopRequest(keys, sessionid, mark, all, p, i) {
  140. setTimeout(function() {
  141. if (i > (keys.length - 1)) {
  142. return;
  143. }
  144. redeemResquest(keys[i], sessionid, mark, all, p[i]);
  145. i++;
  146. loopRequest(keys, sessionid, mark, all, p, i);
  147. }, 10000);
  148. }
  149.  
  150. //激活请求 all参数用来保存请求总数 p参数用来确定哪一个div.game-key-string激活失败并加failed类
  151. function redeemResquest(key, sessionid, mark, all, p) {
  152. return new Promise((resolve, reject) => {
  153. GM_xmlhttpRequest({
  154. method: 'GET',
  155. url: `https://store.steampowered.com/account/ajaxregisterkey/?product_key=${key}&sessionid=${sessionid}`,
  156. data: `product_key=${key}&sessionid=${sessionid}`,
  157. onload: response => {
  158. result = JSON.parse(response.responseText);
  159. if (result.success == 1) {
  160. mark.successful += 1;
  161. $('#rs').html(mark.successful);
  162. } else {
  163. mark.failed += 1;
  164. $('#rf').html(mark.failed);
  165. if (p || p === 0) {
  166. $('.unredeemed-keys-table').find('tbody tr').eq(p).addClass('failed');
  167. }
  168. }
  169. mark.ifsuccess += 1;
  170. if (mark.ifsuccess == all) {
  171. $('#closemodal').addClass('open');
  172. $('.rdone').html('激活完成!');
  173. }
  174. resolve(mark);
  175. },
  176. onerror: () => {
  177. reject(error);
  178. }
  179. });
  180. });
  181. }
  182.  
  183. //获取session ID
  184. function getSessionID() {
  185. $('#g_sessionID').val('');
  186. $('.getsessionid').html('正在获取...');
  187. GM_xmlhttpRequest({
  188. method: 'GET',
  189. url: 'https://store.steampowered.com',
  190. onload: response => {
  191. let res = response.responseText;
  192. let ifss = res.includes('g_sessionID');
  193. if (ifss) {
  194. let start = res.indexOf('g_sessionID') + 15;
  195. let end = start + 24;
  196. let ssid = res.substring(start, end);
  197. sessionid = ssid;
  198. $('#g_sessionID').val(ssid);
  199. $('.getsessionid').html('获取成功!');
  200. $('.getsessionid').hover(function() {
  201. $(this).html('重新获取');
  202. });
  203. }
  204. },
  205. onerror: () => {
  206. alert('获取出错!');
  207. $('.getsessionid').html('重新获取');
  208. }
  209. });
  210. }
  211. //获取session ID
  212. function getSessionID() {
  213. $('#g_sessionID').val('');
  214. $('.getsessionid').html('正在获取...');
  215. GM_xmlhttpRequest({
  216. method: 'GET',
  217. url: 'https://store.steampowered.com',
  218. onload: response => {
  219. let res = response.responseText;
  220. let ifss = res.includes('g_sessionID');
  221. if (ifss) {
  222. let start = res.indexOf('g_sessionID') + 15;
  223. let end = start + 24;
  224. let ssid = res.substring(start, end);
  225. sessionid = ssid;
  226. $('#g_sessionID').val(ssid);
  227. $('.getsessionid').html('获取成功!');
  228. $('.getsessionid').hover(function() {
  229. $(this).html('重新获取');
  230. });
  231. }
  232. },
  233. onerror: () => {
  234. alert('获取出错!');
  235. $('.getsessionid').html('重新获取');
  236. }
  237. });
  238. }
  239. }
  240. redeemFun();
  241.  
  242. $('.js-key-manager-holder').on('click', '.jump-to-page', () => {
  243. redeemFun();
  244. });
  245.  
  246. })(jQuery);
  247. });