Chants for NovelAI

Chants script for NovelAI

As of 2023-12-08. See the latest version.

// ==UserScript==
// @name         Chants for NovelAI
// @namespace    https://www6.notion.site/dc99953d5f04405c893fba95dace0722
// @version      1
// @description  Chants script for NovelAI
// @author       SenY
// @match        https://novelai.net/image
// @icon         https://www.google.com/s2/favicons?sz=64&domain=novelai.net
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    const getChantURL = function(force){
        if(force === true){
            localStorage.removeItem("chantURL");
        }
        let chantURL = localStorage.getItem("chantURL") || prompt("Input your chants json url.\nThe URL must be a Cors-enabled server (e.g., gist.github.com).", "https://gist.githubusercontent.com/vkff5833/989808aadebf8648831955cdf2a7b3e3/raw/yuuri.json");
        if(chantURL){
            localStorage.setItem("chantURL", chantURL);
        }
        return chantURL;
    }
    let chantURL = getChantURL(null);
    let ui = document.createElement("div");
    document.body.appendChild(ui);
    ui.style.position = "fixed";
    ui.style.right = "0px";
    ui.style.bottom = "0px";
    ui.style.zIndex = "32768";
    ui.style.maxWidth = "25%";
    const Build = function(){
        ui.textContent = "";
        let button = document.createElement("button");
        button.textContent = "Reset Chants URL";
        button.style.color = "red";
        button.addEventListener("click", function(){
            chantURL = getChantURL(true);
            Build();
        });
        ui.appendChild(button);
        let hr = document.createElement("hr");
        hr.style.color = "grey";
        hr.style.borderWidth = "2px";
        ui.appendChild(hr);
        let chants = [];
        const Append = function(key) {
            let chant = chants.find(x => x.name == key.trim());
            if(chant){
                document.querySelector("textarea[placeholder]").value += ", "+chant.content;
            }
        }
        fetch(chantURL, {
            method: "GET",
        }).then((response) => response.json())
            .then((data) => {
                chants = data;
                chants.forEach(chant => {
                    let button = document.createElement("button");
                    button.textContent = chant.name;
                    button.style.color = "black";
                    button.addEventListener("click", function(){
                        Append(chant.name);
                    });
                    ui.appendChild(button);
                });
             });
    }
    let init = false;
    document.addEventListener("DOMContentLoaded", function(){
        if(init === false){
            Build();
            init = true;
        }
    });
})();