all repos — NoPaste @ ec60a0bd002f03850a5d89a07971eb8c4469f9ce

Resurrected - The PussTheCat.org fork of NoPaste

index.js (view raw)

  1let copyElement = document.getElementById('copy');
  2const flask = new CodeFlask('#editor', { language: 'javascript', lineNumbers: true, defaultTheme: false });
  3const lzma = new LZMA('lzma.min.js');
  4let select;
  5
  6function init() {
  7    initLangSelector();
  8    initCode();
  9    const clipboard = new ClipboardJS('.clipboard');
 10    clipboard.on('success', function() {
 11        copyElement.style.display = 'none';
 12    });
 13}
 14
 15function initLangSelector() {
 16    select = new SlimSelect({
 17        select: '#language',
 18        data: Object.entries(languages).map(([value, text]) => ({ text, value })),
 19        showContent: 'up',
 20        onChange: () => {
 21            updateLanguage();
 22        }
 23    });
 24
 25    const urlParams = new URLSearchParams(window.location.search);
 26    select.set(Object.keys(languages).indexOf(urlParams.get('lang')) === -1 ? 'javascript' : urlParams.get('lang'));
 27    updateLanguage();
 28}
 29
 30function initCode() {
 31    const base64 = location.hash.substr(1);
 32    if (base64.length === 0) {
 33        return;
 34    }
 35
 36    if (!fetch) {
 37        alert('Your browser does not support this page.  Sorry! :(');
 38        return;
 39    }
 40
 41    fetch('data:application/octet-stream;base64,' + base64)
 42        .then(r => r.blob())
 43        .then(function(blob) {
 44            const reader = new FileReader();
 45            reader.onload = function() {
 46                lzma.decompress(Array.from(new Uint8Array(reader.result)), function(plaintext, error) {
 47                    if (error) {
 48                        alert('Failed to decompress data: ' + error);
 49                        return;
 50                    }
 51                    flask.updateCode(plaintext);
 52                });
 53            };
 54            reader.readAsArrayBuffer(blob);
 55        });
 56}
 57
 58function updateLanguage() {
 59    const lang = select.selected();
 60    if (!Prism.languages.hasOwnProperty(lang)) {
 61        addLanguage(lang);
 62        return;
 63    }
 64    flask.updateLanguage(lang);
 65}
 66
 67function addLanguage(lang) {
 68    // Add a setter to detect when a language is available
 69    Object.defineProperty(Prism.languages, lang, {
 70        set: function(val) {
 71            Prism.languages['_custom_' + lang] = val;
 72            flask.updateLanguage(lang);
 73        },
 74        get: function() {
 75            return Prism.languages['_custom_' + lang];
 76        }
 77    });
 78
 79    const script = document.createElement('script');
 80    script.setAttribute('src', `https://cdn.jsdelivr.net/npm/prismjs@1.14.0/components/prism-${lang}.min.js`);
 81    document.getElementsByTagName('head')[0].appendChild(script);
 82}
 83
 84function generateLink() {
 85    const code = flask.getCode();
 86    lzma.compress(code, 1, function(compressed, error) {
 87        if (error) {
 88            alert('Failed to compress data: ' + error);
 89            return;
 90        }
 91        const reader = new FileReader();
 92        reader.onload = function() {
 93            const base64 = reader.result.substr(reader.result.indexOf(',') + 1);
 94            const url = `${location.protocol}//${location.host}${
 95                location.pathname
 96            }?lang=${select.selected()}#${base64}`;
 97            document.getElementById('copy-link').value = url;
 98            copyElement.style.display = 'block';
 99        };
100        reader.readAsDataURL(new Blob([new Uint8Array(compressed)]));
101    });
102}
103
104init();