Greasy Fork is available in English.

Simpleicons.org Dynamic Color Button

Adds a button with which you can change the color of the SVG before downloading.

目前为 2023-08-11 提交的版本。查看 最新版本

// ==UserScript==
// @name         Simpleicons.org Dynamic Color Button 
// @namespace    https://greasyfork.org/
// @version      0.0.1
// @description  Adds a button with which you can change the color of the SVG before downloading.
// @author       Ap
// @match        https://simpleicons.org/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    // Wait until the DOM is fully loaded
    window.addEventListener('DOMContentLoaded', function() {
        // Find the download button element by its ID
        let downloadBtn = document.getElementById('icon-download-color-svg');

        if (downloadBtn) {
            // Create a new <a> element
            let promptBtn = document.createElement('a');

            // Copy the attributes from the original button
            promptBtn.download = downloadBtn.download;
            promptBtn.href = downloadBtn.href;

            // Add new attributes to the new button
            promptBtn.setAttribute('class', 'detail-button');
            promptBtn.setAttribute('id', 'icon-download-color-svg-prompt');
            promptBtn.setAttribute('role', 'button');

            // Insert the new button next to the original one
            downloadBtn.parentNode.insertBefore(promptBtn, downloadBtn.nextSibling);

            // Add click event listener to the new button
            promptBtn.addEventListener('click', function(event) {
                event.preventDefault(); // Prevent the default action

                // Get the hex code from the user
                let hexCode = prompt("Please enter hex code for color.");

                // Check if the hex code is valid
                if (hexCode && /^([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/i.test(hexCode)) {
                    // Replace the fill value in the href attribute with the new hex code
                    promptBtn.href = downloadBtn.href.replace(/fill=%23[0-9a-fA-F]{6}/, `fill=%23${hexCode}`);

                    // Trigger the download
                    window.location.href = promptBtn.href;
                } else {
                    alert('Invalid hex code entered.');
                }
            });
        }
    });
})();