From 7e48afe36c0b25cabad03714c53b25f80f886b93 Mon Sep 17 00:00:00 2001 From: Athou Date: Mon, 12 Jun 2023 21:47:54 +0200 Subject: [PATCH] correctly detect the extension if the hook is not used on the initial page --- commafeed-client/src/hooks/useBrowserExtension.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/commafeed-client/src/hooks/useBrowserExtension.ts b/commafeed-client/src/hooks/useBrowserExtension.ts index 5523e5e5..1198215b 100644 --- a/commafeed-client/src/hooks/useBrowserExtension.ts +++ b/commafeed-client/src/hooks/useBrowserExtension.ts @@ -1,9 +1,12 @@ import { useEffect, useState } from "react" export const useBrowserExtension = () => { - const [browserExtensionVersion, setBrowserExtensionVersion] = useState() + // the extension will set the "browser-extension-installed" attribute on the root element + const [browserExtensionVersion, setBrowserExtensionVersion] = useState( + document.documentElement.getAttribute("browser-extension-installed") + ) - // the extension will set the "browser-extension-installed" attribute on the root element, monitor it for changes + // monitor the attribute on the root element as it may change after the page was loaded useEffect(() => { const observer = new MutationObserver(mutations => { mutations.forEach(mutation => {