Today a friend asked me if I could help them do something on a web page. They didn't understand JavaScript's quirks well enough to easily get it working.

They did understand Brython enough to do pretty much anything on any page, but didn't know how to inject it. I built a userscript, then they improved the style a bit.

Here's the result:

brython_loader.js

// ==UserScript==
// @name          Brython Loader by Wingy
// @version       2020-08-25-0158AM-UTC-5-Wingy
// @run-at        document-end
// @grant         GM.xmlHttpRequest
// ==/UserScript==

const HOME_SWEET_HOME = 'http://127.0.0.1:18555'

function loadScript(url) {
    const script = document.createElement('script')
    script.src = url
    document.head.appendChild(script)
}

GM.xmlHttpRequest({
  method: 'GET',
  url: `${HOME_SWEET_HOME}/entrypoint`,
  onload: function(response) {
    // Placeholder for the entrypoint script
    const script = document.createElement('script')
    script.textContent = response.responseText
    script.type = 'text/python'
    document.body.appendChild(script)

    const brythonLoader = document.createElement('script')
    brythonLoader.textContent = 'brython()'
    setTimeout(() => document.body.appendChild(brythonLoader), 0) // runs on next js tick
  }
})

loadScript(`${HOME_SWEET_HOME}/brython/brython.js`)
loadScript(`${HOME_SWEET_HOME}/brython/brython_stdlib.js`)