dati

自动答题

  1. // ==UserScript==
  2. // @name dati
  3. // @name:zh-CN 大头答题
  4. // @namespace www.icycat.com
  5. // @description 自动答题
  6. // @description:zh-CN 检测答案自动答题
  7. // @author 冻猫
  8. // @include https://www.douyu.com/*
  9. // @version 1.6.3
  10. // @run-at document-end
  11. // ==/UserScript==
  12.  
  13. function exec(fn) {
  14. var script = document.createElement('script');
  15. script.setAttribute("type", "application/javascript");
  16. script.textContent = '(' + fn + ')();';
  17. document.body.appendChild(script);
  18. }
  19.  
  20. exec(function() {
  21. function checkPreview() {
  22. if (document.querySelector('div[class^="answerPreview"]')) {
  23. console.log('%c30秒后开始答题', 'color:#f60');
  24. setTimeout(function() {
  25. console.log('%c开始检测答案', 'color:#f60');
  26. findAnswer(1);
  27. }, 25000);
  28. return;
  29. }
  30. setTimeout(function() {
  31. checkPreview();
  32. }, 1000);
  33. }
  34.  
  35. function findAnswer(t) {
  36. var json = {};
  37. json[1] = 0;
  38. json[2] = 0;
  39. json[3] = 0;
  40. var content = document.querySelectorAll('.Barrage-content');
  41. for (var i = 0; i < content.length; i++) {
  42. switch (content[i].innerText.substr(0, 1).toUpperCase()) {
  43. case 'A':
  44. json[1]++;
  45. break;
  46. case 'B':
  47. json[2]++;
  48. break;
  49. case 'C':
  50. json[3]++;
  51. break;
  52. }
  53. }
  54. var tempVal = 0,
  55. tempKey = '';
  56. for (var key in json) {
  57. if (json[key] > tempVal) {
  58. tempKey = key;
  59. tempVal = json[key];
  60. }
  61. }
  62. if (tempKey) {
  63. console.log('%c检测到最多答案为' + tempKey + '!', 'color:#f60');
  64. socketAnswer(tempKey - 1);
  65. // answer(tempKey - 1);
  66. } else {
  67. t++;
  68. if (t < 30) {
  69. setTimeout(function() {
  70. findAnswer(t);
  71. }, 100);
  72. } else {
  73. console.log('%c没有检测到答案,有C选C了', 'color:#f60');
  74. socketAnswer(2);
  75. // answer(2);
  76. }
  77. }
  78. }
  79.  
  80. function answer(ans) {
  81. console.log('%c准备答题', 'color:#f60');
  82. var observer = new MutationObserver(function(mutations) {
  83. mutations.forEach(function(mutation) {
  84. var li = document.querySelectorAll('div[class^="answerProblem"] ul li');
  85. if (li.length > 0) {
  86. li[ans].click();
  87. var d = new Date();
  88. console.log('%c' + d.toLocaleTimeString() + '.' + d.getMilliseconds(), 'color:#f60');
  89. console.log('%c答题完成 -- 无延迟点击,19分钟后自动刷新网页', 'color:#f60');
  90. setTimeout(function() {
  91. window.location.reload();
  92. }, 1140000);
  93. observer.disconnect();
  94. }
  95. });
  96. });
  97. observer.observe(document.querySelector('div[class^="answerPreview"]').parentNode, {
  98. childList: true
  99. });
  100. }
  101.  
  102. function socketAnswer(ans) {
  103. window.socketProxy.socketStream.subscribe('compqs', problemHandler);
  104. var rid = window.socketProxy.info.room.roomId;
  105. var uid = document.querySelector('.Avatar-img').getAttribute('uid');
  106.  
  107. function problemHandler(e) {
  108. if (e.qid) {
  109. var msg = {
  110. type: 'compqaq',
  111. acid: 'act_comdt',
  112. qid: e.qid,
  113. aid: ans,
  114. rid: rid,
  115. uid: uid,
  116. };
  117. window.socketProxy.sendMessage(msg);
  118. var d = new Date();
  119. console.log('%c' + d.toLocaleTimeString() + '.' + d.getMilliseconds(), 'color:#f60');
  120. console.log('%c答题完成 -- 直接上传答案,19分钟后自动刷新网页', 'color:#f60');
  121. setTimeout(function() {
  122. window.location.reload();
  123. }, 1140000);
  124. }
  125. }
  126. }
  127.  
  128. checkPreview();
  129. console.log('%c开始自动答题 -- 1.6.3', 'color:#f60');
  130.  
  131. });