all repos — NoPaste @ 9f23498cd0109cf87bf1f681ca231975491959e6

Resurrected - The PussTheCat.org fork of NoPaste

index.js (view raw)

  1const lzma = new LZMA('lzma.min.js');
  2let editor = null;
  3let select = null;
  4let clipboard = null;
  5
  6const init = () => {
  7    initCodeEditor();
  8    initLangSelector();
  9    initCode();
 10    initClipboard();
 11};
 12
 13const initCodeEditor = () => {
 14    CodeMirror.modeURL = 'https://cdn.jsdelivr.net/npm/codemirror@5.51.0/mode/%N/%N.js';
 15    editor = new CodeMirror(document.getElementById('editor'), {
 16        lineNumbers: true,
 17        theme: 'dracula'
 18    });
 19};
 20
 21const initLangSelector = () => {
 22    select = new SlimSelect({
 23        select: '#language',
 24        data: CodeMirror.modeInfo.map(e => ({ text: e.name })),
 25        showContent: 'up',
 26        onChange: e => {
 27            let mode = CodeMirror.findModeByName(e.text);
 28            mode = mode ? mode.mode : null;
 29            editor.setOption('mode', mode);
 30            CodeMirror.autoLoadMode(editor, mode);
 31        }
 32    });
 33
 34    const urlParams = new URLSearchParams(window.location.search);
 35    select.set(decodeURIComponent(urlParams.get('lang') || 'Plain Text'));
 36};
 37
 38const initCode = () => {
 39    const base64 = location.hash.substr(1);
 40    if (base64.length === 0) {
 41        return;
 42    }
 43    decompress(base64, (code, err) => {
 44        if (err) {
 45            alert('Failed to decompress data: ' + err);
 46            return;
 47        }
 48        editor.setValue(code);
 49    });
 50};
 51
 52const initClipboard = () => {
 53    clipboard = new ClipboardJS('.clipboard');
 54    clipboard.on('success', () => {
 55        hideCopyBar(true);
 56    });
 57};
 58
 59const generateLink = () => {
 60    compress(editor.getValue(), (base64, err) => {
 61        if (err) {
 62            alert('Failed to compress data: ' + err);
 63            return;
 64        }
 65        const url = buildUrl(base64);
 66        showCopyBar(url);
 67    });
 68};
 69
 70// Open the "Copy" bar and select the content
 71const showCopyBar = dataToCopy => {
 72    const linkInput = document.getElementById('copy-link');
 73    linkInput.value = dataToCopy;
 74    linkInput.setSelectionRange(0, dataToCopy.length);
 75    document.getElementById('copy').style.display = 'flex';
 76};
 77
 78// Close the "Copy" bar
 79const hideCopyBar = success => {
 80    const copyButton = document.getElementById('copy-btn');
 81    const copyBar = document.getElementById('copy');
 82    if (!success) {
 83        copyBar.style.display = 'none';
 84        return;
 85    }
 86    copyButton.innerText = 'Copied !';
 87    setTimeout(() => {
 88        copyBar.style.display = 'none';
 89        copyButton.innerText = 'Copy';
 90    }, 800);
 91};
 92
 93// Build a shareable URL
 94const buildUrl = rawData => {
 95    return `${location.protocol}//${location.host}${location.pathname}?lang=${encodeURIComponent(
 96        select.selected()
 97    )}#${rawData}`;
 98};
 99
100// Transform a compressed base64 string into a plain text string
101const decompress = (base64, cb) => {
102    const req = new XMLHttpRequest();
103    req.open('GET', 'data:application/octet;base64,' + base64);
104    req.responseType = 'arraybuffer';
105    req.onload = e => {
106        lzma.decompress(new Uint8Array(e.target.response), cb);
107    };
108    req.send();
109};
110
111// Transform a plain text string into a compressed base64 string
112const compress = (str, cb) => {
113    lzma.compress(str, 1, (compressed, err) => {
114        if (err) {
115            cb(compressed, err);
116            return;
117        }
118        const reader = new FileReader();
119        reader.onload = () => {
120            cb(reader.result.substr(reader.result.indexOf(',') + 1));
121        };
122        reader.readAsDataURL(new Blob([new Uint8Array(compressed)]));
123    });
124};
125
126init();