Skip to content

Commit 1e09296

Browse files
Add script to hide tabs for non-admin users
1 parent e72d394 commit 1e09296

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed

examples/hide-all

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<script>
2+
(function () {
3+
const MODULE_KEY = "StackLab_OcultarAbaTodos_permissions_v1";
4+
if (window[MODULE_KEY]) return;
5+
window[MODULE_KEY] = true;
6+
7+
console.log("[StackLab:OcultarAba] iniciado...");
8+
9+
async function fetchUserProfile() {
10+
try {
11+
const resp = await axios.get("/api/v1/profile");
12+
console.log("[StackLab:OcultarAba] RAW PROFILE:", resp);
13+
14+
return resp?.data || null;
15+
} catch (e) {
16+
console.error("[StackLab:OcultarAba] erro ao buscar /profile", e);
17+
return null;
18+
}
19+
}
20+
21+
function isAdminByPermissions(profile) {
22+
if (!profile) {
23+
console.log("[StackLab:OcultarAba] profile NULL → ocultar");
24+
return false;
25+
}
26+
27+
const currentAccountId = profile.account_id;
28+
console.log("[StackLab:OcultarAba] Conta ativa:", currentAccountId);
29+
30+
const accounts = profile.accounts || [];
31+
console.log("[StackLab:OcultarAba] Accounts:", accounts);
32+
33+
const currentAcc = accounts.find(acc => acc.id === currentAccountId);
34+
35+
if (!currentAcc) {
36+
console.warn("[StackLab:OcultarAba] Conta ativa não encontrada no array accounts → fallback ocultar");
37+
return false;
38+
}
39+
40+
console.log("[StackLab:OcultarAba] Conta atual encontrada:", currentAcc);
41+
42+
const perms = currentAcc.permissions || [];
43+
console.log("[StackLab:OcultarAba] Permissions da conta atual:", perms);
44+
45+
const isAdmin = perms.includes("administrator");
46+
47+
console.log("[StackLab:OcultarAba] isAdmin =", isAdmin);
48+
49+
return isAdmin;
50+
}
51+
52+
function ocultarAbaTodos() {
53+
const labels = ["Todos", "Todos os Contatos", "All Contacts"];
54+
55+
const uls = document.querySelectorAll("ul");
56+
uls.forEach(ul => {
57+
ul.querySelectorAll("a").forEach(a => {
58+
const t = (a.innerText || "").trim();
59+
labels.forEach(lbl => {
60+
if (t.startsWith(lbl)) {
61+
const li = a.closest("li");
62+
if (li) {
63+
li.style.display = "none";
64+
console.log("[StackLab:OcultarAba] ocultado:", t);
65+
}
66+
}
67+
});
68+
});
69+
});
70+
}
71+
72+
function startObserver(shouldHide) {
73+
const root = document.body;
74+
75+
const obs = new MutationObserver(() => {
76+
if (shouldHide) ocultarAbaTodos();
77+
});
78+
79+
obs.observe(root, { childList: true, subtree: true });
80+
}
81+
82+
document.addEventListener("DOMContentLoaded", async () => {
83+
console.log("[StackLab:OcultarAba] DOM Loaded");
84+
85+
const profile = await fetchUserProfile();
86+
const isAdmin = isAdminByPermissions(profile);
87+
88+
if (!isAdmin) {
89+
console.log("[StackLab:OcultarAba] Não é admin → ocultando aba");
90+
ocultarAbaTodos();
91+
startObserver(true);
92+
} else {
93+
console.log("[StackLab:OcultarAba] É admin → NÃO ocultar aba");
94+
startObserver(false);
95+
}
96+
});
97+
})();
98+
</script>

0 commit comments

Comments
 (0)