ASIWeb Auto Click Button

Adds a button to asiweb that clicks elements in sequence when enabled

  1. // ==UserScript==
  2. // @name ASIWeb Auto Click Button
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.2
  5. // @description Adds a button to asiweb that clicks elements in sequence when enabled
  6. // @author ils94
  7. // @match https://asiweb.tre-rn.jus.br/asi/*
  8. // @grant GM_setValue
  9. // @grant GM_getValue
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. const clickPaths = [
  16. "/html/body/div[2]/form/div[3]/div/div/div[1]/ul[2]/li[1]/h3/a",
  17. "/html/body/div[2]/form/div[3]/div/div/div[1]/ul[2]/li[1]/div/ul[1]/li/div[2]/a",
  18. "/html/body/div[2]/div[3]/form/div[1]/div[2]/div[2]/p/span[1]/button",
  19. "/html/body/div[2]/div[3]/form/div[1]/div[2]/div[2]/div[3]/p/span[2]/button[1]",
  20. "/html/body/div[2]/div[3]/form/div[1]/div[2]/div[2]/p/span/button[1]"
  21. ];
  22.  
  23. function findElementByXPath(doc, xpath) {
  24. let element = null;
  25. try {
  26. element = doc.evaluate(xpath, doc, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
  27. } catch (e) {
  28. console.log(`Error evaluating XPath ${xpath}:`, e);
  29. }
  30. if (!element) {
  31. const iframes = doc.getElementsByTagName('iframe');
  32. for (let iframe of iframes) {
  33. try {
  34. const iframeDoc = iframe.contentDocument || iframe.contentWindow.document;
  35. element = iframeDoc.evaluate(xpath, iframeDoc, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
  36. if (element) break;
  37. element = findElementByXPath(iframeDoc, xpath);
  38. if (element) break;
  39. } catch (e) {
  40. console.log('Error accessing iframe:', iframe.src, e);
  41. }
  42. }
  43. }
  44. return element;
  45. }
  46.  
  47. function simulateClick(element) {
  48. if (element) {
  49. try {
  50. element.click();
  51. element.dispatchEvent(new Event('click', {
  52. bubbles: true
  53. }));
  54. console.log(`Clicked element:`, element);
  55. return true;
  56. } catch (e) {
  57. console.log(`Error clicking element:`, e);
  58. return false;
  59. }
  60. }
  61. return false;
  62. }
  63.  
  64. function performClickForStep(step) {
  65. const xpath = clickPaths[step];
  66. if (!xpath) {
  67. console.log('No more steps to execute.');
  68. GM_setValue('clickStep', 0);
  69. GM_setValue('clickEnabled', false); // Desativa após último passo
  70. return false;
  71. }
  72.  
  73. const element = findElementByXPath(document, xpath);
  74. console.log(`Attempting step ${step} at XPath ${xpath}:`, element);
  75.  
  76. if (element) {
  77. if (simulateClick(element)) {
  78. const nextStep = step + 1;
  79. GM_setValue('clickStep', nextStep);
  80. console.log(`Step ${step} done. Advancing to step ${nextStep}`);
  81. if (nextStep >= clickPaths.length) {
  82. console.log('Last step reached. Disabling auto-click.');
  83. GM_setValue('clickEnabled', false); // Desativa após último passo
  84. }
  85. return true;
  86. }
  87. } else {
  88. console.log(`Element not found for XPath ${xpath}. Will retry.`);
  89. }
  90. return false;
  91. }
  92.  
  93. function addCustomButton() {
  94. const button = document.createElement('button');
  95. button.textContent = 'Relação para o DB'
  96. button.style.position = 'fixed';
  97. button.style.top = '10px';
  98. button.style.right = '10px';
  99. button.style.zIndex = '9999';
  100. button.style.padding = '10px 20px';
  101. button.style.backgroundColor = '#4CAF50';
  102. button.style.color = 'white';
  103. button.style.border = 'none';
  104. button.style.borderRadius = '5px';
  105. button.style.cursor = 'pointer';
  106. button.style.fontSize = '16px';
  107.  
  108. button.onclick = function() {
  109. console.log('Botão ativado. Iniciando sequência...');
  110. GM_setValue('clickStep', 0);
  111. GM_setValue('clickEnabled', true);
  112. retryActions();
  113. };
  114.  
  115. document.body.appendChild(button);
  116. console.log('Botão personalizado adicionado à página.');
  117. }
  118.  
  119. function retryActions() {
  120. if (!GM_getValue('clickEnabled', false)) {
  121. return;
  122. }
  123.  
  124. let attempts = 0;
  125. const maxAttempts = 60;
  126.  
  127. const interval = setInterval(function() {
  128. if (attempts >= maxAttempts) {
  129. console.log('Número máximo de tentativas atingido.');
  130. clearInterval(interval);
  131. return;
  132. }
  133.  
  134. if (!GM_getValue('clickEnabled', false)) {
  135. clearInterval(interval);
  136. return;
  137. }
  138.  
  139. const currentStep = GM_getValue('clickStep', 0);
  140. if (currentStep >= clickPaths.length) {
  141. console.log('Todos os passos concluídos.');
  142. GM_setValue('clickStep', 0);
  143. GM_setValue('clickEnabled', false);
  144. clearInterval(interval);
  145. return;
  146. }
  147.  
  148. if (performClickForStep(currentStep)) {
  149. clearInterval(interval);
  150. }
  151.  
  152. attempts++;
  153. }, 500);
  154. }
  155.  
  156. // MutationObserver para reagir a mudanças no DOM
  157. const observer = new MutationObserver(function() {
  158. if (GM_getValue('clickEnabled', false)) {
  159. retryActions();
  160. }
  161. });
  162.  
  163. observer.observe(document, {
  164. childList: true,
  165. subtree: true
  166. });
  167.  
  168. // Adiciona o botão na inicialização
  169. window.addEventListener('load', () => {
  170. addCustomButton();
  171. });
  172. })();