mraron / Copy on julia discourse

// ==UserScript==
// @name         Copy on julia discourse
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Click on a code block to copy its contents
// @author       mraron
// @copyright    2021, mraron (https://openuserjs.org/users/mraron)
// @license      MIT
// @match        https://discourse.julialang.org/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
    function copyTextToClipboard(text) {
        if (!navigator.clipboard) {
            console.log("Not supported");
            return;
        }
        navigator.clipboard.writeText(text).then(function() {
            console.log('Async: Copying to clipboard was successful!');
        }, function(err) {
            console.error('Async: Could not copy text: ', err);
        });
    }

    for(let i of document.getElementsByTagName("code")) {
        i.onclick = function() {
            //console.log("copied", i.innerText);
            copyTextToClipboard(i.innerText);
        };
    }
})();