Timing Click

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

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

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