From a6907e406435c585f3ecbc4d4cb37ecbdba03251 Mon Sep 17 00:00:00 2001 From: Omegazette Date: Tue, 6 Feb 2024 23:04:05 -0900 Subject: [PATCH 1/6] Made embedit.redGifConvert automatically add #sound fragment when sound is on. No effect on users not using the userscript. --- js/EmbedIt.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/js/EmbedIt.js b/js/EmbedIt.js index 8f3e41a..899bcfd 100644 --- a/js/EmbedIt.js +++ b/js/EmbedIt.js @@ -73,7 +73,7 @@ embedit.redGifConvert = function (url, embedFunc) { // https://github.com/ubershmekel/redditp/issues/138 // Redgifs isn't allowing CORS requests to others. // access-control-allow-origin: https://www.redgifs.com - const iframeUrl = 'https://www.redgifs.com/ifr/' + name; + const iframeUrl = 'https://www.redgifs.com/ifr/' + name + (rp.settings.sound ? "#sound" : ""); embedFunc($('')); return true; }; From cc720f14f3f43cb1c62d2d3717e3b60e6582035a Mon Sep 17 00:00:00 2001 From: Omegazette Date: Tue, 6 Feb 2024 23:28:12 -0900 Subject: [PATCH 2/6] Extended updateSound method in script.js to post the soundOn/soundOff messages to embedded redgifs iframes when toggled. --- js/script.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/js/script.js b/js/script.js index aee4899..741c1e9 100644 --- a/js/script.js +++ b/js/script.js @@ -17,7 +17,7 @@ rp.settings = { timeToNextSlide: 6 * 1000, cookieDays: 300, nsfw: true, - sound: false + sound: false, }; rp.session = { @@ -232,6 +232,17 @@ $(function () { } else { console.log(audioTags); } + var iframes = document.getElementsByTagName('iframe'); // turn sound on/off in embeds. This only works if the embed implements the soundOn/soundOff postMessage API. + for (var index=0, len = iframes.length; index < len; index++) { // we have to iterate over all the iframes because some extensions add extra ones to the page—this would break if e.g. tridactyl was installed. + const iframe = iframes[index]; + if (iframe.src.indexOf("redgifs") > -1) { // mostly for redgifs + if (rp.settings.sound) { + iframe.contentWindow.postMessage("soundOn", "*"); + } else { + iframe.contentWindow.postMessage("soundOff", "*"); + } + } + } }; var resetNextSlideTimer = function () { From b75b89c9bf4e0785d51b9d938b47e208cb3e6169 Mon Sep 17 00:00:00 2001 From: Omegazette Date: Wed, 7 Feb 2024 20:00:36 -0900 Subject: [PATCH 3/6] Switched RedGIFs to use query string instead of fragment; made it turn off links (the "image" button does the same thing); allowed it to disable looping (for the next thing I'm doing); made it detect if the userscript that does all these things is actually, yknow, installed --- js/EmbedIt.js | 2 +- js/script.js | 17 ++++++++++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/js/EmbedIt.js b/js/EmbedIt.js index 899bcfd..0049cde 100644 --- a/js/EmbedIt.js +++ b/js/EmbedIt.js @@ -73,7 +73,7 @@ embedit.redGifConvert = function (url, embedFunc) { // https://github.com/ubershmekel/redditp/issues/138 // Redgifs isn't allowing CORS requests to others. // access-control-allow-origin: https://www.redgifs.com - const iframeUrl = 'https://www.redgifs.com/ifr/' + name + (rp.settings.sound ? "#sound" : ""); + const iframeUrl = 'https://www.redgifs.com/ifr/' + "?link=false&loop=" + !rp.settings.shouldAutoNextSlide + "&sound=" + rp.settings.sound; embedFunc($('')); return true; }; diff --git a/js/script.js b/js/script.js index 741c1e9..6e7a4fa 100644 --- a/js/script.js +++ b/js/script.js @@ -36,7 +36,9 @@ rp.session = { foundOneImage: false, - loadingNextImages: false + loadingNextImages: false, + + gfy_enhanced_api: false, }; // Variable to store the images we need to set as background @@ -88,7 +90,16 @@ $(function () { // this fadeout was really inconvenient on mobile phones // and instead the minimize buttons should be used. //setupFadeoutOnIdle(); - + + window.onmessage = function(message) { + if (message.data === "gfy_enhanced_api") { + rp.session.gfy_enhanced_api = true; + } + if (message.data === "gfy_ended") { + // next image + } + } + var getNextSlideIndex = function (currentIndex) { if (!rp.settings.nsfw) { // Skip any nsfw if you should @@ -232,7 +243,7 @@ $(function () { } else { console.log(audioTags); } - var iframes = document.getElementsByTagName('iframe'); // turn sound on/off in embeds. This only works if the embed implements the soundOn/soundOff postMessage API. + var iframes = document.getElementsByClassName('iframe'); // turn sound on/off in embeds. This only works if the embed implements the soundOn/soundOff postMessage API. for (var index=0, len = iframes.length; index < len; index++) { // we have to iterate over all the iframes because some extensions add extra ones to the page—this would break if e.g. tridactyl was installed. const iframe = iframes[index]; if (iframe.src.indexOf("redgifs") > -1) { // mostly for redgifs From 89cdf5d5ad994c3deb08e5b06c8f44b02df30702 Mon Sep 17 00:00:00 2001 From: Omegazette Date: Wed, 7 Feb 2024 20:11:12 -0900 Subject: [PATCH 4/6] Refactored it so gfy iframes have a class, so I don't need to do that ugly thing to search for them while excluding iframes created by extensions. Should've thought of that in the first place. --- js/EmbedIt.js | 4 ++-- js/script.js | 26 +++++++++++++------------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/js/EmbedIt.js b/js/EmbedIt.js index 0049cde..e2ab0af 100644 --- a/js/EmbedIt.js +++ b/js/EmbedIt.js @@ -73,8 +73,8 @@ embedit.redGifConvert = function (url, embedFunc) { // https://github.com/ubershmekel/redditp/issues/138 // Redgifs isn't allowing CORS requests to others. // access-control-allow-origin: https://www.redgifs.com - const iframeUrl = 'https://www.redgifs.com/ifr/' + "?link=false&loop=" + !rp.settings.shouldAutoNextSlide + "&sound=" + rp.settings.sound; - embedFunc($('')); + const iframeUrl = 'https://www.redgifs.com/ifr/' + name + "?link=false&loop=" + !rp.settings.shouldAutoNextSlide + "&sound=" + rp.settings.sound; + embedFunc($('')); return true; }; diff --git a/js/script.js b/js/script.js index 6e7a4fa..06fa956 100644 --- a/js/script.js +++ b/js/script.js @@ -92,12 +92,13 @@ $(function () { //setupFadeoutOnIdle(); window.onmessage = function(message) { - if (message.data === "gfy_enhanced_api") { - rp.session.gfy_enhanced_api = true; - } - if (message.data === "gfy_ended") { - // next image - } + if (message.data === "gfy_enhanced_api") { + rp.session.gfy_enhanced_api = true; + } + if (message.data === "gfy_ended") { + if (rp.settings.shouldAutoNextSlide) + nextSlide(); + } } var getNextSlideIndex = function (currentIndex) { @@ -243,16 +244,15 @@ $(function () { } else { console.log(audioTags); } - var iframes = document.getElementsByClassName('iframe'); // turn sound on/off in embeds. This only works if the embed implements the soundOn/soundOff postMessage API. - for (var index=0, len = iframes.length; index < len; index++) { // we have to iterate over all the iframes because some extensions add extra ones to the page—this would break if e.g. tridactyl was installed. - const iframe = iframes[index]; - if (iframe.src.indexOf("redgifs") > -1) { // mostly for redgifs + var iframeTags = document.getElementsByClassName('gfyframe'); // turn sound on/off in embeds. This only works if the embed implements the soundOn/soundOff postMessage API. + if (iframeTags.length === 1) { if (rp.settings.sound) { - iframe.contentWindow.postMessage("soundOn", "*"); + iframeTags[0].contentWindow.postMessage("soundOn", "*"); } else { - iframe.contentWindow.postMessage("soundOff", "*"); + iframe.contentWindow.postMessage("soundOff", "*"); } - } + } else { + console.log(iframeTags); } }; From 7d67894a4869df1fc6c0d95c9a21010e75be81f3 Mon Sep 17 00:00:00 2001 From: Omegazette Date: Wed, 7 Feb 2024 20:21:16 -0900 Subject: [PATCH 5/6] Last commit made it automatically move to the next image when it receives the gfy_ended signal. This one makes it disable the auto-next timer when it sees a gfy image AND rp.session.gfy_enhanced_api is set. It skips to next on a timer when typical when the script isn't present, but waits when it is. --- js/script.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/js/script.js b/js/script.js index 06fa956..074532a 100644 --- a/js/script.js +++ b/js/script.js @@ -716,6 +716,13 @@ $(function () { }); } }; + + var startPlayingGfy = function (gfyframe) { + if (rp.settings.shouldAutoNextSlide && rp.session.gfy_enhanced_api) { + // If gfy_enhanced_api is false, then there's no way for it to detect when the video ends, and it should just use the timeout + clearTimeout(rp.session.nextSlideTimeoutId); + } + } // // Slides the background photos @@ -742,6 +749,11 @@ $(function () { if (maybeVid.length > 0) { startPlayingVideo(maybeVid); } + + var maybeGfy = $('.gfyframe'); + if (maybeGfy.length > 0) { + startPlayingGfy(maybeGfy); // I don't think I need to actually duplicate the code from above like this, but this might make things easier to refactor later. + } }); }; From 5083e21815edc68013274685a0d3059d77d10efe Mon Sep 17 00:00:00 2001 From: Omegazette Date: Thu, 8 Feb 2024 01:40:49 -0900 Subject: [PATCH 6/6] Fixed a mistyped variable name --- js/script.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/js/script.js b/js/script.js index 074532a..36f36ca 100644 --- a/js/script.js +++ b/js/script.js @@ -249,7 +249,7 @@ $(function () { if (rp.settings.sound) { iframeTags[0].contentWindow.postMessage("soundOn", "*"); } else { - iframe.contentWindow.postMessage("soundOff", "*"); + iframeTags[0].contentWindow.postMessage("soundOff", "*"); } } else { console.log(iframeTags);