AutoClicker

一个自动对网页元素(按钮,链接等)进行重复点击的脚本,可以利用到春节刷票,大学选课,双十一抢购等场景中。

  1. // ==UserScript==
  2. // @name AutoClicker
  3. // @namespace http://gv7.me
  4. // @version 0.2.6
  5. // @description 一个自动对网页元素(按钮,链接等)进行重复点击的脚本,可以利用到春节刷票,大学选课,双十一抢购等场景中。
  6. // @author c0ny1,JackyTsuuuy
  7. // @match *://*/*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. 'use strict';
  13.  
  14. // 多少毫秒点击一次
  15. var cyce = 1000;
  16. // 方式一:通过id定位点击元素
  17. var id = "";
  18. // 方式二:通过标签名tag,属性名attr,属性值value定位点击元素
  19. var tag = "";
  20. var attr = "";
  21. var value = "";
  22. // 方式三:通过xpath定位点击元素 e.g: var str_xpath = '//*[@id="su"]';
  23. var str_xpath = '';
  24. // 方式四:通过selector定位点击元素,e.g: var str_qs = "div .search span a";
  25. var str_qs = "";
  26. // 方式五:通过自定义函数定位点击元素
  27. var isCustiom = false;
  28.  
  29.  
  30. /*
  31. 定位点击对象的辅助函数
  32. */
  33. function getTargetByCustom(){
  34. /*若启用方式五,请在该函数体内编写自定义定位点击元素的代码*/
  35. var target;
  36. return target;
  37. }
  38.  
  39. function getTargetById(t_id){
  40. var target = document.getElementById(t_id);
  41. return target;
  42. }
  43.  
  44.  
  45. function getTargetByTAV(t_tag,t_attr,t_value){
  46. var target = document.getElementsByTagName(t_tag);
  47. for(var i=0;i <target.length;i++){
  48. if(target[i].getAttribute(t_attr) == t_value){
  49. return target[i];
  50. }
  51. }
  52. }
  53.  
  54. function getTargetByXpath(str_xpath) {
  55. var xresult = document.evaluate(str_xpath, document, null, XPathResult.ANY_TYPE, null);
  56. var xnodes = [];
  57. var xres;
  58. while (xres = xresult.iterateNext()) {
  59. xnodes.push(xres);
  60. }
  61. return xnodes;
  62. }
  63.  
  64.  
  65. function getTargetByQS(str_qs){
  66. var target = document.querySelector(str_qs);
  67. return target;
  68. }
  69.  
  70.  
  71. function trim(str){
  72. str = str.replace(/(^\s*)|(\s*$)/g, "");
  73. return str;
  74. }
  75.  
  76. /*
  77. 运行流程
  78. */
  79.  
  80. var btn;
  81.  
  82. if(isCustiom === true){
  83. btn = getTargetByCustom();
  84. }
  85.  
  86. if(trim(id) !== "" && (btn === null | typeof(btn) !== 'object')){
  87. btn = getTargetById(id);
  88. }
  89.  
  90. if(trim(tag) !== "" && trim(attr) !== "" && value !== "" && (btn === null | typeof(btn) !== 'object')){
  91. btn = getTargetByTAV(tag,attr,value);
  92. }
  93.  
  94. if(trim(str_xpath) !== "" && (btn === null | typeof(btn) !== 'object')){
  95. btn = getTargetByXpath(str_xpath)[0];
  96. }
  97.  
  98. if(trim(str_qs) !== "" && (btn === null | typeof(btn) !== 'object')){
  99. btn = getTargetByQS(str_qs);
  100. }
  101.  
  102. setInterval(function() {
  103. if (btn !== null && typeof(btn) === 'object') {
  104. console.info("[+] AutoClicker click object: " + btn.innerHTML);
  105. btn.click();
  106. }else{
  107. console.warn('[-] Autoclicker does not find the click object!');
  108. }
  109. },cyce);
  110. })();