correctly detect the extension if the hook is not used on the initial page

This commit is contained in:
Athou
2023-06-12 21:47:54 +02:00
parent cd94a3b56f
commit 7e48afe36c

View File

@@ -1,9 +1,12 @@
import { useEffect, useState } from "react"
export const useBrowserExtension = () => {
const [browserExtensionVersion, setBrowserExtensionVersion] = useState<string>()
// 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 => {