Avatar Builder+

Adds a upload image input to the avatar builder.

当前为 2023-02-15 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Avatar Builder+
// @namespace    Vholran.AvatarBuilder+
// @version      2.2
// @description  Adds a upload image input to the avatar builder.
// @author       Vholran (https://greasyfork.org/en/users/841616)
// @match        https://*.drawaria.online/avatar/builder/
// @require      https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js
// @icon         https://www.google.com/s2/favicons?domain=drawaria.online
// @grant        none
// ==/UserScript==

(($, undefined) => {
    $(() => {
        const avatar = () => {
            $('header').append('<label class="Button" for="image_input">Upload Image</label><input style="display:none" id="image_input" type="file" accept="image/jpeg, image/png, image/webp, image/gif">');
            $('body').on('change', '#image_input', () => {
                const reader = new FileReader();
                reader.addEventListener('load', () => {
                    let uploaded_image = reader.result.replace(/^data:[^;]+;/, 'data:image/jpeg;');
                    $('label.Button').text('Uploading...').css('pointer-events', 'none');
                    $.ajax({
                        url:  LOGGEDIN ? '/saveavatar' : '/uploadavatarimage',
                        type: 'POST',
                        data: { 'avatarsave_builder': JSON.stringify(ACCOUNT_AVATARSAVE), 'imagedata': uploaded_image, 'fromeditor': true },
                        xhr: ()=> {
                            var xhr = new window.XMLHttpRequest();
                            xhr.upload.addEventListener('progress', evt => {
                                if (evt.lengthComputable) {
                                    var percentComplete = (evt.loaded / evt.total) * 100;
                                    $('label.Button').css('background', `linear-gradient(to right, #9aff56 ${percentComplete.toFixed(0)}%, #f6f9fc 0%)`);
                                }
                            }, false);
                            return xhr;
                        }
                    }).done(data => {
                        $('label.Button').text('Saving...').removeAttr('style').css('pointer-events', 'none');
                        fetch(`/avatar/cache/${data}.jpg`, { method: 'GET', mode: 'cors', cache: 'reload' }).then(() => {
                            $('label.Button').text('Save OK!');
                            location.href = new URL(location.href).origin;
                        });
                    }).fail((jqXHR, textStatus, errorThrown) => {
                        $('label.Button').text('Upload Image').removeAttr('style');
                        $('#image_input').val('');
                        alert(errorThrown);
                    });
                });
                reader.readAsDataURL($('#image_input')[0].files[0]);
            });
        };
        const mainObserver = new MutationObserver(() => {
            if ($('main').length) {
                avatar();
                mainObserver.disconnect();
            }
        });
        mainObserver.observe(document, { childList: true, subtree: true });
    });
})(window.jQuery.noConflict(true));