czylabson.asa / Copy on julia discourse 2

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

(function() {
    'use strict';
    var sheet = document.createElement("style");
    sheet.innerHTML = "code {cursor: copy; }";
    sheet.type="text/css"
    document.body.appendChild(sheet);

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