Formulario Helper

Formulario Helper para chamados.

  1. // ==UserScript==
  2. // @name Formulario Helper
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.1
  5. // @description Formulario Helper para chamados.
  6. // @author ils94
  7. // @match https://atendimento-sao.tre-rn.jus.br/front/tracking.injector.php
  8. // @grant GM_setClipboard
  9. // @run-at document-idle
  10. // @license MIT
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15.  
  16. function criarModal() {
  17. const estilo = `
  18. #formModal {
  19. position: fixed;
  20. top: 50%;
  21. left: 50%;
  22. transform: translate(-50%, -50%);
  23. background: white;
  24. padding: 20px;
  25. border: 2px solid #007bff;
  26. border-radius: 10px;
  27. z-index: 10000;
  28. box-shadow: 0 0 10px rgba(0,0,0,0.5);
  29. width: 300px;
  30. font-family: sans-serif;
  31. }
  32. #formModal input, #formModal select, #formModal textarea {
  33. width: 100%;
  34. margin-bottom: 10px;
  35. padding: 5px;
  36. font-size: 14px;
  37. box-sizing: border-box;
  38. }
  39. #formModal textarea {
  40. resize: vertical;
  41. min-height: 60px;
  42. max-height: 120px;
  43. }
  44. #formModal .linha-flex {
  45. display: flex;
  46. gap: 5px;
  47. align-items: stretch;
  48. }
  49. #formModal button {
  50. margin-top: 5px;
  51. padding: 8px;
  52. width: 100%;
  53. font-weight: bold;
  54. cursor: pointer;
  55. }
  56. #fecharModal {
  57. position: absolute;
  58. top: 5px;
  59. right: 10px;
  60. cursor: pointer;
  61. font-weight: bold;
  62. color: red;
  63. }
  64. `;
  65.  
  66. const styleTag = document.createElement('style');
  67. styleTag.textContent = estilo;
  68. document.head.appendChild(styleTag);
  69.  
  70. const modal = document.createElement('div');
  71. modal.id = 'formModal';
  72. modal.style.display = 'none';
  73.  
  74. const {
  75. data,
  76. hora
  77. } = obterDataHoraAtual();
  78.  
  79. modal.innerHTML = `
  80. <span id="fecharModal">X</span>
  81. <label>Data:</label><input id="data" value="${data}">
  82. <label>Hora:</label><input id="hora" value="${hora}">
  83. <label>Origem:</label>
  84. <select id="origem">
  85. <option value="COJE">COJE</option>
  86. <option value="SEDE">SEDE</option>
  87. </select>
  88. <label>Destino:</label>
  89. <select id="destino">
  90. <option value="SEDE">SEDE</option>
  91. <option value="COJE">COJE</option>
  92. </select>
  93. <label>Passageiros:</label><input id="passageiros" placeholder="NOME1, NOME2">
  94. <label>Prioridade:</label>
  95. <select id="prioridade">
  96. <option value="NORMAL">NORMAL</option>
  97. <option value="URGENTE">URGENTE</option>
  98. </select>
  99. <label>Necessidade Especial:</label><input id="especial" placeholder="NENHUMA">
  100. <label>Justificativa do Chamado:</label><textarea id="justificativa" placeholder="Digite a justificativa aqui..."></textarea>
  101. <button id="copiarTexto">Inserir</button>
  102. `;
  103.  
  104. document.body.appendChild(modal);
  105.  
  106. document.getElementById('fecharModal').onclick = () => modal.style.display = 'none';
  107.  
  108. // Sincronizar selects de origem e destino
  109. const origemSelect = modal.querySelector('#origem');
  110. const destinoSelect = modal.querySelector('#destino');
  111.  
  112. origemSelect.addEventListener('change', () => {
  113. destinoSelect.value = origemSelect.value === 'COJE' ? 'SEDE' : 'COJE';
  114. });
  115.  
  116. destinoSelect.addEventListener('change', () => {
  117. origemSelect.value = destinoSelect.value === 'COJE' ? 'SEDE' : 'COJE';
  118. });
  119.  
  120. document.getElementById('copiarTexto').onclick = () => {
  121. const especialInput = document.getElementById('especial').value.trim();
  122. const especialTexto = especialInput === '' ? 'NENHUMA' : especialInput;
  123. const justificativaInput = document.getElementById('justificativa').value.trim();
  124. const passageirosInput = document.getElementById('passageiros').value.trim();
  125. const passageirosTexto = passageirosInput === '' ? 'NENHUM' : passageirosInput;
  126.  
  127. // Verifica se a justificativa está vazia
  128. if (justificativaInput === '') {
  129. alert('Por favor, preencha a Justificativa do Chamado.');
  130. return;
  131. }
  132.  
  133. const texto = `
  134. DATA: ${document.getElementById('data').value}
  135. HORA: ${document.getElementById('hora').value}
  136. ORIGEM: ${document.getElementById('origem').value}
  137. DESTINO: ${document.getElementById('destino').value}
  138. NOME DOS PASSAGEIROS: ${passageirosTexto}
  139. PRIORIDADE: ${document.getElementById('prioridade').value}
  140. NECESSIDADE ESPECIAL: ${especialTexto}
  141. JUSTIFICATIVA DO CHAMADO: ${justificativaInput}
  142. `.trim().toUpperCase();
  143.  
  144. // Função para tentar injetar o texto no editor TinyMCE
  145. function injectText() {
  146. let editorBody = null;
  147. const iframes = document.getElementsByTagName('iframe');
  148. for (let iframe of iframes) {
  149. try {
  150. const iframeDoc = iframe.contentDocument || iframe.contentWindow.document;
  151. // Tenta encontrar o body com id="tinymce" e opcionalmente data-id="content793588473"
  152. const body = iframeDoc.querySelector('body#tinymce');
  153. if (body) {
  154. editorBody = body;
  155. break;
  156. }
  157. } catch (e) {
  158. console.error('Erro ao acessar iframe:', e);
  159. }
  160. }
  161.  
  162. if (editorBody) {
  163. try {
  164. const formattedText = texto.replace(/\n/g, '<br>');
  165. editorBody.innerHTML = `<p>${formattedText}</p>`;
  166. return true;
  167. } catch (e) {
  168. console.error('Erro ao injetar texto no editor:', e);
  169. alert('Erro ao injetar texto no editor TinyMCE. O texto foi copiado para a área de transferência.');
  170. return false;
  171. }
  172. }
  173. return false;
  174. }
  175.  
  176. // Tenta injetar o texto com tentativas a cada 100ms, até 5 segundos
  177. let attempts = 0;
  178. const maxAttempts = 50; // 5 segundos (50 * 100ms)
  179. const interval = setInterval(() => {
  180. if (injectText()) {
  181. clearInterval(interval);
  182. // Copiar para a área de transferência
  183. if (typeof GM_setClipboard !== 'undefined') {
  184. GM_setClipboard(texto);
  185. } else {
  186. navigator.clipboard.writeText(texto).then(() => alert('Copiado e injetado no editor!'));
  187. }
  188. document.getElementById('formModal').style.display = 'none';
  189. } else if (attempts >= maxAttempts) {
  190. clearInterval(interval);
  191. alert('Não foi possível encontrar o editor TinyMCE com id="tinymce". O texto foi copiado para a área de transferência.');
  192. // Copiar para a área de transferência mesmo se a injeção falhar
  193. if (typeof GM_setClipboard !== 'undefined') {
  194. GM_setClipboard(texto);
  195. } else {
  196. navigator.clipboard.writeText(texto).then(() => alert('Copiado para a área de transferência!'));
  197. }
  198. document.getElementById('formModal').style.display = 'none';
  199. }
  200. attempts++;
  201. }, 100);
  202. };
  203. }
  204.  
  205. function criarBotao() {
  206. const doc = window.top.document; // documento da janela principal, não do iframe
  207. // Remove o botão se já existir
  208. const botaoExistente = doc.getElementById('botaoGerarSolicitacao');
  209. if (botaoExistente) botaoExistente.remove();
  210.  
  211. const botao = doc.createElement('button');
  212. botao.id = 'botaoGerarSolicitacao';
  213. botao.textContent = 'Gerar Solicitação';
  214. botao.style.position = 'fixed';
  215. botao.style.bottom = '20px';
  216. botao.style.right = '20px';
  217. botao.style.padding = '10px 15px';
  218. botao.style.backgroundColor = '#007bff';
  219. botao.style.color = '#fff';
  220. botao.style.border = 'none';
  221. botao.style.borderRadius = '5px';
  222. botao.style.cursor = 'pointer';
  223. botao.style.zIndex = '9999';
  224. botao.onclick = () => doc.getElementById('formModal').style.display = 'block';
  225.  
  226. doc.body.appendChild(botao);
  227. }
  228.  
  229. function obterDataHoraAtual() {
  230. const agora = new Date();
  231. const data = agora.toLocaleDateString('pt-BR');
  232. const hora = agora.toLocaleTimeString('pt-BR', {
  233. hour: '2-digit',
  234. minute: '2-digit'
  235. });
  236. return {
  237. data,
  238. hora
  239. };
  240. }
  241.  
  242. window.addEventListener('load', () => {
  243. criarModal();
  244. criarBotao();
  245. });
  246. })();