Bopimo Item Data Downloader

Adds a button to download item textures from Bopimo.com

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Bopimo Item Data Downloader
// @namespace    http://tampermonkey.net/
// @version      0.3
// @description  Adds a button to download item textures from Bopimo.com
// @author       Teemsploit, Variant Tombstones, Evelyn
// @license      MIT
// @match        https://www.bopimo.com/items/*
// @run-at       document-start
// @grant        none
// ==/UserScript==

(function () {
  'use strict';

  function injectUI() {
    var buttonPanel = document.createElement('div');
    buttonPanel.id = "custom-download-panel";
    buttonPanel.className = "shop-card";
    buttonPanel.style = "position: fixed; z-index: 1000; padding: 1rem;";
    buttonPanel.style.bottom = "1rem";
    buttonPanel.style.right = "1rem";
    buttonPanel.innerHTML = `
      <button class="button" id="download-texture-btn">Download Texture</button>
      <button class="button" id="download-mesh-btn" style="margin-left: 10px;">Download Mesh</button>
      <p>Credits: Teemsploit & Variant Tombstones</p>
    `;
    document.body.appendChild(buttonPanel);
  }

  function makeButtonsDoStuffIGuess() {
    const textureButton = document.getElementById("download-texture-btn");
    const meshButton = document.getElementById("download-mesh-btn");

    if (textureButton) {
      textureButton.addEventListener("click", () => download('image'));
    }

    if (meshButton) {
      meshButton.addEventListener("click", () => download('mesh'));
    }
  }

  function download(type) {
    try {
      var imageUrl = document.querySelector('meta[property="og:image"]').getAttribute('content');
      if (!imageUrl) {
        alert('Image link not found.');
        return;
      }
      var assetUrl = imageUrl.replace("renders/thumbnail", "assets");

      // Fix: Proper comparison for the type
      if (type === 'mesh') {
        assetUrl = assetUrl.replace(".png", ".obj");
      }

      var parts = assetUrl.split("/");
      var fileName = parts[parts.length - 1];
      var link = document.createElement("a");
      link.setAttribute("href", assetUrl);
      link.setAttribute("download", fileName);
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link);
    } catch (err) {
      alert('An error occurred: ' + err);
    }
  }

  function waitForDomReady() {
    if (document.readyState === 'loading') {
      document.addEventListener('DOMContentLoaded', makeButtonsDoStuffIGuess);
    } else {
      makeButtonsDoStuffIGuess();
    }
  }

  injectUI();

  waitForDomReady();
})();