ubershmekel / redditp

Convert any reddit page to a presentation or slide show
https://redditp.com
MIT License
259 stars 101 forks source link

Redgifs Sound Not Enabled By Sound Checkbox [SOLUTION IMMINENT] #167

Open omegasome opened 5 months ago

omegasome commented 5 months ago

As the title says; videos on redgifs play, but play with sound disabled, not matter what the sound checkbox is set to. Videos with sound have to be manually enabled, for each video, with the icon in the upper right hand corner of the embed.

Expected behavior: Selecting the sound checkbox should automatically enable audio on any videos loaded from redgifs.

This seems to have been previously addressed by #148 (which was changed some time after posting from "redgifs" to the defunct-but-functionally-similar site "gfycat"), but it's broken again.

Skip my replies, read this.

As you can see I spent a bit of time narrating what I was doing. The tl;dr is that I made a userscript, compatible with ViolentMonkey but not GreaseMonkey, which will allow redditp (pending a small update) to control sound on RedGIFs without affecting users without the script by (safely!) circumventing the single-origin policy.

redditp just needs to be updated to either call iframe.postMessage("soundOn", "*") on the redgifs iframe or set the url of redgifs embeds to have the querystring ?sound=true, and if the sound checkbox is clicked while a redgifs video is playing then call iframe.postMessage("soundOff", "*") or iframe.postMessage("soundOn", "*") as appropriate.

This would all also apply to gfycat if gfycat weren't defunct.

I would love to go the last step and create a PR to have redditp use this cute lil API I've created, but I think this has reached the edge of my javascript ability for now. I poked around in EmbedIt.js and script.js, wasn't quite sure what to do. I think updateSound would need to look for iframes with a redgifs src and postMessage() accordingly. If it doesn't look exclusively for redgifs iframes it could collide with certain browser extensions. That will handle checking/unchecking sound, the only thing left is to make sure sound gets turned on when the image is loaded. I had not reached the edge of my ability. #168

omegasome commented 5 months ago

In #148 it was said that this could be accomplished by finding a way to enable sound automatically in the embed; however, I have checked with developers in the official redgifs server, and they've confirmed that they deliberately made it so there was no way to do so automatically (with flimsy justifications about preventing content from being stolen?)

I've found that if you can select the .soundOff element and send a click event to it, it easily toggles on sound, but... well, I'm not a JavaScript expert, but results I'm finding are indicating it might be impossible to target the contents of the iframe.

Is that just it then? Redgifs decides there's no way to turn on sound without the mouse being moved to the right spot and the clicked and there's nothing we can do about it?

omegasome commented 5 months ago

OK, I know I'm getting into obscure territory here, but... I've written a userscript that implements the solution called for (which redgifs deliberately does not implement for unclear reasons) by injecting a bit of harmless code into the iframe rather than the page; if the fragment of the iframe url is precisely equal to #sound, then it automatically enables sound (by simulating a button press) soon as the page loads.

This will work for anybody using the iframe, without compromising the security, intellectual property, or commercial activities of either RedGifs or redditp.

So if redditp automatically adds the #sound fragment to the url of redgifs embeds when the sound checkbox is ticked, then any user using this script will experience near-seamless automatically-enabled audio, while users without the script will experience precisely what they do already.

omegasome commented 5 months ago

OK, after further testing, I've confirmed I have it working, but only in ViolentMonkey (not GreaseMonkey, which I was previously using), apparently due to single-origin policy reasons (for some reason GreaseMonkey runs the script with the origin as the page, while ViolentMonkey runs it with the origin as the iframe.)

The userscript can be found at https://greasyfork.org/en/scripts/486812-redgifs-iframe-sound-helper. It does not solve this issue because we still require redditp to actually use it.

The script can (a) automatically load the iframe with sound if the fragment is set to #sound or the querystring includes the parameter sound=true. Additionally. if the sound checkbox is enabled or disabled by the user while the video is playing, redditp can call frame.postMessage("soundOn", "*"); or frame.postMessage("soundOff", "*"); to seamlessly toggle the sound.

Assuming I implemented this correctly, you should not need to wait for the iframe to load before posting the "soundOn" message; once it receives it it will enable sound whenever that becomes possible.

Oh, and, finally, the SOUND_DEFAULT constant can be manually set to true to turn sound on automatically by default (i.e. if no query string or fragment is supplied)

And, as before, allowing redditp to interface with this script will not cause the website to break for users not using the script.