czylabson.asa / 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(text);
        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 c of document.getElementsByTagName("code")) {
    	var button = document.createElement("button");
    	c.appendChild(button);
    	button.textContent = "copy";
    	button.onclick = function() {
    	//console.log("copied", i.innerText);
      	copyTextToClipboard(c.childNodes[0].nodeValue.trim());
      };
    }
})();