File size: 3,404 Bytes
f4a41d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// ==UserScript==
// @name         A1111 - Random Artists to Prompt
// @version      0.3.2
// @namespace    https://github.com/CryDotCom/Random-E621-Artist-A1111
// @updateURL    https://raw.githubusercontent.com/CryDotCom/Random-E621-Artist-A1111/master/R-Artists.js
// @downloadURL  https://raw.githubusercontent.com/CryDotCom/Random-E621-Artist-A1111/master/R-Artists.js
// @description  Add randomly chosen E621 Artist names to a textarea from the top 700+ sorted by post count
// @author       Onocom/Crydotcom
// @match        http://127.0.0.1:7860
// @grant        GM_xmlhttpRequest
// @grant        GM_registerMenuCommand
// ==/UserScript==

(function () {
    'use strict';

    // Function to load names from an online hosted raw text file
    function loadNamesFromURL(url, callback) {
        GM_xmlhttpRequest({
            method: 'GET',
            url: url,
            onload: function (response) {
                if (response.status === 200) {
                    const namesText = response.responseText;
                    const namesList = namesText.split('\n').map(name => name.trim()).filter(Boolean);
                    callback(namesList);
                } else {
                    console.error('Failed to load Artists from the provided URL.');
                }
            },
            onerror: function (error) {
                console.error('Error loading Artists:', error);
            }
        });
    }

    // Function to add names to the textarea
    function addNamesToTextarea(names, count) {
        const textarea = document.activeElement;

        if (textarea.tagName === 'TEXTAREA') {
            const currentContent = textarea.value;

            // Prompt for the number of names to add
            const numberOfNames = count || prompt("How many Artists do you want to add?");
            const namesToAdd = parseInt(numberOfNames, 10);

            if (!isNaN(namesToAdd) && namesToAdd > 0) {
                // Choose random names
                const randomNames = [];
                for (let i = 0; i < namesToAdd; i++) {
                    const randomIndex = Math.floor(Math.random() * names.length);
                    // Replace "_" with a space, and "(artist)" with "\(artist\)"
                    const cleanedName = names[randomIndex].replace(/_/g, ' ').replace(/\(artist\)/g, '\\(artist\\)').trim();
                    randomNames.push(cleanedName);
                }

                // Format the names
                const formattedNames = randomNames.map(names => `by ${names}`).join(', ');

                // Add the formatted names to the end of the current content
                const newContent = currentContent.length > 0 ? currentContent + ', ' + formattedNames : formattedNames;

                // Set the new content to the textarea
                textarea.value = newContent;
            } else {
                alert('Please enter a valid number greater than 0.');
            }
        } else {
            alert('Please focus on a textarea before using this script.');
        }
    }

    // Register the menu command
    GM_registerMenuCommand('Add Artists', function () {
        const url = 'https://raw.githubusercontent.com/CryDotCom/Random-E621-Artist-A1111/main/Artist-Names-E621.txt';
        loadNamesFromURL(url, function (names) {
            addNamesToTextarea(names);
        });
    });
})();