Paste Images - MAL

Copy any image and paste it on MAL to get it uploaded to Imgur and have the direct image link and BBCode automatically added on the reply box!

נכון ליום 12-09-2023. ראה הגרסה האחרונה.

// ==UserScript==
// @name         Paste Images - MAL
// @namespace    TheUploader
// @version      3
// @description  Copy any image and paste it on MAL to get it uploaded to Imgur and have the direct image link and BBCode automatically added on the reply box!
// @author       hacker09
// @match        https://myanimelist.net/forum/?topicid=*
// @match        https://myanimelist.net/mymessages.php?go=send&*
// @icon         https://t3.gstatic.com/faviconV2?client=SOCIAL&type=FAVICON&fallback_opts=TYPE,SIZE,URL&url=http://myanimelist.net&size=64
// @grant        none
// ==/UserScript==

(function() {
  'use strict';

  function Paste() { //Starts the Paste function
    document.querySelectorAll(".sourceMode > textarea").forEach(function(el) { //ForEach reply box
      if (!el.hasAttribute('data-paste-listener')) { //Check if the attribute is already added
        el.setAttribute('data-paste-listener', 'true'); //Add an attribute to mark that the listener as added
        el.addEventListener('paste', function(event) { //Add a paste event listener to the reply box
          const clipboardData = event.clipboardData || event.originalEvent.clipboardData; //Retrieves clipboard data for the paste event

          if (Array.from(clipboardData.items).find(item => item.type.indexOf('image') !== -1)) { //If its an image
            const formData = new FormData(); //Creates a new instance of FormData object
            const xhr = new XMLHttpRequest(); //Creates a new xhr request

            formData.append('image', Array.from((event.clipboardData || event.originalEvent.clipboardData).items).find(item => item.type.indexOf('image') !== -1)?.getAsFile()); //Appends an image file from clipboard data to the FormData object
            xhr.open('POST', 'https://api.imgur.com/3/image'); //Call the Imgur API
            xhr.setRequestHeader('Authorization', 'Client-ID aca6d2502f5bfd8'); //Add the API ID to the xhr request
            xhr.onload = function() { //When the xhr request is completed
              if (JSON.parse(xhr.responseText).data.error !== undefined) //If the API is being time rate limited
              { //Starts the if condition
                el.value += ` ${JSON.parse(xhr.responseText).data.error.message}`; //Add the error message to the reply box
              } //Finishes the if condition
              else //If the API is not being time rate limited
              { //Starts the else condition
                el.value += ` [img]${JSON.parse(xhr.responseText).data.link}[/img]`; //Add the Imgur direct image link and BBCode to the reply box
              } //Finishes the else condition

              document.querySelectorAll(".js-timeline-reply-submit,.btn-recaptcha-submit").forEach(function(el) { //ForEach New Reply button / "Send Message"
                el.title = `The script can upload ${xhr.getResponseHeader('X-RateLimit-ClientRemaining')} images today.\nYou have ${xhr.getResponseHeader('X-Ratelimit-Userremaining')} image uploads left per hour.\nYour user upload limits will reset again in ${Math.floor((xhr.getResponseHeader('X-RateLimit-UserReset') - new Date().getTime() / 1000) / 60)} minutes.`; //Show helpful info on hover
              }); //Finishes the ForEach New Reply button function
            }; //Finishes the xhr onload function
            xhr.send(formData);
          } //Finishes the if condition
        }); //Finishes the paste event listener function
      } //Finishes the if condition
    }); //Finishes the ForEach Reply button function
  } //Finishes the Paste function
  Paste(); //Calls the Paste function

  document.querySelectorAll(".js-timeline-reply, .js-thread-reply").forEach(function(el) { //ForEach reply child
    el.onclick = function() { //When the reply child btn is clicked
      setTimeout(() => { Paste(); }, 0); //Run the script again
    }; //Finishes the onclick function
  }) //Finishes the forEach loop
})();