Kongregate Flash games fixer add-on - default swf player setting

Sets the Kongregate Flash games fixer's default swf player to whatever you prefer.

// ==UserScript==
// @name         Kongregate Flash games fixer add-on - default swf player setting
// @version      5
// @author       Matrix4348
// @namespace    https://greasyfork.org/users/4818
// @description  Sets the Kongregate Flash games fixer's default swf player to whatever you prefer.
// @license      MIT
// @match        *://www.kongregate.com/games/*/*
// @match        *://www.kongregate.com/*/games/*/*
// @run-at       document-end
// @grant        GM_setValue
// @grant        GM_getValue
// ==/UserScript==

var current_player;

function FlashSupport(){
    var np=navigator.plugins, npl=np.length;
    for(var k=0;k<npl;k++){ if((np[k].name=="Shockwave Flash")&(np[k].filename!="ruffle.js")){return true;} }
    return false;
}

function return_default_behaviour(){
    if(FlashSupport()==true){ return "Flash"; }
    else if(navigator.userAgent.search("Windows")>-1){ return "SuperNova"; } // At least while Ruffle is not finished.
    else if(navigator.userAgent.search("Windows")==-1){ return "Ruffle"; } // Because SuperNova only works on Windows.
    else { return "Flash"; }
}

function open_settings(){
    var default_player=prompt("Which swf player must be used by default (when a page loads, not when buttons are clicked on): Flash, SuperNova, Ruffle or AwayFL?\n\n Note: Typing anything else will be treated like "+return_default_behaviour()+".");
    switch((default_player||"null").toLowerCase()) {
        case "null":
            break;
        case "flash":
            GM_setValue("default_swf_player","Flash");
            break;
        case "supernova":
            GM_setValue("default_swf_player","SuperNova");
            break;
        case "ruffle":
            GM_setValue("default_swf_player","Ruffle");
            break;
        case "awayfl":
            GM_setValue("default_swf_player","AwayFL");
            break;
        default:
            GM_setValue("default_swf_player",return_default_behaviour());
    }
    document.getElementById("default swf player setting button").innerHTML="Set default swf player <br> <i>Currently: "+GM_getValue( "default_swf_player", return_default_behaviour() )+"</i>";
}

function create_settings_button(){
    var b=document.createElement("button");
    b.id="default swf player setting button";
    b.innerHTML="Set default swf player <br> <i>Currently: "+GM_getValue( "default_swf_player", return_default_behaviour() )+"</i>";
    b.onclick=open_settings;
    b.style.borderWidth="1px";
    b.style.backgroundColor="grey";
    var h=50,w=150;
    b.style.height=h+"px";
    b.style.width=w+"px";
    b.style.position="relative";
    b.style.top=document.getElementById("maingame").clientHeight*(-1)+"px";
    //b.style.left=document.getElementById("maingame").clientWidth*(-1/2)-w*1/2+"px";
    b.style.left=document.getElementById("maingame").clientWidth*(-1/2)+"px";
    document.getElementById("floating_game_holder").appendChild(b);
}

function create_second_button(){
    var b=document.createElement("button");
    b.id="default swf player setting button 2";
    b.innerHTML="Set default swf player to the currently used one ("+return_current_player()+")";
    b.onclick = function(){
        GM_setValue("default_swf_player",return_current_player());
        document.getElementById("default swf player setting button").innerHTML="Set default swf player <br> <i>Currently: "+return_current_player()+"</i>";
    };
    b.style.borderWidth="1px";
    b.style.backgroundColor="grey";
    var h=50,w=150;
    b.style.height=h+"px";
    b.style.width=w+"px";
    b.style.position="relative";
    b.style.top=document.getElementById("maingame").clientHeight*(-1)+document.getElementById("default swf player setting button").clientHeight+13+"px";
    b.style.left=document.getElementById("maingame").clientWidth*(-1/2)-w+"px";
    document.getElementById("floating_game_holder").appendChild(b);
}

function update_current_player(p){
    current_player = p;
}

function return_current_player(){
    return current_player;
}

function add_event_listeners(){
    var players = ["Flash","Ruffle","SuperNova","AwayFL"];
    for(let p of players){
        document.getElementById("Button to (re)load with "+p).addEventListener("click",function(){
            update_current_player(p);
            document.getElementById("default swf player setting button 2").innerHTML="Set default swf player to the currently used one ("+return_current_player()+")";
        });
    }
}

function main_function(){
    var ql=document.getElementById("quicklinks");
    var is_Flash_game, everything_ready;
    if( document.readyState=="complete" && typeof(active_user)!="undefined" && typeof(activateGame)!="undefined" && typeof(holodeck)!="undefined" && ql!==null ){
        if(typeof(active_user.gameType)!="undefined"){
            if(active_user.gameType()=="flash"){ is_Flash_game=true; } else{ is_Flash_game=false; }
        }
        else{ is_Flash_game=-1; }
        if(typeof(LoadFrame4348)!="undefined" && document.getElementById("Button to (re)load with AwayFL")!=undefined ){ everything_ready=true; }
        else{ everything_ready=false; }
    }

    if(is_Flash_game==true&&everything_ready){
        var swf_player=GM_getValue( "default_swf_player", return_default_behaviour() );
        update_current_player(swf_player);
        create_settings_button();
        add_event_listeners();
        create_second_button();
        LoadFrame4348(swf_player);
    }
    else if(is_Flash_game!=false){ setTimeout(function(){ main_function(); },1000); }
}

main_function();