ladroop2 / copy playlist from camsites

// ==UserScript==
// @name         copy playlist from camsites
// @namespace    everywhere
// @version      1.0.2
// @description  Play video from stripchat, chaturbate and many other cam sites in vlc, potplayer or a streamrecorder
// @author       ladroop
// @copyright    2024, ladroop2 (https://openuserjs.org/users/ladroop2)
// @match        https://*.stripchat.com/*
// @match        https://*.camsoda.com/*
// @match        https://*streamray.com/*
// @match        https://*cams.com/*
// @match        https://www.streamate.com/cam/*
// @match        https://*streamate.com/cam/*
// @match        https://*.cam4.com/*
// @match        https://*.bongacams.com/*
// @match        https://www.flirt4free.com/*
// @match        https://www.xlovecam.com/*
// @match        https://www.myfreecams.com/*
// @match        https://dreamcam.com/*
// @match        https://www.olecams.com/*
// @match        https://*showup.tv/*
// @match        https://*chaturbate.com/*
// @license      MIT
// @updateURL    https://openuserjs.org/meta/ladroop2/copy_playlist_from_camsites.meta.js
// @grant        none
// ==/UserScript==



(function() {
    'use strict';

    makepopitup();
    var location=document.location.href;
    var n=0;
    var playlist="";
    var reload=false;
    var needreload=false;
    var observer= new MutationObserver(pagechange);
    var observerConfig = {subtree: true, characterData: true, childList: true };

    if (location.indexOf("xlovecam.com")!=-1){
        needreload=true;
    }
    if (location.indexOf("camsoda")!=-1){
        needreload=true;
        localStorage.clear();
    }
    if (location.indexOf("bongacams")!=-1){
        needreload=true;
        localStorage.clear();
    }
    pagechange1();

    observer.observe(document.getElementsByTagName("head")[0],observerConfig);

    function pagechange(){
        if (location != document.location.href){
            location=document.location.href;
            pagechange1();
        }
    }

    function pagechange1(){
        if(needreload&&reload){
            document.location.reload();
        }
        reload=true;
        playlist="";
        document.getElementById("popitup1").style.display="none";
        document.getElementById("popitup").innerHTML="";
        setTimeout(selectsite,5000);
    }

    function selectsite(){
        n=0;
        if (location.indexOf("showup")!=-1){
            showup();
            return;
        }
        if (location.indexOf("olecams")!=-1){
            olecams();
            return;
        }
        if (location.indexOf("dreamcam")!=-1){
            dreamcam();
            return;
        }
        if (location.indexOf("chaturbate")!=-1){
            chaturbate();
            return;
        }
        if (location.indexOf("myfreecams")!=-1){
            mfc();
            return;
        }
        if (location.indexOf("stripchat")!=-1){
            stripchat();
            return;
        }
        if (location.indexOf("camsoda")!=-1){
            camsoda();
            return;
        }
        if (location.indexOf("bongacams")!=-1){
            bongacams();
            return;
        }
        if (location.indexOf("streamray")!=-1){
            streamray();
            return;
        }
        if (location.indexOf("cams.com")!=-1){
            streamray();
            return;
        }
        if (location.indexOf("streamate")!=-1){
            streamate();
            return;
        }
        if (location.indexOf("cam4")!=-1){
            cam4();
            return;
        }
        if (location.indexOf("flirt4free.com")!=-1){
            f4f();
            return;
        }
        if (location.indexOf("xlovecam.com")!=-1){
            xlovecam();
            return;
        }
    }

    function showup(){
        var scripts=document.getElementsByTagName("script");
        var preload="";
        for (n=0; n<scripts.length; n++){
            if (scripts[n].innerHTML.indexOf("var csrfToken =")==0){
                preload=scripts[n].innerHTML;
                break;
            }
        }
        var cdn1=preload.split("stormStreamingAddr = '")[1].split("'")[0];
        var cdn2=preload.split("transcoderAddr = '")[1].split("'")[0];
        var id=preload.split("streamID = '")[1].split("'")[0];
        playlist="https://"+cdn1+"/h5live/http/playlist.m3u8?url=rtmp%3A%2F%2F"+cdn2+"%3A1935%2Fwebrtc&stream="+id+"_aac&cid=713.675&pid=9.281.485.628";
        document.getElementById("popitup1").style.display="block";
        document.getElementById("popitup").innerHTML=playlist;
    }

    function olecams(){
        var modelnr=parseInt(document.querySelector('[property="og:image"]').getAttribute("content").split("/")[5]);
        var url="https://apiv2.olecams.com/rooms/connect";
         fetch(url,{
             headers: {"Content-Type": "application/json",},
             credentials: "same-origin",
             method: "POST",
             body: JSON.stringify({"id":modelnr,"mode":"free","nick":null,"afno":null,"sitemode":"REGISTERED","paymenttype":null,"ttl":1800})
         }).then(
             function(response) {
                 if (response.status !== 200){
                       return;
                 }
                 response.json().then(function(roomdata) {
                     var manifesturl=roomdata.message.manifest+"?&accessToken="+roomdata.message.accessToken
                     olecams2(manifesturl);
                 });
             });
    }
    function olecams2(url){
        fetch(url,{ credentials: "same-origin"}).then(
             function(response) {
                 if (response.status !== 200){
                       return;
                 }
                 response.json().then(function(roomdata) {
                     playlist=roomdata.formats["mp4-hls"].encodings[2].location;
                     document.getElementById("popitup1").style.display="block";
                     document.getElementById("popitup").innerHTML=playlist;
                 });
             });
    }

    function dreamcam(){
        var model=location.split("/")[4];
        var url="https://bss.dreamcamtrue.com/api/clients/v1/broadcasts/models/"+model+"?partnerId=dreamcam_oauth2&show-hidden=true&stream-types=video2D";
           fetch(url,{ credentials: "same-origin"}).then(
             function(response) {
                 if (response.status !== 200){
                       return;
                 }
                 response.json().then(function(roomdata) {
                     playlist=roomdata.streams[1].url
                     if (playlist==null){return;}
                     document.getElementById("popitup1").style.display="block";
                     document.getElementById("popitup").innerHTML=playlist;
                 });
             });
    }

    function chaturbate(){
        var model=location.split("/")[3];
        if (model==""){return;}
        var url="https://chaturbate.com/api/chatvideocontext/"+model+"/";
         fetch(url,{ credentials: "same-origin"}).then(
             function(response) {
                 if (response.status !== 200){
                       return;
                 }
                 response.json().then(function(roomdata) {
                     playlist=roomdata.hls_source.split("?")[0];
                     if (playlist==""){return;}
                     document.getElementById("popitup1").style.display="block";
                     document.getElementById("popitup").innerHTML=playlist;
                 });
             });

    }

    function mfc(){
        var model=location.split("#")[1];
        var jsonfile="https://api-edge.myfreecams.com/usernameLookup/"+model;
        fetch(jsonfile,{ credentials: "same-origin"}).then(
            function(response){
                if (response.status !== 200){
                    return;
                }
                response.json().then(function(roomdata) {
                    var modelnr=roomdata.result.user.id;
                    var videoserver=roomdata.result.user.sessions[0].server_name;
                    var phase=roomdata.result.user.sessions[0].phase;
                    mfc2(modelnr,videoserver,phase);
                });
            });
    }
    function mfc2(modelnr,videoserver,phase){
        modelnr=modelnr+100000000;
        var servernr=videoserver.split("ideo")[1];
        playlist="https://edgevideo.myfreecams.com/llhls/NxServer/"+servernr+"/ngrp:mfc_"+phase+modelnr+".f4v_cmaf/playlist.m3u8?nc=0.6190622874050598&v=1.97.23";
        fetch(playlist,{ credentials: "same-origin"}).then(
            function(response) {
                if (response.status !== 200) {
                    return;
                }
                response.text().then(function(data) {
                    var chunk="chunklist"+data.split("chunklist")[1].split("m3u8")[0]+"m3u8";
                    playlist="https://"+videoserver+".myfreecams.com/NxServer/ngrp:mfc_"+phase+modelnr+".f4v_cmaf/"+chunk+"?nc=0.813118007341&v=1.96";
                    document.getElementById("popitup1").style.display="block";
                    document.getElementById("popitup").innerHTML=playlist;
                });
            });
    }

    function xlovecam(){
        var initial =document.getElementById("app-data").innerHTML;
        var playdata=initial.split('"hlsPlaylistFree":"')[1].split('"')[0];
        var playdatachop=playdata.split("\\");
        playlist=playdatachop.join("");
        document.getElementById("popitup1").style.display="block";
        document.getElementById("popitup").innerHTML=playlist;
    }

    function f4f(){
        var modelnr=document.getElementById("bodyTag").getAttribute("data-current-model-id");
        var jsonfile="https://www.flirt4free.com/ws/chat/get-stream-urls.php?model_id="+modelnr;
        fetch(jsonfile,{ credentials: "same-origin"}).then(
            function(response){
                if (response.status !== 200){
                    return;
                }
                response.json().then(function(roomdata) {
                    playlist="https:"+roomdata.data.hls[0].url;
                    document.getElementById("popitup1").style.display="block";
                    document.getElementById("popitup").innerHTML=playlist;
                });
            });

    }

    function bongacams(){
        var initial=document.querySelector('[data-type="initialState"]').innerHTML;
        var model=initial.split('"username":"')[1].split('"')[0];
        var edge="live-"+initial.split("mobile-")[1].split(".com")[0];
        playlist="https://"+edge+".com/hls/stream_"+model+"/playlist.m3u8";
        document.getElementById("popitup1").style.display="block";
        document.getElementById("popitup").innerHTML=playlist;
    }

    function cam4(){
        var model=location.split("/")[3];
        var jsonfile="https://cam4.com/rest/v1.0/profile/"+model+"/streamInfo";
        fetch(jsonfile,{ credentials: "same-origin"}).then(
            function(response){
                if (response.status !== 200){
                    return;
                }
                response.json().then(function(roomdata) {
                    playlist=roomdata.cdnURL;
                    document.getElementById("popitup1").style.display="block";
                    document.getElementById("popitup").innerHTML=playlist;
                });
            });
    }

    function streamate(){
        var model=location.split("/")[4];
        var jsonfile="https://manifest-server.naiadsystems.com/live/s:"+model+".json";
        fetch(jsonfile,{ credentials: "same-origin"}).then(
            function(response){
                if (response.status !== 200){
                    return;
                }
                response.json().then(function(roomdata) {
                    playlist=roomdata.formats["mp4-hls"].manifest.split("?")[0];
                    document.getElementById("popitup1").style.display="block";
                    document.getElementById("popitup").innerHTML=playlist;
                });
            });
    }

    function streamray(){
        if (document.getElementsByTagName("video").length==0){return;}
        var model=location.split("/")[3].split("#")[0].toLowerCase();
        if (model==""){return;}
        if (model=="webcam"){return;}
        playlist="https://camscdn.cams.com/camscdn/cdn-"+model+".m3u8";
        document.getElementById("popitup1").style.display="block";
        document.getElementById("popitup").innerHTML=playlist;
    }

    function camsoda(){
        var scripts=document.getElementsByTagName("script");
        var preload="";
        for (n=0; n<scripts.length; n++){
            if (scripts[n].innerHTML.indexOf("window.__PRELOADED_STATE__ =")==0){
                preload=scripts[n].innerHTML;
                break;
            }
        }
        var model=location.split("/")[3];
        var edge=preload.split('"edge_servers":["')[1].split('"')[0];
        var streamname=preload.split('"stream_name":"')[1].split('"')[0];
        var token=preload.split('"token":"')[1].split('"')[0];
        playlist="https://"+edge+"/"+streamname+"_v1/index.m3u8?token="+token;
        document.getElementById("popitup1").style.display="block";
        document.getElementById("popitup").innerHTML=playlist;
    }

    function stripchat(){
        var model=location.split("/")[3];
        var findstring='alt="'+model+' Live chatroom"';
        var modelimg=document.querySelector('['+findstring+']');
        var modelnumber=modelimg.src.split("/")[5].split("_webp")[0];
        playlist="https://edge-hls.doppiocdn.com/hls/"+modelnumber+"/master/"+modelnumber+"_auto.m3u8";
        document.getElementById("popitup1").style.display="block";
        document.getElementById("popitup").innerHTML=playlist;
    }

    function makepopitup(){
        var popstyle="color:black;z-index:100000;top:10px;left:10px;box-shadow:0px 0px 32px rgba(0, 0, 0, 0.32);border-radius:4px;border:1px solid rgb(221, 221, 221);background-color:rgb(200, 200, 200);position:fixed; display:none; white-space: pre-line; text-align: left; line-height: 1.4; height: auto; width:500px; padding: 10px 10px 10px 10px; box-sizing: border-box; overflow-wrap: break-word; word-break: break-word; padding: 15px;";
        var newelem=document.createElement('span');
        newelem.setAttribute("style", popstyle);
        newelem.id="popitup1";
        var newdiv=document.createElement('div');
        newdiv.id="popitup";
        newelem.appendChild(newdiv);
        newdiv=document.createElement('div');
        newdiv.innerHTML="\n(Close)";
        newdiv.style.float="left";
        newdiv.style.cursor="pointer";
        newdiv.addEventListener("click", close );
        newelem.appendChild(newdiv);
        newdiv=document.createElement('div');
        newdiv.id="closecopy";
        newdiv.innerHTML="\n(Close and copy to clipbord)";
        newdiv.style.float="right";
        newdiv.style.cursor="pointer";
        newdiv.addEventListener("click", closecopy );
        newelem.appendChild(newdiv);
        document.getElementsByTagName("body")[0].appendChild(newelem);
    }

    function closecopy(){
        navigator.clipboard.writeText(playlist);
        document.getElementById("popitup1").style.display="none";
    }

    function close(){
        document.getElementById("popitup1").style.display="none";
    }

})();