CodeHS Brython Graphics Patcher

Patches CodeHS Brython runtimes.

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

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

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==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);
    };
})();