Kongvenience

Improves Kongregate game pages: adds a button for reloading game frame, removes anti-ad blockers banner, automatically launches games and hides right part of the page.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(У мене вже є менеджер скриптів, дайте мені встановити його!)

Advertisement:

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

Advertisement:

// ==UserScript==
// @name         Kongvenience
// @namespace    https://greasyfork.org/users/4818
// @version      5
// @author       Matrix4348
// @description  Improves Kongregate game pages: adds a button for reloading game frame, removes anti-ad blockers banner, automatically launches games and hides right part of the page.
// @license      MIT
// @match        *://www.kongregate.com/games/*/*
// @match        *://www.kongregate.com/*/games/*/*
// @match        *://www.kongregate.com/games/*/*/embed*
// @match        *://www.kongregate.com/*/games/*/*/embed*
// @run-at       document-start
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_registerMenuCommand
// @grant        unsafeWindow
// ==/UserScript==

function add_reload_button(){
    var fullscreen_button = document.getElementById("fullscreen_button");
    var button_area = fullscreen_button?.parentElement;
    if( document.readyState == "complete" && button_area !== null ){
        var button = document.createElement("button");
        button.classList.add("hide-if-kfgf"); // The button will be hidden if the Kongregate Flash games fixer user script is also running.
        button_area.insertBefore(button,fullscreen_button.previousElementSibling);
        var a = document.createElement("a");
        a.id="kongvenience-reload-button";
        a.innerHTML="Reload";
        a.addEventListener("click", reload_game);
        button.appendChild(a);
    }
    else{ setTimeout(function(){ add_reload_button(); },1000); }
}

function reload_game(){
    document.getElementById("play-now-overlay").getElementsByTagName("BUTTON")?.[0].click();
    var game = document.getElementById("game");
    if(game.style.display != "none"){
        game.querySelector("iframe").src = game.querySelector("iframe").src;
    }
}

function autoplay(x){
    if( document.readyState == "complete" && document.getElementById("play-now-overlay") !== null ){
        var waiting_time; // For Flash games, we will wait before clicking the "play now" button in case the Kongregate Flash games fixer is running in parallel because it must supersede Kongvenience.
        try{ waiting_time = unsafeWindow[0].active_user.gameType() == "flash" ? 5000 : 100; }
        catch(e){ waiting_time = 100; }
        setTimeout(function(){
            if( document.getElementById("game")?.style?.display == "none" ){
                document.getElementById("play-now-overlay").getElementsByTagName("BUTTON")?.[0].click();
            }
        }, waiting_time);
    }
    else{ setTimeout(function(y){ autoplay(y); }, 1000, x--); }
}

function hide_right_part(){
    var b = document.querySelector('button[aria-label="Theater Mode"]');
    if( b && document.readyState == "complete" ){ b.click(); } // Clicking must be delayed because it does not always produce effects otherwise. Is the onclick function added AFTER b is no longer null?
    else{ setTimeout(function(){ hide_right_part(); },1000); }
}

function remove_banner(node_list){
    var doc = document.getElementsByClassName("game-embed-iframe")?.[0]?.contentDocument || document;
    var nodes = node_list || doc.body.getElementsByTagName("div");
    var l = nodes.length;
    var something_was_removed = false;
    for(let k = 0; k<l; k++){
        var node = nodes[k];
        var c = node?.classList?.[0] || "", i = node?.innerHTML || "";
        var class_condition = c.search(/[a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9]/)>-1,
            content_conditions = ( i.search("You are seeing this message because ad or script blocking software is interfering with this page")>-1 ||
                                  i.search("Disable any ad or script blocking software")>-1 ||
                                  i.search("https://www.gstatic.com/images/icons/material/system/1x/warning_amber_24dp.png")>-1 ||
                                  ( node?.src || "" ).search("https://www.gstatic.com/images/icons/material/system/1x/warning_amber_24dp.png")>-1 ||
                                  i == "" );
        if( class_condition && content_conditions ){
            node.remove();
            something_was_removed = true;
        }
    }
    // If a node was removed, then recursively clean the page and ensure no node was forgotten (since many empty nodes exist and we have nodes inside nodes inside nodes...)
    if(something_was_removed){
        remove_banner();
    }
}

