CodeHS Brython Graphics Patcher

Patches CodeHS Brython runtimes.

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

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

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

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

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

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.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

// ==UserScript==
// @name         CodeHS Brython Graphics Patcher
// @namespace    https://thetridentguy.com/
// @version      0.1.0
// @description  Patches CodeHS Brython runtimes.
// @author       TheTridentGuy
// @match        https://*.codehs.me/__codehs__/index.html*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=codehs.com
// @license      MIT
// @run-at       document-start
// ==/UserScript==

(function() {
    'use strict';
    const patch = `
import traceback
def _patch_handler(handler):
    def patched_handler(callback):
        def patched_callback(*args, **kwargs):
            try: 
                callback(*args, **kwargs)
            except:
                print()
                print(traceback.format_exc())
        handler(patched_callback)
    return patched_handler

add_mouse_click_handler = _patch_handler(add_mouse_click_handler)
add_key_down_handler = _patch_handler(add_key_down_handler)
add_key_up_handler = _patch_handler(add_key_up_handler)\n`
    let old_append_child = HTMLBodyElement.prototype.appendChild;
    HTMLBodyElement.prototype.appendChild = function(element){
        if(element.tagName == "SCRIPT"){
            let original_code = element.innerHTML.split("\n")
            element.innerHTML = original_code.slice(0,2).join("\n") + patch + original_code.slice(2).join("\n");
        }
        return old_append_child.call(this, element);
    };
})();