|
|
"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" |
|
|
); |
|
|
}; |
|
|
|
|
|
|
|
|
const isInIframe = () => { |
|
|
try { |
|
|
return window.self !== window.top; |
|
|
} catch { |
|
|
|
|
|
|
|
|
return true; |
|
|
} |
|
|
}; |
|
|
|
|
|
|
|
|
const isEmbedded = () => { |
|
|
try { |
|
|
return window.location !== window.parent.location; |
|
|
} catch { |
|
|
|
|
|
return true; |
|
|
} |
|
|
}; |
|
|
|
|
|
|
|
|
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; |
|
|
} |
|
|
}; |
|
|
|
|
|
if (shouldShowWarning()) { |
|
|
|
|
|
setShowWarning(true); |
|
|
} |
|
|
}, []); |
|
|
|
|
|
return ( |
|
|
<IframeWarningModal isOpen={showWarning} onOpenChange={setShowWarning} /> |
|
|
); |
|
|
} |
|
|
|