Skip to content

Instantly share code, notes, and snippets.

@aquaductape
Last active May 10, 2024 00:29
Show Gist options
  • Save aquaductape/fe60f584fb6a9cdaa9a66807104520f8 to your computer and use it in GitHub Desktop.
Save aquaductape/fe60f584fb6a9cdaa9a66807104520f8 to your computer and use it in GitHub Desktop.
detect solidjs on page
const hasSolidJS = async () => {
if (
typeof window._$HY === "object" &&
window._$HY.completed instanceof WeakSet
)
return true;
const hydrateKeyName = "data-hk";
const bodyFirstEl = document.body.firstElementChild;
if (bodyFirstEl && bodyFirstEl.hasAttribute(hydrateKeyName)) return true;
const scripts = document.querySelectorAll("script");
const links = document.querySelectorAll("link");
const attributeHydrateKeyNameRegex = new RegExp(
`(?:has|get)Attribute\\(["']${hydrateKeyName}["']\\)`
);
const linkTypes = ["modulepreload", "prefetch", "preload", "prerender"];
const doesFileTextContainSolid = (text) => {
return (
text.match(/\$DX_DELEGATE/) || text.match(attributeHydrateKeyNameRegex)
);
};
for (const script of scripts) {
if (script.textContent.match(attributeHydrateKeyNameRegex)) return true;
if (
script.type !== "module" ||
script.crossOrigin !== "anonymous" ||
script.src.match(/^chrome-extension/)
)
continue;
const result = await fetch(script.src);
const text = await result.text();
if (doesFileTextContainSolid(text)) return true;
}
for (const link of links) {
if (
(!link.type && !link.rel) ||
(link.rel && !linkTypes.includes(link.rel)) ||
(link.type && !linkTypes.includes(link.type)) ||
(link.href && link.href.match(/^chrome-extension/)) ||
(link.hasAttribute("src") &&
link.getAttribute("src").match(/^chrome-extension/)) ||
(link.getAttribute("as") !== "script" &&
!(link.href || link.getAttribute("src")).match(/\.js$/))
) {
continue;
}
const result = await fetch(link.href || link.getAttribute("src"));
const text = await result.text();
if (doesFileTextContainSolid(text)) return true;
}
return false;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment