Advertisement
Guest User

Untitled

a guest
Sep 29th, 2019
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name Patrick.net Image Clipboard Paste
  3. // @namespace    http://tampermonkey.net/
  4. // @version      0.1
  5. // @description  Image Paste
  6. // @match http://patrick.net/post/*
  7. // @match https://patrick.net/post/*
  8. // @match http://patrick.net/edit_comment*
  9. // @match https://patrick.net/edit_comment*
  10. // @match http://patrick.net/new_post*
  11. // @match https://patrick.net/new_post*
  12. // @run-at document-end
  13. // ==/UserScript==
  14.  
  15. /* jshint esnext:true */
  16.  
  17. (function() {
  18.     'use strict';
  19.  
  20.     // <include file="common.js">
  21.  
  22.     // This file (common.js) contains some common code that will be replaced into other js files
  23.     // via running the build.sh script
  24.  
  25.     /**
  26.      * Recursively creates dom elements from your html string.
  27.      *
  28.      * @param {string} htmlStr
  29.      * @return {DocumentFragment}
  30.      */
  31.     function htmlToFragment(htmlStr) {
  32.         const template = document.createElement('template');
  33.         template.innerHTML = htmlStr;
  34.         return template.content;
  35.     }
  36.  
  37.     /**
  38.      * Recursively creates dom elements from your html string. Only the first top-level element will be returned.
  39.      *
  40.      * @example htmlToElements('  <label>foo <input value=123></label>').querySelector('input').value === '123'
  41.      * @param {string} htmlStr
  42.      * @return {Element}
  43.      */
  44.     function htmlToElements(htmlStr) {
  45.         return htmlToFragment(htmlStr).firstElementChild;
  46.     }
  47.  
  48.     /**
  49.      * A compact shorthand of document.querySelector(...);
  50.      *
  51.      * @param {string} cssSelector
  52.      * @param {Element | Document} [contextElement]
  53.      * @return {HTMLElement | null}
  54.      */
  55.     function qs(cssSelector, contextElement = document) {
  56.         return contextElement.querySelector(cssSelector);
  57.     }
  58.  
  59.     /**
  60.      * A compact shorthand of document.querySelectorAll(...), but returns a real array instead of a NodeList
  61.      *
  62.      * @param {string} cssSelector
  63.      * @param {Element | Document} [contextElement]
  64.      * @return {Array<HTMLElement>}
  65.      */
  66.     function qsa(cssSelector, contextElement = document) {
  67.         return Array.from(contextElement.querySelectorAll(cssSelector));
  68.     }
  69.  
  70.     function insertAfter(item, reference) {
  71.         if (reference.nextSibling) {
  72.             reference.parentNode.insertBefore(item, reference.nextSibling);
  73.         } else {
  74.             reference.parentNode.appendChild(item);
  75.         }
  76.     }
  77.  
  78.     function addCss(cssStr) {
  79.         const style = document.createElement('style');
  80.         style.type = 'text/css';
  81.         style.rel = 'stylesheet';
  82.         style.innerHTML = cssStr;
  83.         document.getElementsByTagName('head')[0].appendChild(style);
  84.     }
  85.  
  86.     // </include>
  87.  
  88.     const imageForm = qs('#upload-file,#upload_form');
  89.     const fileInput = imageForm.querySelector('[type=file]');
  90.  
  91.     const dropArea = imageForm;
  92.     // const dropArea = htmlToElements('<div style="width: 300px; height: 300px; border: 1px solid #ccc;"></div>');
  93.     const label = htmlToElements('<div>Drag and drop an image file, or paste an image from your clipboard into this box.</div>');
  94.  
  95.     insertAfter(dropArea, imageForm);
  96.     imageForm.prepend(label);
  97.  
  98.  
  99.     dropArea.addEventListener('drop', evt => {
  100.         evt.preventDefault();
  101.         fileInput.files = evt.dataTransfer.files;
  102.     });
  103.  
  104.     dropArea.addEventListener('dragover', evt => {
  105.         evt.preventDefault();
  106.     });
  107.  
  108.     dropArea.addEventListener('paste', evt => {
  109.         const dataTransfer = new ClipboardEvent('').clipboardData || // Firefox < 62 workaround exploiting https://bugzilla.mozilla.org/show_bug.cgi?id=1422655
  110.             new DataTransfer(); // specs compliant (as of March 2018 only Chrome)
  111.  
  112.         // use event.originalEvent.clipboard for newer chrome versions
  113.         const items = (event.clipboardData || event.originalEvent.clipboardData).items;
  114.         // find pasted image among pasted items
  115.         for (let i = 0; i < items.length; i++) {
  116.             if (items[i].type.indexOf("image") === 0) {
  117.                 const pastedImageFile = items[i].getAsFile();
  118.  
  119.                 // Copy it, so we can change the name to be unique.
  120.                 const blob = pastedImageFile.slice(0, pastedImageFile.size, 'image/png');
  121.                 const newFile = new File([blob], `pasted-image-${new Date().getTime()}.png`, {type: 'image/png'});
  122.  
  123.                 dataTransfer.items.add(newFile);
  124.             }
  125.         }
  126.  
  127.         fileInput.files = dataTransfer.files;
  128.     });
  129.  
  130.     addCss(`
  131.     #upload-file, #upload_form {
  132.         border: 1px solid #ccc;
  133.         padding: 1em;
  134.         background-color: #d9edf7;
  135.         margin-bottom: 1em;
  136.     }
  137.     `);
  138. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement