dati

自动答题

目前为 2018-12-05 提交的版本。查看 最新版本

  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/9999
  9. // @include https://www.douyu.com/1209
  10. // @include https://www.douyu.com/196
  11. // @version 1.5
  12. // @run-at document-end
  13. // ==/UserScript==
  14.  
  15. function checkPreview() {
  16. if (document.querySelector('div[class^="answerPreview"]')) {
  17. console.log('30秒后开始答题');
  18. setTimeout(function() {
  19. console.log('开始检测答案');
  20. findAnswer(1);
  21. }, 25000);
  22. return;
  23. }
  24. setTimeout(function() {
  25. checkPreview();
  26. }, 1000);
  27. }
  28.  
  29. function findAnswer(t) {
  30. var json = {};
  31. json[1] = 0;
  32. json[2] = 0;
  33. json[3] = 0;
  34. var content = document.querySelectorAll('.chat-msg-item').length ? document.querySelectorAll('.chat-msg-item') : document.querySelectorAll('.Barrage-content');
  35. for (var i = 0; i < content.length; i++) {
  36. switch (content[i].innerText.substr(0, 1).toUpperCase()) {
  37. case 'A':
  38. json[1]++;
  39. break;
  40. case 'B':
  41. json[2]++;
  42. break;
  43. case 'C':
  44. json[3]++;
  45. break;
  46. }
  47. }
  48. var tempVal = 0,
  49. tempKey = '';
  50. for (var key in json) {
  51. if (json[key] > tempVal) {
  52. tempKey = key;
  53. tempVal = json[key];
  54. }
  55. }
  56. if (tempKey) {
  57. console.log('检测到最多答案为' + tempKey + '!');
  58. answer(tempKey);
  59. } else {
  60. t++;
  61. if (t < 40) {
  62. setTimeout(function() {
  63. findAnswer(t);
  64. }, 100);
  65. } else {
  66. console.log('没有检测到答案,有C选C了');
  67. answer(3);
  68. }
  69. }
  70. }
  71.  
  72. function answer(i) {
  73. console.log('准备答题');
  74. var observer = new MutationObserver(function(mutations) {
  75. mutations.forEach(function(mutation) {
  76. var li = document.querySelectorAll('div[class^="answerProblem"] ul li');
  77. if (li.length > 0) {
  78. li[i - 1].click();
  79. console.log('答题完成,19分钟后自动刷新网页');
  80. setTimeout(function() {
  81. window.location.reload();
  82. }, 1140000);
  83. observer.disconnect();
  84. }
  85. });
  86. });
  87. observer.observe(document.querySelector('div[class^="answerPreview"]').parentNode, {
  88. childList: true
  89. });
  90. }
  91.  
  92. checkPreview();
  93. console.log('开始自动答题');