Detect iframe clicks via loss of focus

This commit is contained in:
Andrew Kingston 2025-02-04 12:00:42 +00:00
parent 7b13b54b52
commit 9ad4e1048a
No known key found for this signature in database
1 changed files with 11 additions and 1 deletions

View File

@ -34,7 +34,7 @@ let candidateTarget: HTMLElement | undefined
// Processes a "click outside" event and invokes callbacks if our source element
// is valid
const handleClick = (e: MouseEvent) => {
const target = e.target as HTMLElement
const target = (e.target || e.relatedTarget) as HTMLElement
// Ignore click if this is an ignored class
if (target.closest('[data-ignore-click-outside="true"]')) {
@ -91,9 +91,19 @@ const handleMouseDown = (e: MouseEvent) => {
document.addEventListener("click", handleMouseUp, true)
}
// Handle iframe clicks by detecting a loss of focus on the main window
const handleBlur = () => {
if (document.activeElement?.tagName === "IFRAME") {
handleClick(
new MouseEvent("click", { relatedTarget: document.activeElement })
)
}
}
// Global singleton listeners for our events
document.addEventListener("mousedown", handleMouseDown)
document.addEventListener("contextmenu", handleClick)
window.addEventListener("blur", handleBlur)
/**
* Adds or updates a click handler