From 440d2c807fa172a4c6bb268c9923e1d4537a52cc Mon Sep 17 00:00:00 2001 From: evazion Date: Fri, 3 Sep 2021 05:33:11 -0500 Subject: [PATCH] posts: fix JS errors on non-flash posts when using Ruffle extension. Fix https://github.com/danbooru/danbooru/issues/4783#issuecomment-912426739: Spoke too soon. It works in flash posts, but not in non-flash posts. Here's the full trace that happens by trying the Shift-E shortcut (tested on https://danbooru.donmai.us/posts/4749304 with the latest ruffle for chrome at https://ruffle.rs/#downloads): 4749304:7 Uncaught TypeError: Cannot read properties of undefined (reading 'appendChild') at Object.initialize_ruffle_player (application-237cebbe071cb34ce095.js:1) at Object.initialize_all (application-237cebbe071cb34ce095.js:1) at HTMLDocument. (application-237cebbe071cb34ce095.js:1) at f (857-ff5115a2d6ceb23b4291.js:2) at p (857-ff5115a2d6ceb23b4291.js:2) at nrWrapper (4749304?q=status%3Aany:7) Caused by trying to initialize the Ruffle player on non-Flash posts, because we checked for the existence of the `window.RufflePlayer` object to tell if we were on a Flash post, but when using the Ruffle browser extension the RufflePlayer object will always exist. --- app/javascript/src/javascripts/posts.js.erb | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/app/javascript/src/javascripts/posts.js.erb b/app/javascript/src/javascripts/posts.js.erb index ee193ea92..d7c0b374c 100644 --- a/app/javascript/src/javascripts/posts.js.erb +++ b/app/javascript/src/javascripts/posts.js.erb @@ -382,16 +382,15 @@ Post.initialize_ugoira_player = function() { }; Post.initialize_ruffle_player = function() { - if (!window.RufflePlayer) { - return; - } - - let ruffle = window.RufflePlayer.newest(); - let player = ruffle.createPlayer(); let $container = $(".ruffle-container[data-swf]"); - let src = $container.attr("data-swf"); - $container.get(0).appendChild(player); - player.load(src); + + if ($container.length) { + let ruffle = window.RufflePlayer.newest(); + let player = ruffle.createPlayer(); + let src = $container.attr("data-swf"); + $container.get(0).appendChild(player); + player.load(src); + } }; Post.resize_ugoira_controls = function() {