From c0d702f01fa10389cb5c677e3336b29e3ed44e81 Mon Sep 17 00:00:00 2001 From: Ayke Halder Date: Sun, 9 Jan 2022 01:57:49 +0100 Subject: [PATCH] DEV: migrate audio cloak-prevention to decorateCookedElement (#15502) Migrate deprecated decorateCooked to decorateCookedElement for audio cloak-prevention. This might give a minimal performance boost: running audio cloak-prevention for 20 (non-audio) posts takes 1 ms and not 15 ms. Co-authored-by: Jarek Radosz --- .../discourse/app/initializers/post-decorations.js | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/app/assets/javascripts/discourse/app/initializers/post-decorations.js b/app/assets/javascripts/discourse/app/initializers/post-decorations.js index 983b6892120..1b52c6fc423 100644 --- a/app/assets/javascripts/discourse/app/initializers/post-decorations.js +++ b/app/assets/javascripts/discourse/app/initializers/post-decorations.js @@ -39,20 +39,19 @@ export default { nativeLazyLoading(api); - api.decorateCooked( - ($elem) => { - const players = $("audio", $elem); - if (players.length) { - players.on("play", () => { + api.decorateCookedElement( + (elem) => { + elem.querySelectorAll("audio").forEach((player) => { + player.addEventListener("play", () => { const postId = parseInt( - $elem.closest("article").data("post-id"), + elem.closest("article")?.dataset.postId, 10 ); if (postId) { api.preventCloak(postId); } }); - } + }); }, { id: "discourse-audio" } );