Timing Click

倒计时自动点击,电商抢东西专用

目前为 2019-08-23 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Timing Click
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.3
  5. // @description 倒计时自动点击,电商抢东西专用
  6. // @author Cherokeeli
  7. // @match *://*.taobao.com/*
  8. // @match *://*.jd.com/*
  9. // @require https://code.jquery.com/jquery-latest.js
  10. // @run-at document-idle
  11. // @grant GM_addStyle
  12. // @grant GM_getResourceText
  13. // @grant GM_xmlhttpRequest
  14. // @connect githubusercontent.com
  15.  
  16. // ==/UserScript==
  17.  
  18. (function() {
  19. 'use strict';
  20. var $ = $ || window.$;
  21. var _$ = $.noConflict(true);
  22.  
  23. var mainCss = `#ql-panel #counterClickTime {
  24. position:relative;
  25. z-index:999;
  26. height: 20px !important;
  27. width: 150px !important;
  28. font-size: 1em !important;
  29. padding: 5px !important;
  30. border: none !important;
  31. margin: 0 !important;
  32. }
  33.  
  34. #ql-panel #counterClickSelector {
  35. position:relative;
  36. z-index:999;
  37. height: 20px !important;
  38. width: 150px !important;
  39. font-size: 1em !important;
  40. padding: 5px !important;
  41. border: none !important;
  42. margin: 0 !important;
  43. }
  44.  
  45. #ql-panel #listenButton {
  46. position:relative;
  47. z-index:999;
  48. width: 100% !important;
  49. border: none !important;
  50. color: red !important;
  51. font-size: 1em !important;
  52. margin: 0 !important;
  53. background-color: #fff;
  54. }
  55.  
  56. #ql-panel #triggerButton {
  57. height:50px;
  58. width:50px;
  59. position:relative;
  60. z-index:9999;
  61. background-position:center;
  62. background-size:100%;
  63. background-image:url(https://raw.githubusercontent.com/Cherokeeli/monkey-lib/master/timing-click/image/1.pic.png);
  64. }
  65.  
  66. #ql-panel {
  67. position:fixed;
  68. z-index:9999;
  69. }
  70.  
  71. #ql-panel #hidePanel {
  72. position:absolute;
  73. z-index:9999;
  74. display:none;
  75. border: solid 1px rgb(238,238,238) !important;
  76. box-shadow: 0 0 5px rgb(213,210,210) !important;
  77. }`;
  78.  
  79. /*拖动*/
  80. class DragObj {
  81. constructor(dom) {
  82. this.mouse = {
  83. x: 0,
  84. y: 0
  85. };
  86. this.obj = {
  87. x: 0,
  88. y: 0
  89. };
  90. this.isClicked = false;
  91. if(dom) {
  92. this.dom = dom;
  93. } else {
  94. throw new Error('不存在的dom节点');
  95. }
  96. }
  97.  
  98. init(options={}) {
  99. document.addEventListener('mousedown', this.down.bind(this));
  100. document.addEventListener('mousemove', this.move.bind(this));
  101. document.addEventListener('mouseup', this.end.bind(this));
  102. if(typeof options.click ==='function') {
  103. this.clickCb = options.click;
  104. }
  105. if(typeof options.exclude === 'object') {
  106. this.excludeDom = options.exclude;
  107. }
  108. if(typeof options.include === 'object') {
  109. this.includeDom = options.include;
  110. }
  111. }
  112.  
  113. down(event) {
  114. if(this.includeDom.contains(event.target)) {
  115. this.isClicked = true;
  116. this.mouse.x = event.clientX;
  117. this.mouse.y = event.clientY;
  118. this.obj.x = this.dom.offsetLeft;
  119. this.obj.y = this.dom.offsetTop;
  120. }
  121. }
  122.  
  123. move(event) {
  124. if(this.isClicked) {
  125. let moveX = event.clientX - this.mouse.x;
  126. let moveY = event.clientY - this.mouse.y;
  127. this.dom.style.left = `${this.obj.x+moveX}px`;
  128. this.dom.style.top = `${this.obj.y+moveY}px`;
  129. }
  130. }
  131.  
  132. end(event) {
  133. this.isClicked = false;
  134. if(this.clickCb && (event.clientX === this.mouse.x && event.clientY===this.mouse.y)) {
  135. if(!this.excludeDom.contains(event.target) && this.includeDom.contains(event.target)) {
  136. this.clickCb(event);
  137. }
  138. }
  139. }
  140. }
  141.  
  142. GM_addStyle(mainCss);
  143.  
  144. let timeInput = _$('<input id="counterClickTime" placeholder="输入开抢时间" type="datetime-local" />');
  145. let selectorInput = _$('<input id="counterClickSelector" placeholder="输入抢按钮选择器" type="text" />');
  146. let listenButton = _$('<button id="listenButton">准备开抢</button>');
  147. let triggerButton = _$('<div id="triggerButton"></div>');
  148. let panel = _$('<div id="ql-panel"></div>');
  149. let hidePanel = _$('<div id="hidePanel"></div>');
  150.  
  151. hidePanel.append(timeInput);
  152. hidePanel.append(selectorInput);
  153. hidePanel.append(listenButton);
  154.  
  155. panel.append(triggerButton);
  156. panel.append(hidePanel);
  157.  
  158. _$(document.body).prepend(panel);
  159.  
  160. (new DragObj(panel[0])).init({
  161. click: function(event) {
  162. hidePanel.toggle('slow');
  163. },
  164. exclude: hidePanel[0],
  165. include: panel[0]
  166. });
  167.  
  168. function fireEvent(dom, eventName) {
  169. let event = new MouseEvent(eventName);
  170. return dom.dispatchEvent(event);
  171. }
  172.  
  173. function createWorkerFromExternalURL(url, callback) {
  174. GM_xmlhttpRequest({
  175. method: 'GET',
  176. url: url,
  177. onload: function (response) {
  178. var script, dataURL, worker = null;
  179. if (response.status === 200) {
  180. script = response.responseText;
  181. dataURL = 'data:text/javascript;base64,' + btoa(script);
  182. worker = new unsafeWindow.Worker(dataURL);
  183. }
  184. callback(worker);
  185. },
  186. onerror: function () {
  187. callback(null);
  188. }
  189. });
  190. }
  191.  
  192. /*开始抢*/
  193. listenButton.click(function(e) {
  194. let time = timeInput.val();
  195. let targetTime = Date.parse(new Date(time));
  196. let currentTime = Date.now();
  197. let timeout = targetTime-currentTime;
  198. console.log(timeout, selectorInput.val());
  199. createWorkerFromExternalURL('https://raw.githubusercontent.com/Cherokeeli/monkey-lib/master/timing-click/count-worker.js', function (worker) {
  200. if (!worker) throw Error('Create webworker failed');
  201. let btn = listenButton[0];
  202. worker.onmessage = function (event) {
  203. if (event.data === -1) {
  204. btn.disabled = false;
  205. _$(selectorInput.val()).trigger('click');
  206. fireEvent(document.querySelector(selectorInput.val()), 'click');
  207. } else {
  208. btn.disabled = true;
  209. btn.innerHTML = `距离开抢还有${Math.ceil(event.data / 1000)}秒`;
  210. }
  211. };
  212. worker.postMessage(timeout);
  213. });
  214. });
  215.  
  216. })();