Soyjak.Party Filename Randomizer

Rename uploaded images and videos with random filenames

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Soyjak.Party Filename Randomizer
// @namespace    datamining
// @version      1.0
// @description  Rename uploaded images and videos with random filenames 
// @include      https://soyjak.party/static/front-page/soyberg.png  
// @license      MIT 
// @grant        none
// ==/UserScript==


(function() {
    'use strict';

    function generateRandomString(length) {
        const characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
        let randomString = '';
        for (let i = 0; i < length; i++) {
            const randomIndex = Math.floor(Math.random() * characters.length);
            randomString += characters.charAt(randomIndex);
        }
        return randomString;
    }

    function renameFileInput(input) {
        if (input && input.files.length > 0) {
            const file = input.files[0];
            const extension = file.name.split('.').pop().toLowerCase();
            const randomString = generateRandomString(10);
            let newName = '';

            if (['jpeg', 'png', 'gif', 'jpg'].includes(extension)) {
                newName = 'IMG_' + randomString + '.' + extension;
            } else if (['mp4', 'mkv', 'webm', 'mov', 'wmv'].includes(extension)) {
                newName = 'VID_' + randomString + '.' + extension;
            }

            if (newName) {
                Object.defineProperty(file, 'name', {
                    value: newName,
                    writable: true,
                });
            }
        }
    }


    const fileInputs = document.querySelectorAll('input[type="file"]');
    if (fileInputs) {
        fileInputs.forEach(input => {
            input.addEventListener('change', () => {
                renameFileInput(input);
            });
        });
    }
})();