hnOsmium0001

irccloud upload to imgur userscript

Mar 14th, 2021
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         Upload to custom services
  3. // @namespace    http://github.com/hnOsmium0001/
  4. // @version      1.0
  5. // @description  Add option to upload to custom image hosting services
  6. // @author       You
  7. // @match        https://*.irccloud.com/*
  8. // @grant        none
  9. // ==/UserScript==
  10.  
  11. (function() {
  12.     'use strict';
  13.  
  14.     function hasClassPrefix(element, prefix) {
  15.         if (!element.getAttribute) return false;
  16.  
  17.         const classes = (element.getAttribute("class") || "").split();
  18.         return classes.some(x => x.startsWith(prefix));
  19.     }
  20.  
  21.     class ChildrenSelector {
  22.         constructor(elm) {
  23.             this.elm = elm;
  24.         }
  25.  
  26.         andThenTag(tag, alternativeElm) {
  27.             if (!this.elm) {
  28.                 this.elm = alternativeElm;
  29.                 return this;
  30.             }
  31.  
  32.             for (const child of this.elm.childNodes) {
  33.                 if (child.tagName === tag) {
  34.                     this.elm = child;
  35.                     return this;
  36.                 }
  37.             }
  38.             this.elm = alternativeElm;
  39.             return this;
  40.         }
  41.  
  42.         andThenClass(prefix, alternativeElm) {
  43.             if (!this.elm) {
  44.                 this.elm = alternativeElm;
  45.                 return this;
  46.             }
  47.  
  48.             for (const child of this.elm.childNodes) {
  49.                 if (hasClassPrefix(child, prefix)) {
  50.                     this.elm = child;
  51.                     return this;
  52.                 }
  53.             }
  54.             // Failed to find a matching children
  55.             this.elm = alternativeElm;
  56.             return this;
  57.         }
  58.  
  59.         accept(successful, failed) {
  60.             if (this.elm) {
  61.                 successful(this.elm);
  62.             } else {
  63.                 failed();
  64.             }
  65.         }
  66.     }
  67.  
  68.     const uploadContainer = document.getElementById('fileUploadForm');
  69.     let batchFile;
  70.     new ChildrenSelector(uploadContainer)
  71.         .andThenClass("batch")
  72.         // TODO
  73.         .accept(e => { batchFile = e }, () => {});
  74.     let singleFile;
  75.     new ChildrenSelector(uploadContainer)
  76.         .andThenClass("single")
  77.         .andThenClass("previewWrapper")
  78.         .accept(e => { singleFile = e }, () => {});
  79.  
  80.     let inputBox;
  81.     (function checkSession() {
  82.         function init() {
  83.             // cb() is a global function provided by irccloud
  84.             inputBox = document.getElementById("bufferInputView" + cb().bid())
  85.         }
  86.  
  87.         if (window.hasOwnProperty('SESSION')) {
  88.             window.SESSION.bind('init', function () {
  89.                 init();
  90.                 window.SESSION.buffers.on('doneSelected', function () {
  91.                     init();
  92.                 });
  93.             });
  94.         } else {
  95.             setTimeout(checkSession, 100);
  96.         }
  97.     })();
  98.  
  99.     function uploadImage(imageData) {
  100.         const FILTER = ";base64,";
  101.         const data = new FormData();
  102.         data.append("image", imageData.substring(imageData.indexOf(FILTER) + FILTER.length));
  103.  
  104.         const req = new XMLHttpRequest();
  105.         req.onreadystatechange = () => {
  106.             if (req.readyState == 4) {
  107.                 // Response should contain a URL to the uploaded image
  108.                 const response = JSON.parse(req.response);
  109.                 inputBox.value += response.data.link;
  110.             }
  111.         };
  112.         // For whatever reason, thea ction endpoint for uploading image is /image instead of /upload as described at https://apidocs.imgur.com/#c85c9dfc-7487-4de2-9ecd-66f727cf3139
  113.         // See https://stackoverflow.com/questions/55733271/upload-image-to-imgur-failed-because-of-cors
  114.         req.open("POST", "https://api.imgur.com/3/image");
  115.         req.setRequestHeader("Authorization", "Client-ID YOUR_CLIENT_ID");
  116.         req.send(data);
  117.     }
  118.  
  119.     new ChildrenSelector(uploadContainer)
  120.         .andThenClass("buttons")
  121.         .accept(uploadButtonContainer => {
  122.             const uploadBtn = document.createElement("button");
  123.             const closeBtn = uploadButtonContainer.lastElementChild;
  124.             uploadButtonContainer.prepend(uploadBtn);
  125.             uploadBtn.type = "button";
  126.             uploadBtn.classList.add("action");
  127.             uploadBtn.classList.add("confirm");
  128.             uploadBtn.innerHTML = "<span>Upload to Imgur</span>";
  129.             uploadBtn.addEventListener("click", () => {
  130.                 console.log("Uploading to imgur");
  131.                 if (singleFile.hasChildNodes()) {
  132.                     const img = singleFile.children[0].children[0];
  133.                     uploadImage(img.src);
  134.                     closeBtn.click();
  135.                 }
  136.                 if (batchFile.hasChildNodes()) {
  137.                     closeBtn.click();
  138.                 }
  139.             });
  140.         }, () => console.log("Failed to find button list of fileUploadContainer"));
  141. })();
Add Comment
Please, Sign In to add comment