ffactory / Bauer sucht frau script

// ==UserScript==
// @name         Bauer sucht frau script
// @namespace    https://ffactory.ml/
// @version      0.1
// @description  love yu :)
// @author       Filippo Orru
// @match        https://www.atv.at/tv/bauer-sucht-frau/*
// @icon         https://www.google.com/s2/favicons?domain=mozilla.org
// @updateURL    https://openuserjs.org/meta/ffactory/Bauer_sucht_frau_script.meta.js
// @downloadURL  https://openuserjs.org/install/ffactory/Bauer_sucht_frau_script.user.js
// @copyright    2021, ffactory (https://openuserjs.org/users/ffactory)
// @license      MIT
// @grant        none
// @run-at document-end
// ==/UserScript==

(function () {
    'use strict';
    if (window.top != window.self) { //-- Don't run on frames or iframes
        return;
    }

    function main() {
        let ws
        const video = document.getElementsByClassName("_oasis_video")[0];
        // State
        let joinedGroup = false;
        let ignoreNextSend = false;
        let ignoreNextReceive = false;

        function createWs(host, groupName) {
            ws = new WebSocket('wss://' + host + "/");

            ws.onopen = function () {
                ws.send("HELLO," + groupName);
                joinedGroup = true;
            }

            ws.onmessage = function (e) {
                let parts = e.data.split(",");
                if (parts.length > 1 && parts[0] == 'EVENT') {
                    if (ignoreNextReceive === true) {
                        ignoreNextReceive = false;
                    } else {
                        ignoreNextSend = true;
                        let eventType = parts[1];
                        let totalDuration = parseFloat(parts[2]);
                        let currentTime = parseFloat(parts[3]);
                        if (totalDuration === video.duration) {
                            if (eventType == 'PLAY') {
                                video.play();
                            } else if (eventType == 'PAUSE') {
                                video.pause();
                                //} else if (eventType == 'SETTIME') {
                            }
                            video.currentTime = currentTime;
                        }
                    }
                }
            };

            ws.onclose = function () {
                console.log("closed");
            };

            ws.onerror = function (e) {
                console.log("error", e)
            };
        }

        function createForm() {
            let label = document.createElement("label");
            label.innerText = "Enter a group name:";
            label.for = "groupName";
            let input = document.createElement("input");
            input.type = "text";
            input.name = "groupName";
            let button = document.createElement("button");
            button.innerText = "Join group!";
            let form = document.createElement("form");
            form.id = "groupNameForm";
            form.appendChild(label);
            form.appendChild(input);
            form.appendChild(button);
            form.addEventListener('submit', function (e) {
                e.preventDefault();
                e.stopPropagation();
                let input = document.querySelector('input[name=groupName]').value;
                let host = input.split("@")[0];
                let groupName = input.split("@")[1];
                if (groupName && groupName.length > 3) {
                    video.classList.remove("hidden");
                    createWs(host, groupName);
                }
            });
            document.getElementsByClassName("video-player-parking-lot ")[0].appendChild(form);
        }

        createForm();

        video.addEventListener("play", function () {
            if (video.duration < 300) { // skip ad
                video.currentTime = video.duration - 1;
            } else {
                if (joinedGroup) {
                    if (!ignoreNextSend) {
                        ignoreNextReceive = true;
                        ws.send("EVENT,PLAY," + video.duration.toString() + "," + video.currentTime.toString());
                    } else {
                        ignoreNextSend = false;
                    }
                }
            }
        });
        video.addEventListener("pause", function () {
            if (joinedGroup) {
                if (!ignoreNextSend) {
                    ignoreNextReceive = true;
                    ws.send("EVENT,PAUSE," + video.duration.toString() + "," + video.currentTime.toString());
                } else {
                    ignoreNextSend = false;
                }
            }
        });
    }

    let startupInterval

    function startup() {
        if (document.getElementsByClassName("_oasis_video").length > 0) {
            window.clearInterval(startupInterval);
            main();
        } else {
            console.log("waiting for startup");
        }
    }

    startupInterval = window.setInterval(startup, 4000);
})();