pub / stagit-scripts

Building src.jayvii.de with stagit
git clone https://src.jayvii.de/pub/stagit-scripts.git
Home | Log | Files | Exports | Refs | README | RSS

code_to_clipboard.js (875B)


      1 /* SPDX-License-Identifier: AGPL-3.0-or-later
      2  * SPDX-FileCopyrightText: 2024 JayVii <jayvii[AT]posteo[DOT]de>
      3  */
      4 
      5 /* Decode HTML by creating a text area in DOM and returning its text value */
      6 function decode(encoded_html) {
      7     var obj = document.createElement("textarea");
      8     obj.innerHTML = encoded_html;
      9     return obj.value;
     10 }
     11 
     12 /* Extract Codeblock text from HTML */
     13 function codeblock(selector = 'pre#blob') {
     14   /* Fetch content of code block */
     15   var code_raw = document.querySelector(selector).innerHTML;
     16   /* Strip line number links */
     17   var code_encoded = code_raw.replace(/<a.*<\/a>\ /g, '');
     18   /* Decode codeblock text */
     19   var code = decode(code_encoded);
     20   return code;
     21 }
     22 
     23 /* Copy to clipboard and alert user */
     24 function copy_to_clipboard(text) {
     25   navigator.clipboard.writeText(text);
     26   alert("Copied to your clipboard:\n====================\n\n" + text);
     27 }