From 31ed54592829e450d983f582f1e75cfa9461e240 Mon Sep 17 00:00:00 2001 From: Jimmy GALLAND Date: Sun, 22 Nov 2026 15:09:52 +0100 Subject: [PATCH] fix --- src/content.js | 408 ++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 336 insertions(+), 72 deletions(-) diff --git a/src/content.js b/src/content.js index 3da50e4..bb6d3d2 100644 --- a/src/content.js +++ b/src/content.js @@ -1,320 +1,584 @@ (async () => { + if ( + window.top !== window || + document.getElementById("skynet-sso-bar") + ) { + return; + } const allowed = await browser.runtime.sendMessage({ + type: "SSO_IS_HOST_ALLOWED", + host: location.hostname + }).catch(() => false); if (!allowed || document.getElementById("skynet-sso-bar")) { + return; + } const prefs = await browser.storage.local.get({ + showNotch: true + }); const bar = document.createElement("aside"); + bar.id = "skynet-sso-bar"; + bar.dataset.collapsed = "false"; + bar.dataset.showNotch = String(prefs.showNotch !== false); + bar.setAttribute("aria-live", "polite"); - bar.innerHTML = ` - + // Build the toolbar with DOM methods instead of innerHTML. + function createElement(tag, className, text) { + const element = document.createElement(tag); + if (className) element.className = className; + if (text !== undefined) element.textContent = text; + return element; + } -
- - Vérification de la session… -
+ const brand = createElement("button", "skynet-sso-brand"); + brand.type = "button"; + brand.title = "Accueil SKYNET"; -
+ const mark = createElement("img", "skynet-sso-mark"); + mark.src = browser.runtime.getURL("icons/skynet.png"); + mark.alt = ""; -
- + const brandText = createElement("span"); + brandText.append("SKYNET "); + brandText.appendChild(createElement("b", "", "SSO")); + brand.append(mark, brandText); - -
+ const status = createElement("div", "skynet-sso-status"); + status.appendChild(createElement("i")); + status.appendChild( + createElement("span", "skynet-sso-label", "Vérification de la session…") + ); - - `; + const userElement = createElement("div", "skynet-sso-user"); + + const actions = createElement("div", "skynet-sso-actions"); + + const logoutButton = createElement("button", "skynet-sso-logout", "Déconnexion"); + logoutButton.type = "button"; + logoutButton.hidden = true; + + const collapseButton = createElement("button", "skynet-sso-collapse"); + collapseButton.type = "button"; + collapseButton.setAttribute("aria-label", "Masquer la barre SKYNET"); + collapseButton.title = "Masquer la barre"; + collapseButton.appendChild(createElement("span")); + collapseButton.firstElementChild.setAttribute("aria-hidden", "true"); + + actions.append(logoutButton, collapseButton); + + const tabButton = createElement("button", "skynet-sso-tab"); + tabButton.type = "button"; + tabButton.setAttribute("aria-label", "Afficher la barre SKYNET"); + tabButton.title = "Afficher la barre"; + const tabIcon = createElement("span"); + tabIcon.setAttribute("aria-hidden", "true"); + tabButton.appendChild(tabIcon); + + bar.append(brand, status, userElement, actions, tabButton); document.documentElement.appendChild(bar); function collapseAfterPageLoad() { + bar.dataset.collapsed = "true"; + } if (document.readyState === "complete") { + collapseAfterPageLoad(); + } else { + window.addEventListener("load", collapseAfterPageLoad, { once: true }); + } const dialog = document.createElement("div"); + dialog.id = "skynet-sso-dialog"; - dialog.innerHTML = ` -
- - `; + + // Build the logout dialog without parsing HTML strings. + const backdrop = createElement("div", "skynet-sso-dialog-backdrop"); + + const dialogPanelElement = createElement("section", "skynet-sso-dialog-panel"); + dialogPanelElement.setAttribute("role", "dialog"); + dialogPanelElement.setAttribute("aria-modal", "true"); + dialogPanelElement.setAttribute("aria-labelledby", "skynet-sso-dialog-title"); + dialogPanelElement.setAttribute( + "aria-describedby", + "skynet-sso-dialog-description" + ); + dialogPanelElement.tabIndex = -1; + + const dialogSymbol = createElement("div", "skynet-sso-dialog-symbol"); + dialogSymbol.setAttribute("aria-hidden", "true"); + dialogSymbol.appendChild(createElement("span")); + + const dialogEyebrow = createElement( + "p", + "skynet-sso-dialog-eyebrow", + "SKYNET SECURITY" + ); + + const dialogTitle = createElement( + "h2", + "", + "Confirmer la déconnexion" + ); + dialogTitle.id = "skynet-sso-dialog-title"; + + const dialogDescriptionElement = createElement( + "p", + "", + "Souhaitez-vous vraiment fermer votre session SKYNET sur l’ensemble des services ?" + ); + dialogDescriptionElement.id = "skynet-sso-dialog-description"; + + const dialogErrorElement = createElement("p", "skynet-sso-dialog-error"); + dialogErrorElement.setAttribute("role", "alert"); + dialogErrorElement.hidden = true; + + const dialogProgressElement = createElement("div", "skynet-sso-dialog-progress"); + dialogProgressElement.setAttribute("aria-live", "polite"); + dialogProgressElement.hidden = true; + + const spinner = createElement("span", "skynet-sso-spinner"); + spinner.setAttribute("aria-hidden", "true"); + + const progressLabel = createElement( + "span", + "skynet-sso-dialog-progress-label", + "Déconnexion en cours…" + ); + dialogProgressElement.append(spinner, progressLabel); + + const dialogActions = createElement("div", "skynet-sso-dialog-actions"); + + const cancelButton = createElement( + "button", + "skynet-sso-dialog-cancel", + "Annuler" + ); + cancelButton.type = "button"; + + const confirmButton = createElement("button", "skynet-sso-dialog-confirm"); + confirmButton.type = "button"; + confirmButton.append( + createElement("span", "", "Me déconnecter"), + createElement("span", "skynet-sso-dialog-arrow", "↗") + ); + confirmButton.lastElementChild.setAttribute("aria-hidden", "true"); + + dialogActions.append(cancelButton, confirmButton); + + dialogPanelElement.append( + dialogSymbol, + dialogEyebrow, + dialogTitle, + dialogDescriptionElement, + dialogErrorElement, + dialogProgressElement, + dialogActions + ); + + dialog.append(backdrop, dialogPanelElement); + document.documentElement.appendChild(dialog); const dialogPanel = dialog.querySelector(".skynet-sso-dialog-panel"); + const dialogCancel = dialog.querySelector(".skynet-sso-dialog-cancel"); + const dialogConfirm = dialog.querySelector(".skynet-sso-dialog-confirm"); + const dialogError = dialog.querySelector(".skynet-sso-dialog-error"); + const dialogProgress = dialog.querySelector(".skynet-sso-dialog-progress"); + const dialogProgressLabel = dialog.querySelector(".skynet-sso-dialog-progress-label"); + const dialogDescription = dialog.querySelector("#skynet-sso-dialog-description"); function closeLogoutDialog() { + if (dialog.dataset.busy === "true") return; + dialog.dataset.open = "false"; + dialogError.hidden = true; + logout.focus(); + } function openLogoutDialog() { + dialog.dataset.open = "true"; + dialog.dataset.busy = "false"; + dialogError.hidden = true; + dialogProgress.hidden = true; + dialogDescription.hidden = false; + dialogConfirm.disabled = false; + dialogCancel.disabled = false; + dialogConfirm.querySelector("span").textContent = "Me déconnecter"; + dialogPanel.focus(); + } function setLogoutBusy(busy) { + dialog.dataset.busy = String(busy); + dialogProgress.hidden = !busy; + dialogDescription.hidden = busy; + dialogCancel.disabled = busy; + dialogConfirm.disabled = busy; + dialogConfirm.querySelector("span").textContent = busy + ? "Déconnexion…" + : "Me déconnecter"; + dialogProgressLabel.textContent = "Déconnexion en cours…"; + dialogError.hidden = true; + } dialogCancel.addEventListener("click", closeLogoutDialog); + dialog.querySelector(".skynet-sso-dialog-backdrop") + .addEventListener("click", closeLogoutDialog); + dialogConfirm.addEventListener("click", async () => { + if (dialog.dataset.busy === "true") return; setLogoutBusy(true); try { + const result = await browser.runtime.sendMessage({ + type: "SSO_LOGOUT" + }); if (!result?.success) { + throw new Error("Le serveur n’a pas confirmé la déconnexion."); + } dialogProgressLabel.textContent = "Session fermée. Actualisation…"; + render({ status: "anonymous" }); + window.location.reload(); + } catch (error) { + dialog.dataset.busy = "false"; + dialogProgress.hidden = true; + dialogDescription.hidden = false; + dialogCancel.disabled = false; + dialogConfirm.disabled = false; + dialogConfirm.querySelector("span").textContent = "Réessayer"; + dialogError.textContent = error.message || "La déconnexion a échoué. Veuillez réessayer."; + dialogError.hidden = false; + } + }); document.addEventListener("keydown", event => { + if (dialog.dataset.open !== "true") return; + if (event.key === "Escape") { + event.preventDefault(); + closeLogoutDialog(); + } + if (event.key === "Tab") { + const focusable = [dialogCancel, dialogConfirm].filter( + element => !element.disabled + ); + if (!focusable.length) { + event.preventDefault(); + return; + } + const first = focusable[0]; + const last = focusable[focusable.length - 1]; + if (event.shiftKey && document.activeElement === first) { + event.preventDefault(); + last.focus(); + } else if (!event.shiftKey && document.activeElement === last) { + event.preventDefault(); + first.focus(); + } + } + }); const label = bar.querySelector(".skynet-sso-label"); + const user = bar.querySelector(".skynet-sso-user"); + const logout = bar.querySelector(".skynet-sso-logout"); + const tab = bar.querySelector(".skynet-sso-tab"); + const collapse = bar.querySelector(".skynet-sso-collapse"); - const brand = bar.querySelector(".skynet-sso-brand"); + brand.addEventListener("click", async event => { + event.preventDefault(); + event.stopPropagation(); const cfg = await browser.storage.local.get({ + homeUrl: "https://celjim.fr" + }); const destination = String( + cfg.homeUrl || "https://git.celjim.fr/public/zoraxy-sso-extension-firefox" + ).trim(); if (/^https?:\/\//i.test(destination)) { + window.location.assign(destination); + } + }); document.addEventListener("click", event => { + if ( + bar.dataset.collapsed !== "true" && + !bar.contains(event.target) + ) { + bar.dataset.collapsed = "true"; + } + }, true); + let autoCollapseTimer = null; + + function clearAutoCollapse() { + if (autoCollapseTimer !== null) { + window.clearTimeout(autoCollapseTimer); + autoCollapseTimer = null; + } + } + + function scheduleAutoCollapse() { + clearAutoCollapse(); + autoCollapseTimer = window.setTimeout(() => { + bar.dataset.collapsed = "true"; + autoCollapseTimer = null; + }, 2000); + } + function revealBar(event) { + event?.preventDefault(); + event?.stopPropagation(); + bar.dataset.collapsed = "false"; + } // Tout clic dans la barre annule le repli automatique. + // Le clic sur l'encoche reste une action d'ouverture. + bar.addEventListener("click", event => { + if (!tab.contains(event.target)) { + clearAutoCollapse(); + } + }, true); // Repli manuel + collapse.addEventListener("click", event => { + event.preventDefault(); + event.stopPropagation(); clearAutoCollapse(); + bar.dataset.collapsed = "true"; + }); // Déploiement au survol ou au clic de l'encoche + // L'encoche révèle la barre au survol, sans lancer de minuterie. + // La barre ne se replie automatiquement qu'après que le pointeur l'a quittée. + bar.addEventListener("mouseenter", clearAutoCollapse); + + bar.addEventListener("mouseleave", () => { + if (bar.dataset.collapsed !== "true") { + scheduleAutoCollapse(); + } + }); + tab.addEventListener("mouseenter", revealBar); - tab.addEventListener("click", revealBar); + + tab.addEventListener("click", event => { + clearAutoCollapse(); + revealBar(event); + }); function render(state) { + bar.dataset.state = state.status; + user.textContent = state.username || ""; + logout.hidden = state.status !== "authenticated"; if (state.status === "authenticated") { + label.textContent = "Session SSO active"; + } else if (state.status === "anonymous") { + label.textContent = "Aucune session SSO"; + } else if (state.status === "unavailable") { + label.textContent = "Serveur SSO indisponible"; + } else { + label.textContent = "Vérification de la session…"; + } + } browser.runtime.sendMessage({ + type: "SSO_GET_STATE" + }).then(render).catch(() => { + render({ status: "unavailable" }); + }); browser.runtime.onMessage.addListener(message => { + if (message?.type === "SSO_STATE") { + render(message.state); + } + }); browser.storage.onChanged.addListener((changes, area) => { + if (area === "local" && changes.showNotch) { + bar.dataset.showNotch = String( + changes.showNotch.newValue !== false + ); + } + }); logout.addEventListener("click", event => { + event.preventDefault(); + event.stopPropagation(); + openLogoutDialog(); + }); + })();