-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
110 lines (95 loc) · 3.83 KB
/
script.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
// Get DOM elements
const themeToggle = document.getElementById('theme-toggle');
const wordInput = document.getElementById('word-input');
const characterCount = document.getElementById('character-count');
const seedLengthSlider = document.getElementById('seed-length');
const seedLengthValue = document.getElementById('seed-length-value');
const generateBtn = document.getElementById('generate-btn');
const lastSeedsContainer = document.getElementById('last-seeds-container');
const lastSeedsList = document.getElementById('last-seeds-list');
const copyAllBtn = document.getElementById('copy-all-btn');
const sentence = document.getElementById('sentence');
// Initialize variables
let lastSeeds = [];
let darkMode = false;
// Load theme from localStorage
const storedTheme = localStorage.getItem('theme');
if (storedTheme === 'dark') {
darkMode = true;
document.body.classList.add('dark-mode');
themeToggle.checked = true;
}
// Update character count and sentence
const updateCharacterCountAndSentence = () => {
const inputValue = wordInput.value;
const words = inputValue.split(/[,\s]+/).filter(Boolean);
const validChars = words.join('').length;
characterCount.textContent = `${validChars}/16`;
wordInput.value = words.join(', ');
const seedLength = seedLengthSlider.value;
const wordsText = words.length > 0 ? `the word${words.length > 1 ? 's' : ''} "${words.join('", "')}"` : 'nothing but letters, numbers';
const sentenceText = `I want a ${seedLength} characters long seed that contains ${wordsText} and a nice cup of randomness.`;
sentence.textContent = sentenceText;
};
// Generate random seed
const generateSeed = (length, words) => {
const characters = 'abcdefghijklmnopqrstuvwxyz0123456789';
let seed = '';
let wordIndex = 0;
for (let i = 0; i < length; i++) {
if (words.length > 0 && wordIndex < words.length && Math.random() < 0.2) {
seed += words[wordIndex];
wordIndex++;
} else {
seed += characters.charAt(Math.floor(Math.random() * characters.length));
}
}
return seed;
};
// Generate new seed
const generateNewSeed = () => {
const seedLength = seedLengthSlider.value;
const words = wordInput.value.split(/[,\s]+/).filter(Boolean);
const newSeed = generateSeed(seedLength, words);
lastSeeds.unshift(newSeed);
lastSeeds = lastSeeds.slice(0, 5);
updateLastSeedsList();
lastSeedsContainer.classList.remove('hidden');
};
// Update last seeds list
const updateLastSeedsList = () => {
lastSeedsList.innerHTML = '';
lastSeeds.forEach(seed => {
const seedBox = document.createElement('div');
seedBox.className = 'seed-box';
const seedSpan = document.createElement('span');
seedSpan.textContent = seed;
seedBox.appendChild(seedSpan);
const copyBtn = document.createElement('button');
copyBtn.textContent = 'Copy';
copyBtn.addEventListener('click', () => {
navigator.clipboard.writeText(seed);
alert(`Seed "${seed}" copied to clipboard!`);
});
seedBox.appendChild(copyBtn);
lastSeedsList.appendChild(seedBox);
});
};
// Copy all seeds
const copyAllSeeds = () => {
const seedsText = lastSeeds.join('\n');
navigator.clipboard.writeText(seedsText);
alert('All seeds copied to clipboard!');
};
// Event listeners
wordInput.addEventListener('input', updateCharacterCountAndSentence);
seedLengthSlider.addEventListener('input', updateCharacterCountAndSentence);
generateBtn.addEventListener('click', generateNewSeed);
copyAllBtn.addEventListener('click', copyAllSeeds);
themeToggle.addEventListener('change', () => {
darkMode = !darkMode;
document.body.classList.toggle('dark-mode');
localStorage.setItem('theme', darkMode ? 'dark' : 'light');
});
// Initialize
updateCharacterCountAndSentence();