function add_style(){
    var css = `
#kongvenience-settings {
	display: flex;
	flex-direction: column;
	position: fixed;
	top: 50%;
	left: 50%;
	transform: translateX(-50%) translateY(-50%);
	border: 5px double black;
	background-color: white;
	color: black;
    z-index:1000;
}

#kongvenience-close-button {
	color: black;
	background-color: white;
	padding: 2px 5px;
	border: 1px solid;
	position: absolute;
	right: 5px;
	top: 5px;
}

#kongvenience-settings-header{
    align-self: center;
    font-size: 25px;
}

#kongvenience-settings-tab {
    padding: 10px 30px;
}

.kongvenience-setting {
	margin: 5px 0px;
}

.kongvenience-setting span{
    margin-left: 5px;
}
        `;
    if(document.head){
        var s = document.createElement("style");
        s.innerHTML = css;
        document.head.appendChild(s);
    }
    else{ setTimeout( function(){ add_style(); }, 1000); }
}

function build_settings_window(){
    var w = document.createElement("div");
    w.id = "kongvenience-settings";
    document.body.appendChild(w);

    var h = document.createElement("div");
    h.id = "kongvenience-settings-header";
    h.innerHTML = "Kongvenience settings";
    w.appendChild(h);

    var T = document.createElement("div");
    T.id = "kongvenience-settings-tab";
    w.appendChild(T);

    build_settings_element(T, "Add a button to reload games", "add_reload_button", true);
    build_settings_element(T, "Automatically launch games", "autoplay", true);
    build_settings_element(T, "Hide right part of game pages automatically", "hide_right_part", true);
    build_settings_element(T, "Automatically (try to) remove the banner that asks to disable ad blockers", "auto_remove_banner", true);

    var close = document.createElement("button");
    close.id = "kongvenience-close-button";
    close.innerHTML = "Close";
    close.addEventListener("click", close_settings);
    w.appendChild(close);
}

function close_settings(){
    var s = document.getElementById("kongvenience-settings");
    if(s){ s.remove(); }
}

function build_settings_element(container, text, gm_setting_name, default_value){
    var d = document.createElement("div");
    d.classList.add("kongvenience-setting");
    container.appendChild(d);

    var checkbox = document.createElement("input");
    checkbox.type = "checkbox";
    checkbox.defaultChecked = GM_getValue(gm_setting_name, default_value);
    checkbox.addEventListener("click", function(){ GM_setValue(gm_setting_name, checkbox.checked); } );
    d.appendChild(checkbox);

    var span = document.createElement("span");
    span.innerHTML = text;
    d.appendChild(span);
}

if(document.URL.search("www.kongregate.com(.*?)/games/(.*?)/embed")>-1){

    // Step 1: setting up a mutation observer to remove the banner as soon as it is loaded
    if( GM_getValue("auto_remove_banner", true) ){
        var targetNode = document.getElementsByClassName("game-embed-iframe")?.[0]?.contentDocument || document;
        var config = { childList: true, subtree: true };
        var callback = (mutationList, observer) => { for (let mutation of mutationList) { remove_banner(mutation.addedNodes); } };
        var observer = new MutationObserver(callback);
        observer.observe(targetNode, config);
    }

    // Step 2: removing the banner after the page has loaded, in case the script was not executed early enough for step one to suffice
    if( document.readyState=="complete" && GM_getValue("auto_remove_banner", true) ){ remove_banner(); }

    // Step 3: removing the banner with either a right-click (if this feature is available) or from the user script manager's menu, in case of very bad luck and both previous steps failed!
    GM_registerMenuCommand("Remove anti-ad blockers banner", function(){remove_banner();} );

}

else if(top===self){
    add_style();
    GM_registerMenuCommand("Open settings", function(){ build_settings_window(); } );
    if( GM_getValue("add_reload_button", true) ){ add_reload_button(); }
    if( GM_getValue("autoplay", true) ){ autoplay(100); }
    if( GM_getValue("hide_right_part", true) ){ hide_right_part(); }
}