Usuario
<script>
let nouns = [];
let conjunctions = [];
let verbs = [];
function addWord(type) {
const input = document.getElementById(`${type}Input`);
if (input.value) {
if (type === 'noun') nouns.push(input.value);
else if (type === 'conj') conjunctions.push(input.value);
else verbs.push(input.value);
input.value = '';
displayWords();
}
}
function displayWords() {
document.getElementById('nounList').innerHTML = 'Nouns: ' + nouns.join(', ');
document.getElementById('conjList').innerHTML = 'Conjunctions: ' + conjunctions.join(', ');
document.getElementById('verbList').innerHTML = 'Verbs: ' + verbs.join(', ');
}
function getRandomElement(arr) {
return arr[Math.floor(Math.random() * arr.length)];
}
function outputWords() {
const randomNoun = getRandomElement(nouns);
const randomConj = getRandomElement(conjunctions);
const randomVerb = getRandomElement(verbs);
const newOutput = `${randomNoun} ${randomConj} ${randomVerb}\n`; // 各文の末尾に改行を追加
let currentOutput = document.getElementById('outputArea').textContent;
currentOutput += (currentOutput.length > 0 ? '\n' : '') + newOutput; // 新しい出力の前に改行を挿入
// 単語の境界を考慮して40文字ごとに改行を挿入
let formattedOutput = '';
let lineLength = 0;
currentOutput.split(' ').forEach(word => {
if (lineLength + word.length > 40) {
formattedOutput += '\n';
lineLength = 0;
}
formattedOutput += (lineLength > 0 ? ' ' : '') + word;
lineLength += word.length + 1; // 単語の長さ + スペース
});
document.getElementById('outputArea').textContent = formattedOutput.trim();
}
function downloadAsText() {
const textToWrite = 'Nouns:\n' + nouns.join('\n') + '\n\nConjunctions:\n' + conjunctions.join('\n') + '\n\nVerbs:\n' + verbs.join('\n');
const textBlob = new Blob([textToWrite], { type: 'text/plain' });
const downloadLink = document.createElement('a');
downloadLink.download = 'WordList.txt';
downloadLink.href = window.URL.createObjectURL(textBlob);
downloadLink.style.display = 'none';
document.body.appendChild(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink);
}
function loadFile(event) {
const file = event.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = function(e) {
const fileContents = e.target.result;
parseFileContents(fileContents);
displayWords();
};
reader.readAsText(file);
}
}
function parseFileContents(contents) {
const lines = contents.split(/\r\n|\n|\r/);
let currentCategory = null;
lines.forEach(line => {
if (line.startsWith('Nouns:')) {
currentCategory = nouns;
} else if (line.startsWith('Conjunctions:')) {
currentCategory = conjunctions;
} else if (line.startsWith('Verbs:')) {
currentCategory = verbs;
} else if (line && currentCategory !== null) {
currentCategory.push(line);
}
});
}
function downloadOutputAsText() {
const textToWrite = document.getElementById('outputArea').textContent;
const textBlob = new Blob([textToWrite], { type: 'text/plain' });
const downloadLink = document.createElement('a');
downloadLink.download = 'Output.txt';
downloadLink.href = window.URL.createObjectURL(textBlob);
downloadLink.style.display = 'none';
document.body.appendChild(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink);
}
function loadOutputFromFile(event) {
const file = event.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = function(e) {
const fileContents = e.target.result;
document.getElementById('outputArea').textContent = fileContents;
};
reader.readAsText(file);
}
}
</script>
あなたに構築してもらったコードです。1点改修をお願いします。 ・WordListの表示部分に折り畳み機能(on or off)をつけてほしい.