nguyenthanhnam0212 / Flaticon SVG Downloader

// ==UserScript==
// @name         Flaticon SVG Downloader
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Download SVG from flaticon icon editor
// @match        https://www.flaticon.com/*
// @license MIT
// @grant        none
// ==/UserScript==

(function () {
  'use strict';

  // format time yyyyMMdd_HHmmss
  function getTimestamp() {
    const now = new Date();
    const pad = (n) => n.toString().padStart(2, "0");

    return now.getFullYear() +
      pad(now.getMonth() + 1) +
      pad(now.getDate()) + "_" +
      pad(now.getHours()) +
      pad(now.getMinutes()) +
      pad(now.getSeconds());
  }

  function createDownloadButton(svgElement) {
    if (document.getElementById("tm_download_svg_btn")) return;

    const btn = document.createElement("button");
    btn.id = "tm_download_svg_btn";
    btn.innerText = "⬇ Download SVG";
    btn.style.position = "fixed";
    btn.style.top = "120px";
    btn.style.right = "20px";
    btn.style.zIndex = "9999";
    btn.style.padding = "12px 18px";
    btn.style.fontSize = "16px";
    btn.style.background = "#ff4b4b";
    btn.style.color = "white";
    btn.style.border = "none";
    btn.style.borderRadius = "8px";
    btn.style.cursor = "pointer";
    btn.style.boxShadow = "0 4px 12px rgba(0,0,0,0.2)";

    btn.onclick = () => {
      const svgText = svgElement.outerHTML;

      const blob = new Blob([svgText], {
        type: "image/svg+xml"
      });
      const url = URL.createObjectURL(blob);

      const a = document.createElement("a");
      a.href = url;
      a.download = `flaticon_${getTimestamp()}.svg`;
      document.body.appendChild(a);
      a.click();
      document.body.removeChild(a);
      URL.revokeObjectURL(url);
    };

    document.body.appendChild(btn);
  }

  // wait until svg appears (flaticon load bằng JS nên phải observe)
  function observeSVG() {
    const observer = new MutationObserver(() => {
      const holder = document.querySelector(
        "div.detail__editor__icon-holder.icon-holder"
      );

      if (!holder) return;

      const svg = holder.querySelector("svg");
      if (!svg) return;

      console.log("SVG detected!");
      createDownloadButton(svg);
    });

    observer.observe(document.body, {
      childList: true,
      subtree: true
    });
  }

  observeSVG();
})();