all repos — NoPaste @ c815b7c125ddae0d045e8f4c7a650010cd7e5c43

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 => ({
 25            text: e.name,
 26            value: slugify(e.name),
 27            data: { mime: e.mime, mode: e.mode }
 28        })),
 29        showContent: 'up',
 30        onChange: e => {
 31            const language = e.data || { mime: null, mode: null };
 32            editor.setOption('mode', language.mime);
 33            CodeMirror.autoLoadMode(editor, language.mode);
 34        }
 35    });
 36
 37    const urlParams = new URLSearchParams(window.location.search);
 38    select.set(decodeURIComponent(urlParams.get('lang') || 'plain-text'));
 39};
 40
 41const initCode = () => {
 42    const base64 = location.hash.substr(1);
 43    if (base64.length === 0) {
 44        return;
 45    }
 46    decompress(base64, (code, err) => {
 47        if (err) {
 48            alert('Failed to decompress data: ' + err);
 49            return;
 50        }
 51        editor.setValue(code);
 52    });
 53};
 54
 55const initClipboard = () => {
 56    clipboard = new ClipboardJS('.clipboard');
 57    clipboard.on('success', () => {
 58        hideCopyBar(true);
 59    });
 60};
 61
 62const generateLink = () => {
 63    compress(editor.getValue(), (base64, err) => {
 64        if (err) {
 65            alert('Failed to compress data: ' + err);
 66            return;
 67        }
 68        const url = buildUrl(base64);
 69        showCopyBar(url);
 70    });
 71};
 72
 73// Open the "Copy" bar and select the content
 74const showCopyBar = dataToCopy => {
 75    const linkInput = document.getElementById('copy-link');
 76    linkInput.value = dataToCopy;
 77    linkInput.setSelectionRange(0, dataToCopy.length);
 78    document.getElementById('copy').style.display = 'flex';
 79};
 80
 81// Close the "Copy" bar
 82const hideCopyBar = success => {
 83    const copyButton = document.getElementById('copy-btn');
 84    const copyBar = document.getElementById('copy');
 85    if (!success) {
 86        copyBar.style.display = 'none';
 87        return;
 88    }
 89    copyButton.innerText = 'Copied !';
 90    setTimeout(() => {
 91        copyBar.style.display = 'none';
 92        copyButton.innerText = 'Copy';
 93    }, 800);
 94};
 95
 96// Build a shareable URL
 97const buildUrl = rawData => {
 98    return `${location.protocol}//${location.host}${location.pathname}?lang=${encodeURIComponent(
 99        select.selected()
100    )}#${rawData}`;
101};
102
103// Transform a compressed base64 string into a plain text string
104const decompress = (base64, cb) => {
105    const progressBar = document.getElementById('progress');
106
107    const req = new XMLHttpRequest();
108    req.open('GET', 'data:application/octet;base64,' + base64);
109    req.responseType = 'arraybuffer';
110    req.onload = e => {
111        lzma.decompress(
112            new Uint8Array(e.target.response),
113            (result, err) => {
114                progressBar.style.width = '0';
115                cb(result, err);
116            },
117            progress => {
118                progressBar.style.width = 100 * progress + '%';
119            }
120        );
121    };
122    req.send();
123};
124
125// Transform a plain text string into a compressed base64 string
126const compress = (str, cb) => {
127    const progressBar = document.getElementById('progress');
128
129    lzma.compress(
130        str,
131        1,
132        (compressed, err) => {
133            if (err) {
134                progressBar.style.width = '0';
135                cb(compressed, err);
136                return;
137            }
138            const reader = new FileReader();
139            reader.onload = () => {
140                progressBar.style.width = '0';
141                cb(reader.result.substr(reader.result.indexOf(',') + 1));
142            };
143            reader.readAsDataURL(new Blob([new Uint8Array(compressed)]));
144        },
145        progress => {
146            progressBar.style.width = 100 * progress + '%';
147        }
148    );
149};
150
151const slugify = str =>
152    str
153        .toString()
154        .toLowerCase()
155        .replace(/\s+/g, '-')
156        .replace(/\+/g, '-p')
157        .replace(/#/g, '-sharp')
158        .replace(/[^\w\-]+/g, '');
159
160init();