File size: 5,840 Bytes
ac25389
 
 
 
 
 
 
 
 
 
3297211
 
 
ac25389
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3297211
 
 
 
 
 
 
ac25389
 
 
 
 
3297211
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ac25389
 
 
 
 
 
 
 
 
 
3297211
ac25389
 
 
 
 
 
 
3297211
 
 
ac25389
 
 
 
425b9a7
 
 
 
 
ac25389
 
3297211
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ac25389
 
 
 
425b9a7
 
 
 
 
ac25389
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
var key_down_history = [];
var currentIndex = -1;

var gradioContainer = null;

var isInIframe = (window.self !== window.top);
var currentTime = new Date().getTime();

let windowWidth = window.innerWidth;
let lines_json = []
let cn_lines_json = []
let en_lines_json = []
let ja_lines_json = []

function addInit() {
    return true;
}

function gradioApp() {
    const elems = document.getElementsByTagName('gradio-app');
    const elem = elems.length == 0 ? document : elems[0];

    if (elem !== document) {
        elem.getElementById = function(id) {
            return document.getElementById(id);
        };
    }
    return elem.shadowRoot ? elem.shadowRoot : elem;
}

function initialize() {
    gradioObserver.observe(gradioApp(), { childList: true, subtree: true });

    gradioContainer = gradioApp().querySelector(".gradio-container");

    loadData();

    return true;
}

async function loadData() {
    const fetchLines = fetch('file=assets/lines.json')
        .then(response => {
            if (!response.ok) {
                throw new Error('Network response was not ok');
            }
            return response.json();
        });

    const fetchCnLines = fetch('file=assets/cn_lines.json')
        .then(response => {
            if (!response.ok) {
                throw new Error('Network response was not ok');
            }
            return response.json();
        });

    const fetchEnLines = fetch('file=assets/en_lines.json')
        .then(response => {
            if (!response.ok) {
                throw new Error('Network response was not ok');
            }
            return response.json();
        });

    const fetchJaLines = fetch('file=assets/ja_lines.json')
        .then(response => {
            if (!response.ok) {
                throw new Error('Network response was not ok');
            }
            return response.json();
        });

    Promise.all([fetchLines, fetchCnLines, fetchEnLines, fetchJaLines])
        .then(([linesData, cnLinesData, enLinesData, jaLinesData]) => {
            lines_json = linesData;
            cn_lines_json = cnLinesData;
            en_lines_json = enLinesData;
            ja_lines_json = jaLinesData;
            set_speak_examples();
        })
        .catch(error => {
            console.error('There has been a problem with your fetch operation:', error);
        });
}

function set_speak_examples() {
    buttons = gradioApp().querySelectorAll("#examples div button");
    speak_input = gradioApp().querySelector("#speak_input label textarea");
    voice = gradioApp().querySelector("#voice_list label input");

    let lines_praise = lines_json["夸夸你 | Praise"];
    let lines_scripts = lines_json["游戏台词 | Scripts"];
    let lines_meme = lines_json["玩梗 | Meme"];
    let praiseArray = Object.values(lines_praise);
    let scriptsArray = Object.values(lines_scripts);
    let memeArray = Object.values(lines_meme);
    let cnlinesArray = Object.values(cn_lines_json);
    let enlinesArray = Object.values(en_lines_json);
    let jalinesArray = Object.values(ja_lines_json);

    buttons[0].addEventListener("click", function() {
        const randomString = praiseArray[Math.floor(Math.random() * praiseArray.length)];
        speak_input.value = randomString;
        var event = new Event('input', {
            bubbles: true,
            cancelable: true,
        });
        speak_input.dispatchEvent(event);
    });
    buttons[1].addEventListener("click", function() {
        if (voice.value == "Chinese") {
            const randomString = cnlinesArray[Math.floor(Math.random() * cnlinesArray.length)];
            speak_input.value = randomString;
            var event = new Event('input', {
                bubbles: true,
                cancelable: true,
            });
            speak_input.dispatchEvent(event);
        } else if (voice.value == "English") {
            const randomString = enlinesArray[Math.floor(Math.random() * enlinesArray.length)];
            speak_input.value = randomString;
            var event = new Event('input', {
                bubbles: true,
                cancelable: true,
            });
            speak_input.dispatchEvent(event);
        } else if (voice.value == "Japanese"){
            const randomString = jalinesArray[Math.floor(Math.random() * jalinesArray.length)];
            speak_input.value = randomString;
            var event = new Event('input', {
                bubbles: true,
                cancelable: true,
            });
            speak_input.dispatchEvent(event);
        }


        // const randomString = scriptsArray[Math.floor(Math.random() * scriptsArray.length)];
        // speak_input.value = randomString;
        // var event = new Event('input', {
        //     bubbles: true,
        //     cancelable: true,
        // });
        // speak_input.dispatchEvent(event);
    });
    buttons[2].addEventListener("click", function() {
        const randomString = memeArray[Math.floor(Math.random() * memeArray.length)];
        speak_input.value = randomString;
        var event = new Event('input', {
            bubbles: true,
            cancelable: true,
        });
        speak_input.dispatchEvent(event);
    });

}

// 监视页面内部 DOM 变动
var gradioObserver = new MutationObserver(function (mutations) {
    for (var i = 0; i < mutations.length; i++) {
        if (mutations[i].addedNodes.length) {
            if (addInit()) {
                gradioObserver.disconnect();
                return;
            }
        }
    }
});

// 监视页面变化
window.addEventListener("DOMContentLoaded", function () {
    windowWidth = window.innerWidth;
    gradioApp().addEventListener("render", initialize);
    isInIframe = (window.self !== window.top);
});

console.log("Welcome to TalkingFlower!");