-
Notifications
You must be signed in to change notification settings - Fork 0
/
catscript.js
187 lines (155 loc) · 5.77 KB
/
catscript.js
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
180
181
182
183
184
185
186
187
function generateMeows(userMessage) {
const meows = Math.floor(Math.random() * 10) + 1;
let meowSentence = '';
const punctuation = ['.', '!', '?', ',', ''];
const emojis = ['😸', '🐟', '🐾', '🐕', '🧶']; // Random emojis
const sounds = ['Purr', 'Hiss', 'Mrow', 'Chirp']; // Random cat sounds
let capitalizeNext = true;
// Trigger word reactions
const triggers = {
"tuna": "Tuna! I love tuna!",
"fish": "Fish? Yum!",
"dog": "Dog? Hiss! I'm a cat, remember?",
"yarn": "Yarn! Let me pounce on it!"
};
// Check for trigger words
const lowerMessage = userMessage.toLowerCase();
for (const word in triggers) {
if (lowerMessage.includes(word)) {
return triggers[word];
}
}
for (let i = 0; i < meows; i++) {
if (capitalizeNext) {
meowSentence += 'Meow';
capitalizeNext = false;
} else {
meowSentence += ' meow';
}
// Randomly add punctuation mid-sentence
if (i < meows - 1) {
const randomPunctuation = punctuation[Math.floor(Math.random() * punctuation.length)];
meowSentence += randomPunctuation ? randomPunctuation + ' ' : ' ';
if (['.', '!', '?'].includes(randomPunctuation)) {
capitalizeNext = true;
}
} else {
const lastChar = userMessage.slice(-1);
if (lastChar === '?') {
meowSentence += punctuation[Math.floor(Math.random() * 2)];
} else {
meowSentence += punctuation[Math.floor(Math.random() * 3)];
}
}
// Randomly add emoji or sound, ensuring proper spacing
if (Math.random() < 0.8) {
if (Math.random() < 0.5) {
const randomEmoji = emojis[Math.floor(Math.random() * emojis.length)];
meowSentence += ' ' + randomEmoji;
} else {
let randomSound = sounds[Math.floor(Math.random() * sounds.length)];
if (capitalizeNext) randomSound = randomSound.charAt(0).toUpperCase() + randomSound.slice(1);
meowSentence += ' ' + randomSound;
if (i === meows - 1 && !meowSentence.endsWith('.')) {
meowSentence += '.';
}
capitalizeNext = false;
}
}
}
// Ensure proper spacing, capitalization, and remove unnecessary spaces
meowSentence = meowSentence.replace(/\s{2,}/g, ' ').trim().replace(/(\s*([.!?]))/g, '$2 ');
// Remove extra spaces before ending punctuation
meowSentence = meowSentence.replace(/\s([.!?])/g, '$1');
// Remove any double spaces in the final sentence
meowSentence = meowSentence.replace(/\s{2,}/g, ' ');
return meowSentence.trim();
}
function sendMessage() {
const input = document.getElementById('user-input');
const messageText = input.value.trim();
if (messageText) {
addMessage(messageText, 'user-message');
saveChatHistory(messageText, 'user-message');
const catResponse = generateMeows(messageText);
addMessage(catResponse, 'cat-message');
saveChatHistory(catResponse, 'cat-message');
input.value = '';
toggleSendButton();
}
}
function addMessage(text, className) {
const chatMessages = document.getElementById('chat-messages');
const messageDiv = document.createElement('div');
messageDiv.className = `message ${className} ${className === 'cat-message' ? 'fade-in' : ''}`;
messageDiv.textContent = text;
chatMessages.appendChild(messageDiv);
chatMessages.scrollTop = chatMessages.scrollHeight;
}
function saveColorMode() {
const isGreenMode = document.body.classList.contains('green-mode');
localStorage.setItem('colorMode', isGreenMode ? 'green-mode' : 'light-mode');
}
function applySavedColorMode() {
const savedMode = localStorage.getItem('colorMode');
if (savedMode === 'green-mode') {
document.body.classList.add('green-mode');
} else {
document.body.classList.remove('green-mode');
}
}
document.getElementById('toggle-mode').addEventListener('click', () => {
document.body.classList.toggle('green-mode');
saveColorMode();
});
applySavedColorMode();
function resetChat() {
const chatMessages = document.getElementById('chat-messages');
chatMessages.innerHTML = '';
localStorage.removeItem('chatHistory');
}
function saveChatHistory(message, type) {
let history = JSON.parse(localStorage.getItem('chatHistory')) || [];
history.push({ text: message, type });
localStorage.setItem('chatHistory', JSON.stringify(history));
}
function loadChatHistory() {
const history = JSON.parse(localStorage.getItem('chatHistory')) || [];
history.forEach(message => {
addMessage(message.text, message.type);
});
}
document.getElementById('send-button').addEventListener('click', sendMessage);
document.getElementById('user-input').addEventListener('keypress', (event) => {
if (event.key === 'Enter') {
sendMessage();
}
});
document.getElementById('reset').addEventListener('click', resetChat);
loadChatHistory();
function setFaviconAndLogo() {
const favicon = document.getElementById('favicon');
const appleTouchIcon = document.getElementById('apple-touch-icon');
const chatLogo = document.getElementById('chat-logo');
if (window.matchMedia && window.matchMedia('(prefers-color-scheme: light)').matches) {
favicon.href = 'LightModeFavicon.ico';
appleTouchIcon.href = 'LightModeFavicon.ico';
chatLogo.src = 'LightModeHeader.png';
} else {
favicon.href = 'DarkModeFavicon.ico';
appleTouchIcon.href = 'DarkModeFavicon.ico';
chatLogo.src = 'DarkModeHeader.png';
}
}
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', setFaviconAndLogo);
setFaviconAndLogo();
function toggleSendButton() {
const input = document.getElementById('user-input');
const sendButton = document.getElementById('send-button');
if (input.value.trim() === '') {
sendButton.disabled = true;
} else {
sendButton.disabled = false;
}
}
document.getElementById('user-input').addEventListener('input', toggleSendButton);