|
|
"use client"; |
|
|
|
|
|
import { useEffect, useState } from "react"; |
|
|
import IframeWarningModal from "./modal"; |
|
|
|
|
|
export default function IframeDetector() { |
|
|
const [showWarning, setShowWarning] = useState(false); |
|
|
|
|
|
useEffect(() => { |
|
|
|
|
|
const isAllowedDomain = (hostname: string) => { |
|
|
const host = hostname.toLowerCase(); |
|
|
return ( |
|
|
host.endsWith(".huggingface.co") || |
|
|
host.endsWith(".hf.co") || |
|
|
host === "huggingface.co" || |
|
|
host === "hf.co" || |
|
|
host === "enzostvs-deepsite.hf.space" || |
|
|
host === "deepsite.hf.co" |
|
|
); |
|
|
}; |
|
|
|
|
|
|
|
|
const isInIframe = () => { |
|
|
try { |
|
|
return window.self !== window.top; |
|
|
} catch { |
|
|
|
|
|
|
|
|
return true; |
|
|
} |
|
|
}; |
|
|
|
|
|
|
|
|
const isEmbedded = () => { |
|
|
try { |
|
|
return window.location !== window.parent.location; |
|
|
} catch { |
|
|
|
|
|
return true; |
|
|
} |
|
|
}; |
|
|
|
|
|
|
|
|
const addCanonicalUrl = () => { |
|
|
|
|
|
const existingCanonical = document.querySelector('link[rel="canonical"]'); |
|
|
if (existingCanonical) { |
|
|
existingCanonical.remove(); |
|
|
} |
|
|
|
|
|
|
|
|
const canonical = document.createElement("link"); |
|
|
canonical.rel = "canonical"; |
|
|
canonical.href = window.location.href; |
|
|
document.head.appendChild(canonical); |
|
|
|
|
|
|
|
|
if (isInIframe() || isEmbedded()) { |
|
|
try { |
|
|
const parentHostname = document.referrer |
|
|
? new URL(document.referrer).hostname |
|
|
: null; |
|
|
if (parentHostname && !isAllowedDomain(parentHostname)) { |
|
|
|
|
|
const noIndexMeta = document.createElement("meta"); |
|
|
noIndexMeta.name = "robots"; |
|
|
noIndexMeta.content = "noindex, nofollow"; |
|
|
document.head.appendChild(noIndexMeta); |
|
|
} |
|
|
} catch (error) { |
|
|
|
|
|
console.debug( |
|
|
"SEO: Could not determine parent domain for iframe indexing rules" |
|
|
); |
|
|
} |
|
|
} |
|
|
}; |
|
|
|
|
|
|
|
|
const shouldShowWarning = () => { |
|
|
if (!isInIframe() && !isEmbedded()) { |
|
|
return false; |
|
|
} |
|
|
|
|
|
try { |
|
|
|
|
|
const parentHostname = window.parent.location.hostname; |
|
|
return !isAllowedDomain(parentHostname); |
|
|
} catch { |
|
|
|
|
|
try { |
|
|
if (document.referrer) { |
|
|
const referrerUrl = new URL(document.referrer); |
|
|
return !isAllowedDomain(referrerUrl.hostname); |
|
|
} |
|
|
} catch { |
|
|
|
|
|
} |
|
|
return true; |
|
|
} |
|
|
}; |
|
|
|
|
|
|
|
|
addCanonicalUrl(); |
|
|
|
|
|
if (shouldShowWarning()) { |
|
|
|
|
|
setShowWarning(true); |
|
|
} |
|
|
}, []); |
|
|
|
|
|
return ( |
|
|
<IframeWarningModal isOpen={showWarning} onOpenChange={setShowWarning} /> |
|
|
); |
|
|
} |
|
|
|