diff --git a/manifest.json b/manifest.json index 2591e942483dc87a856c4dfc98f2a935162d2486..d67fa4584bd94feb482d4b7525bd0177a48453dc 100644 --- a/manifest.json +++ b/manifest.json @@ -74,6 +74,15 @@ "src/utils/stats.js" ], "run_at": "document_end" + }, + { + "matches": ["<all_urls>"], + "js": [ + "src/utils/logger.js", + "src/utils/api.js", + "src/utils/highlighting.js" + ], + "run_at": "document_start" } ], diff --git a/src/sidebar/sidebar.js b/src/sidebar/sidebar.js index 2c4123be6e852f6ed2cf7ee380f376e11feaa332..5b90964773983506a0c316017f13ccc7dc7e0af2 100644 --- a/src/sidebar/sidebar.js +++ b/src/sidebar/sidebar.js @@ -590,6 +590,11 @@ browser.runtime.onMessage.addListener(async (message) => { }); break; + case "updateAuthToken": + authToken = message.token; + window.authToken = message.token; + break; + default: console.warn("âš ï¸ Action inconnue reçue :", message.command); } diff --git a/src/utils/highlighting.js b/src/utils/highlighting.js index 97d622599a546e96a814b834a340b0a214baada0..4ffd5c91f25db834be3adf927f07354af57b4f5e 100644 --- a/src/utils/highlighting.js +++ b/src/utils/highlighting.js @@ -9,7 +9,6 @@ try { // Vérification de l'existence de l'objet browser console.log("🔠Vérification de l'environnement:", { hasBrowser: typeof browser !== 'undefined', - hasChrome: typeof chrome !== 'undefined', windowLocation: window.location.href }); @@ -31,12 +30,16 @@ async function initAuthToken() { const { accessToken } = await browser.storage.local.get("accessToken"); if (accessToken) { window.authToken = accessToken; + authToken = accessToken; log("🔑 Token récupéré depuis le stockage local"); + return true; } else { log("âš ï¸ Aucun token trouvé dans le stockage local"); + return false; } } catch (error) { console.error("⌠Erreur lors de la récupération du token:", error); + return false; } } @@ -47,18 +50,15 @@ initAuthToken(); browser.runtime.onMessage.addListener((message) => { if (message.command === "updateAuthToken" && message.token) { window.authToken = message.token; - log("🔑 Token mis à jour via message"); + authToken = message.token; + log("🔑 Token mis à jour via message:", !!message.token); } }); -// Déplacer la déclaration de l'observer au début du fichier, avant son utilisation -let observer = null; - // Au début du fichier, après la déclaration de observer document.addEventListener('visibilitychange', async () => { - if (document.visibilityState === 'visible' && highlightingActive) { + if (document.visibilityState === 'visible' && window.highlightingActive) { log("📄 Page redevenue visible, réinitialisation complète du surlignage"); - // Réinitialiser complètement le surlignage removeAllHighlights(); await updateLexiconCache(); highlightVisibleContent(); @@ -68,9 +68,8 @@ document.addEventListener('visibilitychange', async () => { // Ajoutons aussi un listener pour le chargement complet de la page window.addEventListener('load', async () => { - if (highlightingActive) { + if (window.highlightingActive) { log("📄 Page chargée, réinitialisation complète du surlignage"); - // Réinitialiser complètement le surlignage removeAllHighlights(); await updateLexiconCache(); highlightVisibleContent(); @@ -80,6 +79,12 @@ window.addEventListener('load', async () => { (function () { try { + // Déplacer la vérification hasRun ici + if (window.hasRun) { + console.log("âš ï¸ highlighting.js déjà chargé, mais listener maintenu"); + return; + } + window.hasRun = true; console.log("🟢 IIFE de highlighting.js démarrée"); // Vérification immédiate des objets critiques @@ -93,10 +98,11 @@ window.addEventListener('load', async () => { // Amélioration du log initial pour vérifier si le script s'exécute console.log("🟢 Début d'exécution du script highlighting.js"); - // Cache des mots par lexique + // Variables globales pour le script let lexiconWordsCache = new Map(); let highlightingActive = false; let activeLexiconIds = new Set(); + let observer = null; // ───────────────────────────────────────────────────────────────────────────── // Gestion des messages du background @@ -136,11 +142,6 @@ window.addEventListener('load', async () => { log("📡 Enregistrement du script auprès du background"); browser.runtime.sendMessage({ command: "register-highlighting-script" }); - // Déplacer la vérification hasRun ici - if (window.hasRun) { - console.log("âš ï¸ highlighting.js déjà chargé, mais listener maintenu"); - return; - } window.hasRun = true; log("🚀 Initialisation de highlighting.js"); @@ -159,15 +160,11 @@ window.addEventListener('load', async () => { log(`🎬 Démarrage du surlignage (lexiconId: ${lexiconId})`); try { - // Attendre jusqu'à 5 secondes maximum pour le token - let attempts = 0; - while (!window.authToken && attempts < 50) { - await new Promise(resolve => setTimeout(resolve, 100)); - attempts++; - } - + // Attendre que le token soit disponible via initAuthToken + await initAuthToken(); + if (!window.authToken) { - throw new Error("⌠Pas de token d'authentification disponible après attente"); + throw new Error("⌠Pas de token d'authentification disponible après initialisation"); } if (lexiconId) { @@ -425,7 +422,6 @@ window.addEventListener('load', async () => { } }); }); - observer.observe(document.body, { childList: true, subtree: true