diff --git a/Libraries/LibWeb/DOM/EventTarget.cpp b/Libraries/LibWeb/DOM/EventTarget.cpp index b37bf0a415f..596b7cf3aec 100644 --- a/Libraries/LibWeb/DOM/EventTarget.cpp +++ b/Libraries/LibWeb/DOM/EventTarget.cpp @@ -859,16 +859,22 @@ bool EventTarget::dispatch_event(Event& event) auto unsafe_shared_time = HighResolutionTime::unsafe_shared_current_time(); auto current_time = HighResolutionTime::relative_high_resolution_time(unsafe_shared_time, realm().global_object()); - if (is(this)) { - auto* window = static_cast(this); + GC::Ptr window = [&]() { + if (is(this)) + return GC::Ptr { static_cast(this) }; + + if (is(this)) + return static_cast(this)->document().window(); + + if (is(this)) + return static_cast(this)->window(); + + return GC::Ptr { nullptr }; + }(); + + if (window) { window->set_last_activation_timestamp(current_time); window->close_watcher_manager()->notify_about_user_activation(); - } else if (is(this)) { - auto const* element = static_cast(this); - if (auto window = element->document().window()) { - window->set_last_activation_timestamp(current_time); - window->close_watcher_manager()->notify_about_user_activation(); - } } }