Skip to content

Instantly share code, notes, and snippets.

@budal
Last active February 9, 2025 00:30
Show Gist options
  • Save budal/c538c2e02c7cd8f03a1778738c20582f to your computer and use it in GitHub Desktop.
Save budal/c538c2e02c7cd8f03a1778738c20582f to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Filtro Inteligente de Unidades (SADE)
// @namespace http://budal.dev/
// @version 0.1
// @description Adiciona um campo para buscar e selecionar unidades automaticamente no SADE. Tal campo tem persistência no armazenamento das unidades anteriormente selecionadas.
// @author Thiago Philipe Budal
// @match *://*/syspm-web/public/usuarioAcesso
// @grant none
// ==/UserScript==
(function() {
'use strict';
function loadToastify(callback) {
let toastifyCss = document.createElement("link");
toastifyCss.rel = "stylesheet";
toastifyCss.href = "https://cdn.jsdelivr.net/npm/toastify-js/src/toastify.min.css";
document.head.appendChild(toastifyCss);
let toastifyScript = document.createElement("script");
toastifyScript.src = "https://cdn.jsdelivr.net/npm/toastify-js";
toastifyScript.onload = callback;
document.head.appendChild(toastifyScript);
}
function showToast(message, type = "success") {
let bgColor = type === "error" ? "linear-gradient(to right, #ff5f6d, #ff9966)" : "linear-gradient(to right, #00b09b, #96c93d)";
Toastify({
text: message,
duration: 3000,
gravity: "top",
position: "right",
backgroundColor: bgColor,
stopOnFocus: true,
}).showToast();
}
function initFiltroUnidades() {
const campoRamal = document.getElementById("CRYPT_ID_RAMAL");
if (!campoRamal) {
showToast("❌ Campo de Ramal não encontrado!", "error");
return;
}
const selectUnidades = document.getElementById("CRYPT_ID_AREA_DESPACHO");
if (!selectUnidades) {
showToast("❌ Select de Unidades não encontrado!", "error");
return;
}
const filtroContainer = document.createElement("div");
filtroContainer.style.display = "flex";
filtroContainer.style.flexDirection = "column";
filtroContainer.style.width = "90%";
filtroContainer.style.marginTop = "10px";
filtroContainer.innerHTML = `
<textarea id="filtro-unidades" placeholder="Digite as unidades separadas por vírgula (ex: 12BPM, 13BPM)"
style="width: 100%; height: 80px; padding: 5px; font-size: 14px; resize: none;"></textarea>
<div style="display: flex; width: 100%; margin-top: 5px;">
<button id="botao-selecionar-unidades" type="button"
style="flex: 1; padding: 10px; cursor: pointer; background-color: #007bff; color: white; border: none;">
Selecionar
</button>
<button id="botao-inverter-selecao" type="button"
style="flex: 1; padding: 10px; cursor: pointer; background-color: #ffcc00; color: black; border: none; margin-left: 5px;">
Inverter Seleção
</button>
</div>
`;
campoRamal.parentNode.insertBefore(filtroContainer, campoRamal.nextSibling);
const inputFiltro = document.getElementById("filtro-unidades");
const unidadesSalvas = localStorage.getItem("sade_filtro_unidades");
if (unidadesSalvas) {
inputFiltro.value = unidadesSalvas;
}
inputFiltro.addEventListener("input", () => {
localStorage.setItem("sade_filtro_unidades", inputFiltro.value);
});
document.getElementById("botao-selecionar-unidades").addEventListener("click", (event) => {
event.preventDefault();
event.stopPropagation();
const filtroTexto = inputFiltro.value.toUpperCase().split(",").map(x => x.trim()).filter(Boolean);
if (!filtroTexto.length) {
showToast("⚠️ Digite pelo menos um valor para filtrar.", "error");
return;
}
let encontrados = 0;
Array.from(selectUnidades.options).forEach(opt => {
if (filtroTexto.some(filtro => opt.text.toUpperCase().startsWith(filtro))) {
opt.selected = true;
encontrados++;
}
});
showToast(`✅ ${encontrados} unidades selecionadas.`);
});
document.getElementById("botao-inverter-selecao").addEventListener("click", (event) => {
event.preventDefault();
event.stopPropagation();
let totalInvertidos = 0;
Array.from(selectUnidades.options).forEach(opt => {
opt.selected = !opt.selected;
totalInvertidos++;
});
showToast(`🔄 ${totalInvertidos} unidades tiveram a seleção invertida.`);
});
showToast("✅ Campo de filtro de unidades adicionado com sucesso.");
}
loadToastify(() => setTimeout(initFiltroUnidades, 500));
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment