From af1b36535cba6d1a2cf4db0a45eda77d838f3744 Mon Sep 17 00:00:00 2001
From: Lucie Bader <167515375+Lucie-Bdr@users.noreply.github.com>
Date: Wed, 19 Feb 2025 13:10:42 +0100
Subject: [PATCH] Surlignage actif sur ttes les pages

---
 src/background/background.js |  4 +--
 src/utils/highlighting.js    | 54 ++++++++++++++++++++----------------
 2 files changed, 32 insertions(+), 26 deletions(-)

diff --git a/src/background/background.js b/src/background/background.js
index ea71766..ddfb4d0 100644
--- a/src/background/background.js
+++ b/src/background/background.js
@@ -45,7 +45,7 @@ browser.storage.onChanged.addListener((changes, area) => {
         if (!extensionActive) {
           log("Token ajouté, activation automatique de l'extension.");
           browser.storage.local.set({ extensionActive: true });
-          enableExtensionFeatures();
+          updateExtension();
           browser.runtime.sendMessage({
             action: "updateUI",
             extensionActive: true,
@@ -155,7 +155,7 @@ async function saveToken(token) {
   const { extensionActive } = await browser.storage.local.get("extensionActive");
   if (!extensionActive) {
     await browser.storage.local.set({ extensionActive: true });
-    enableExtensionFeatures();
+    updateExtension();
     browser.runtime.sendMessage({
       action: "updateUI",
       extensionActive: true,
diff --git a/src/utils/highlighting.js b/src/utils/highlighting.js
index 27402c8..e65a253 100644
--- a/src/utils/highlighting.js
+++ b/src/utils/highlighting.js
@@ -1,3 +1,6 @@
+window.activeLexiconIds = window.activeLexiconIds || new Set();
+
+
 // Logs immédiats pour vérifier l'injection
 console.log("🔵 DÉBUT DU FICHIER highlighting.js");
 try {
@@ -55,28 +58,6 @@ browser.runtime.onMessage.addListener((message) => {
     }
 });
 
-// Au début du fichier, après la déclaration de observer
-document.addEventListener('visibilitychange', async () => {
-    if (document.visibilityState === 'visible' && window.highlightingActive && activeLexiconIds.size > 0) {
-        log("📄 Page redevenue visible, réinitialisation du surlignage");
-        removeAllHighlights();
-        await updateLexiconCache();
-        highlightVisibleContent();
-        attachMutationObserver();
-    }
-});
-
-// Ajoutons aussi un listener pour le chargement complet de la page
-window.addEventListener('pageshow', async () => {
-    if (window.highlightingActive && activeLexiconIds.size > 0) {
-        log("📄 Page affichée (pageshow), réinitialisation du surlignage");
-        removeAllHighlights();
-        await updateLexiconCache();
-        highlightVisibleContent();
-        attachMutationObserver();
-    }
-});
-
 // Déplacer checkAndRestoreHighlightingState après la déclaration de startHighlighting
 (function () {
     try {
@@ -90,9 +71,27 @@ window.addEventListener('pageshow', async () => {
         let lexiconWordsCache = new Map();
         let highlightingActive = false;
         window.highlightingActive = false;
-        let activeLexiconIds = new Set();
         let observer = null;
 
+        document.addEventListener('visibilitychange', async () => {
+            if (document.visibilityState === 'visible' && window.highlightingActive && activeLexiconIds.size > 0) {
+                log("📄 Page redevenue visible, réinitialisation du surlignage");
+                removeAllHighlights();
+                await updateLexiconCache();
+                highlightVisibleContent();
+                attachMutationObserver();
+            }
+        });
+        window.addEventListener('pageshow', async () => {
+            if (window.highlightingActive && activeLexiconIds.size > 0) {
+                log("📄 Page affichée (pageshow), réinitialisation du surlignage");
+                removeAllHighlights();
+                await updateLexiconCache();
+                highlightVisibleContent();
+                attachMutationObserver();
+            }
+        });
+
         // ─────────────────────────────────────────────────────────────────────────────
         // Gestion des messages du background
         // ─────────────────────────────────────────────────────────────────────────────
@@ -213,6 +212,7 @@ window.addEventListener('pageshow', async () => {
                 getAllLexiconWords: !!window.getAllLexiconWords,
                 activeLexiconIds: Array.from(activeLexiconIds)
             });
+            let allWords;
             try {
                 if (!window.authToken) {
                     throw new Error("Pas de token d'authentification");
@@ -227,7 +227,7 @@ window.addEventListener('pageshow', async () => {
                 }
 
                 log("📥 Appel de getAllLexiconWords...");
-                const allWords = await window.getAllLexiconWords(window.authToken);
+                allWords = await window.getAllLexiconWords(window.authToken);
                 log("📝 Réponse de getAllLexiconWords:", allWords);
                 
                 if (!allWords || typeof allWords !== 'object') {
@@ -505,6 +505,11 @@ window.addEventListener('pageshow', async () => {
                     return;
                 }
                 log("🔄 État des lexiques trouvé:", activeLexicons);
+                
+                // Restauration de l'état de surlignage
+                for (const lexiconId of activeLexicons) {
+                    await startHighlighting(lexiconId);
+                }
             } catch (error) {
                 console.error("❌ Erreur lors de la restauration de l'état:", error);
                 window.highlightingActive = false;
@@ -514,6 +519,7 @@ window.addEventListener('pageshow', async () => {
 
         // Ne pas démarrer automatiquement le surlignage
         checkAndRestoreHighlightingState();
+
     } catch (error) {
         console.error("🔴 Erreur critique dans l'IIFE:", error);
     }
-- 
GitLab