faubiguy / Xkcd Forums Monospace Button

// ==UserScript==
// @name         Xkcd Forums Monospace Button
// @version      0.1
// @description  Adds a button to make the text box on the xkcd forums monospace.
// @author       faubiguy
// @include      forums.xkcd.com/posting.php*
// @include      fora.xkcd.com/posting.php*
// @include      forums3.xkcd.com/posting.php*
// @include      echochamber.me/posting.php*
// @grant        none
// ==/UserScript==

var isMonospace = false

button = document.createElement('input')
button.type = 'button'
button.classList.add('button2')
button.value = 'monospace'
button.title = 'Toggle monospace font'

postform = document.getElementById('postform')
postAction = postform.action

function toggleMonospace() {
    isMonospace = !isMonospace
    document.getElementById('message').style['font-family'] = isMonospace ? 'monospace' : ''
    if (isMonospace){
        postform.action = postAction + '&monospace=1'
    } else {
        postform.action = postAction
    }
}

button.addEventListener('click', toggleMonospace)

if (document.location.search.indexOf('monospace=1') !== -1) {
    toggleMonospace()
}

document.getElementById('format-buttons').appendChild(button)