diff --git a/Userland/Libraries/LibWeb/HTML/HTMLMediaElement.cpp b/Userland/Libraries/LibWeb/HTML/HTMLMediaElement.cpp
index a347c1d6b54..7df28b19345 100644
--- a/Userland/Libraries/LibWeb/HTML/HTMLMediaElement.cpp
+++ b/Userland/Libraries/LibWeb/HTML/HTMLMediaElement.cpp
@@ -366,6 +366,23 @@ WebIDL::ExceptionOr HTMLMediaElement::pause()
return {};
}
+WebIDL::ExceptionOr HTMLMediaElement::toggle_playback()
+{
+ // FIXME: This runs from outside the context of any user script, so we do not have a running execution
+ // context. This pushes one to allow the promise creation hook to run.
+ auto& environment_settings = document().relevant_settings_object();
+ environment_settings.prepare_to_run_script();
+
+ ScopeGuard guard { [&] { environment_settings.clean_up_after_running_script(); } };
+
+ if (potentially_playing())
+ TRY(pause());
+ else
+ TRY(play());
+
+ return {};
+}
+
// https://html.spec.whatwg.org/multipage/media.html#dom-media-volume
WebIDL::ExceptionOr HTMLMediaElement::set_volume(double volume)
{
diff --git a/Userland/Libraries/LibWeb/HTML/HTMLMediaElement.h b/Userland/Libraries/LibWeb/HTML/HTMLMediaElement.h
index f3b3fc560ea..57606b9be93 100644
--- a/Userland/Libraries/LibWeb/HTML/HTMLMediaElement.h
+++ b/Userland/Libraries/LibWeb/HTML/HTMLMediaElement.h
@@ -84,6 +84,7 @@ public:
bool potentially_playing() const;
WebIDL::ExceptionOr> play();
WebIDL::ExceptionOr pause();
+ WebIDL::ExceptionOr toggle_playback();
double volume() const { return m_volume; }
WebIDL::ExceptionOr set_volume(double);
diff --git a/Userland/Libraries/LibWeb/Painting/MediaPaintable.cpp b/Userland/Libraries/LibWeb/Painting/MediaPaintable.cpp
index ab950f96cac..a8d423d9cca 100644
--- a/Userland/Libraries/LibWeb/Painting/MediaPaintable.cpp
+++ b/Userland/Libraries/LibWeb/Painting/MediaPaintable.cpp
@@ -310,24 +310,9 @@ MediaPaintable::DispatchEventOfSameName MediaPaintable::handle_mouseup(Badge WebIDL::ExceptionOr {
- if (media_element.paused())
- TRY(media_element.play());
- else
- TRY(media_element.pause());
- return {};
- };
-
if (cached_layout_boxes.control_box_rect.has_value() && cached_layout_boxes.control_box_rect->contains(position)) {
if (cached_layout_boxes.playback_button_rect.has_value() && cached_layout_boxes.playback_button_rect->contains(position)) {
- toggle_playback().release_value_but_fixme_should_propagate_errors();
+ media_element.toggle_playback().release_value_but_fixme_should_propagate_errors();
return DispatchEventOfSameName::Yes;
}
@@ -339,7 +324,7 @@ MediaPaintable::DispatchEventOfSameName MediaPaintable::handle_mouseup(Badge