bossdeer upload

Enable image pasting and ImgBB upload integration in bossdeer

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         bossdeer upload
// @namespace    http://violentmonkey.github.io
// @version      1.1
// @description  Enable image pasting and ImgBB upload integration in bossdeer
// @author       NotFenixio
// @match        https://deer.meltland.dev/*
// @grant        GM_addStyle
// @license MIT
// ==/UserScript==

(function () {
  "use strict";

  async function uploadToImgBB(apiKey, imageData) {
    const formData = new FormData();
    formData.append("key", apiKey);
    formData.append("image", imageData);

    const response = await fetch("https://api.imgbb.com/1/upload", {
      method: "POST",
      body: formData,
    });

    const result = await response.json();
    if (result.success) {
      return result.data.url;
    } else {
      console.error("ImgBB upload failed:", result);
      throw new Error("Failed to upload image");
    }
  }

  const mainConfig = document.getElementById("main-config");
  if (mainConfig) {
    const inputHTML = `
            <br>
            <input id="imgbb-api-key" placeholder="ImgBB API Key..." type="text" maxlength="656">
            <button id="save-api-key">Save API Key</button>
        `;
    const h2Element = Array.from(document.querySelectorAll('h2')).find(h2 => h2.textContent.trim() === 'Misc');
    if (h2Element) {
        h2Element.insertAdjacentHTML('beforebegin', inputHTML);
    }

    document.getElementById("save-api-key").onclick = () => {
      const apiKey = document.getElementById("imgbb-api-key").value;
      if (apiKey) {
        localStorage.setItem("imgbbApiKey", apiKey);
        alert("ImgBB API key saved");
      }
    };
  }

  const msMsg = document.querySelector("#ms-msg");
  if (msMsg) {
    msMsg.addEventListener("paste", async (event) => {
      const apiKey = localStorage.getItem("imgbbApiKey");
      if (!apiKey) {
        alert("Please set your ImgBB API key in the Settings section");
        return;
      }

      const items = (event.clipboardData || event.originalEvent.clipboardData)
        .items;
      for (const item of items) {
        if (item.kind === "file") {
          const file = item.getAsFile();
          const reader = new FileReader();

          reader.onload = async (e) => {
            try {
              document.querySelector("#error-text").innerText = "Uploading...";
              if (
                document
                  .querySelector("#error-bar")
                  .classList.contains("hidden")
              ) {
                document.querySelector("#error-bar").classList.toggle("hidden");
              }
              const imageUrl = await uploadToImgBB(
                apiKey,
                e.target.result.split(",")[1],
              );
              attachments.push(imageUrl);
              updateDetailsMsg();
              document.querySelector("#error-text").innerText = "Uploaded!";
              setTimeout(() => {
                document.querySelector("#error-bar").classList.toggle("hidden");
              }, 1000);
            } catch (error) {
              console.error("Error uploading image:", error);
              document.querySelector("#error-text").innerText =
                "Error uploading! " + error;
              document.querySelector("#error-bar").classList.toggle("hidden");
            }
          };

          reader.readAsDataURL(file);
        }
      }
    });
  }
})();