Greasy Fork

来自缓存

Greasy Fork is available in English.

Popmundo Songtitle Generator

Adds button to generate random names for songs

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Popmundo Songtitle Generator
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Adds button to generate random names for songs
// @match        https://*.popmundo.com/World/Popmundo.aspx/Character/SongCreate
// @grant        GM_getValue
// @grant        GM_setValue
// @grant        GM.setValue
// @grant        GM.getValue
// ==/UserScript==

(function() {
    'use strict';

    const setValue = GM_setValue || GM.setValue || null;
    const getValue = GM_getValue || GM.getValue || null;

    if (document.location.href.endsWith("Character/SongCreate")) {
        const cacheKey = "random_song_list";
        const songTitleInput = document.querySelector("#ctl00_cphLeftColumn_ctl00_txtSongName");
        if (! songTitleInput) return;

        const randomSongButton = document.createElement("button");
        randomSongButton.innerText = "Random";
        randomSongButton.classList.add("round");
        randomSongButton.addEventListener("click", (e) => {
            e.preventDefault();
            e.target.disabled = true;

            let titles = GM_getValue(cacheKey, "");

            if (titles.length < 1) {
                const httpRequest = new XMLHttpRequest();

                httpRequest.onreadystatechange = function() {
                    if (httpRequest.readyState === 4) {
                        if (httpRequest.status === 200) {
                            const jsonData = JSON.parse(httpRequest.responseText);
                            const titles = jsonData.data.map((entry) => { return entry.name; });
                            const title = titles.shift();
                            songTitleInput.value = title;
                            GM_setValue(cacheKey, titles.join("\n"));
                        } else {
                            songTitleInput.value = "Failed to get random song name :(";
                        }

                        setTimeout(() => {e.target.disabled = false}, 1000);
                    }
                };

                httpRequest.open("GET", "https://story-shack-cdn-v2.glitch.me/generators/song-title-generator?count=6");
                httpRequest.setRequestHeader("Referer", "https://thestoryshack.com/tools/song-title-generator/?v=1");
                httpRequest.send();
            } else {
                titles = titles.split("\n");

                const title = titles.shift();
                songTitleInput.value = title;
                GM_setValue(cacheKey, titles.join("\n"));

                setTimeout(() => {e.target.disabled = false}, 1000);
            }
        });

        songTitleInput.parentElement.style.display = "flex";
        songTitleInput.insertAdjacentElement("afterend", randomSongButton);
    }
})();