gazza911 / BravoTV/Oxygen - Open Largest Images

// ==UserScript==
// @name         BravoTV/Oxygen - Open Largest Images
// @namespace    TVMaze
// @version      1.08
// @updateURL    https://openuserjs.org/meta/gazza911/BravoTVOxygen_-_Open_Largest_Images.meta.js
// @downloadURL  https://openuserjs.org/install/gazza911/BravoTVOxygen_-_Open_Largest_Images.user.js
// @description  Get the largest BravoTV/Oxygen images of each type
// @author       gazza911
// @match        https://www.oxygen.com/*
// @match        https://www.bravotv.com/*
// @icon         https://www.google.com/s2/favicons?domain=bravotv.com
// @grant        GM_openInTab
// @grant        GM_registerMenuCommand
// @require      https://code.jquery.com/jquery-3.7.1.min.js
// @license      MIT
// ==/UserScript==

(function() {
   $("body").on("mousedown", '.player__inner-wrapper,.video-card__thumbnail,picture,figure', async function(e) {
       var $container = $(this).attr("class") === "player__inner-wrapper" ? $(this).find("picture") : $(this);
       if (e.which === 3) {
           var img = await getLargestImageByElements($container.children("source,img"));
           if (img) {
               GM.openInTab(img);
           }
       }
   });
})();

async function getLargestImageByElements(elements) {
    var highest = 0;
    var image;
    var onlyThumbnails = true;
    if (elements.length > 0) {
        elements.each(function(i) {
            var width = parseInt($(this).attr("width") || $(this).width());
            var src = $(this).attr("srcset") || $(this).attr("src");
            if (src.indexOf(" ") > 0) {
                src = src.split(" ")[0];
            }
            if (src.indexOf("thumbnail") === -1) {
                onlyThumbnails = false;
            }
            if (width > highest) {
                image = src;
                highest = width;
            }
        });

        if (image.indexOf("bravo-video.nbcuni.com") > 0) {
            image = "https://bravo-video.nbcuni.com" + image.split("bravo-video.nbcuni.com")[1];
        }
        else if (onlyThumbnails) {
            var match = image.match(/(.*)(_\d+x\d+.*)(\.jpg|\.png).*/);
            var original = image;
            // Route SD pictures through NBC instead
            if (image.indexOf("proxy_SD") > 0) {
                var slashes = image.split("/");
                var path = slashes[slashes.length - 1];
                var matches = /.*(proxy_SD_.*)_\d+x\d+_\d+/g.exec(path);
                if (!matches) {
                    image = "https://img.nbc.com/files/" + path;
                }
                else if (matches.length === 2) {
                    image = "https://img.nbc.com/files/" + path.split("proxy_SD_")[0] + matches[1] + ".jpg";
                }
                else {
                    image = "https://img.nbc.com/files/" + matches[0] + ".jpg";
                }
            } else {
                var res = await fetch(image.replace("watch_thumbnail__tablet","episode_detail__tablet__1_5x"), { method: "HEAD" });
                if (res.ok) {
                    image = image.replace("watch_thumbnail__tablet","episode_detail__tablet__1_5x");
                } else if (match) {
                    image = match[1].replace("watch_thumbnail__tablet","episode_detail__tablet__1_5x") + match[3] + "?id=" + match[2];
                }
            }
        }
        if (image.indexOf("http") !== 0) {
            image = "https://" + window.location.host + image;
        }
        var final = await fetch(image, { method: "HEAD" });
        if (!final.ok) {
            if (original.indexOf("http") !== 0) {
                image = "https://" + window.location.host + original;
            } else {
                image = original;
            }
            alert('Larger image could not be found, try going to the episode page');
        }
    }
    return image;
}