File size: 4,833 Bytes
0aabf7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
function saveToHistory() {
    const historyItem = {
        query: document.getElementById('query').value,
        promptEn: document.getElementById('promptEn').value,
        promptMyLanguage: document.getElementById('promptMyLanguage').value,
        danbooruTags: document.getElementById('danbooruTags').value,
        timestamp: new Date().toISOString()
    };

    let history = JSON.parse(localStorage.getItem('gemini_prompt_history') || '[]');
    history.unshift(historyItem);
    history = history.slice(0, 10); // 最新の10件のみを保持
    localStorage.setItem('gemini_prompt_history', JSON.stringify(history));

    updateHistoryList();
}
function updateHistoryList() {
    const historyList = document.getElementById('historyList');
    const noHistoryMessage = document.getElementById('noHistoryMessage');
    historyList.innerHTML = '';

    const history = JSON.parse(localStorage.getItem('gemini_prompt_history') || '[]');

    if (history.length === 0) {
        noHistoryMessage.classList.remove('d-none');
        historyList.classList.add('d-none');
    } else {
        noHistoryMessage.classList.add('d-none');
        historyList.classList.remove('d-none');

        history.forEach((item, index) => {
            const li = document.createElement('li');
            li.className = 'list-group-item list-group-item-action d-flex justify-content-between align-items-start';

            const contentDiv = document.createElement('div');
            contentDiv.className = 'ms-2 me-auto';
            contentDiv.style.width = 'calc(100% - 40px)';
            contentDiv.style.cursor = 'pointer';
            contentDiv.onclick = () => loadHistoryItem(index);

            const queryDiv = document.createElement('div');
            queryDiv.className = 'text-truncate';
            queryDiv.textContent = item.query;

            const dateDiv = document.createElement('div');
            dateDiv.className = 'small text-muted';
            dateDiv.textContent = new Date(item.timestamp).toLocaleString();

            contentDiv.appendChild(queryDiv);
            contentDiv.appendChild(dateDiv);

            const deleteButton = document.createElement('button');
            deleteButton.className = 'btn btn-danger btn-sm';
            deleteButton.innerHTML = '<i class="fas fa-trash"></i>';
            deleteButton.onclick = (e) => {
                e.stopPropagation();
                deleteHistoryItem(index);
            };

            li.appendChild(contentDiv);
            li.appendChild(deleteButton);
            historyList.appendChild(li);
        });
    }
}
function deleteHistoryItem(index) {
    if (confirm('この履歴項目を削除してもよろしいですか?')) {
        let history = JSON.parse(localStorage.getItem('gemini_prompt_history') || '[]');
        history.splice(index, 1);
        localStorage.setItem('gemini_prompt_history', JSON.stringify(history));
        updateHistoryList();
    }
}
function loadHistoryItem(index) {
    const history = JSON.parse(localStorage.getItem('gemini_prompt_history') || '[]');
    const item = history[index];

    document.getElementById('query').value = item.query;
    document.getElementById('promptEn').value = item.promptEn;
    document.getElementById('promptMyLanguage').value = item.promptMyLanguage;
    document.getElementById('danbooruTags').value = item.danbooruTags;

    saveToUserStorage(true);
}

function clearHistory() {
    if (confirm('本当に履歴をすべて削除してもよろしいですか?')) {
        localStorage.removeItem('gemini_prompt_history');
        updateHistoryList();
    }
}

function createHistoryItem(item, index) {
    const li = document.createElement('li');
    li.className = 'list-group-item d-flex justify-content-between align-items-center';
    li.textContent = item.query;

    const buttonsContainer = document.createElement('div');

    const useButton = document.createElement('button');
    useButton.className = 'btn btn-sm btn-primary me-2';
    useButton.innerHTML = '<i class="fas fa-redo"></i>';
    useButton.onclick = () => useHistoryItem(index);

    const deleteButton = document.createElement('button');
    deleteButton.className = 'btn btn-sm btn-danger';
    deleteButton.innerHTML = '<i class="fas fa-trash"></i>';
    deleteButton.onclick = () => {
        // 個別の履歴項目削除時にも確認ポップアップを表示
        if (confirm('この履歴項目を削除してもよろしいですか?')) {
            deleteHistoryItem(index);
        }
    };

    buttonsContainer.appendChild(useButton);
    buttonsContainer.appendChild(deleteButton);
    li.appendChild(buttonsContainer);

    return li;
}