Skip to content

Commit

Permalink
Merge pull request #6 from chinkan/feature/fix-regenerating
Browse files Browse the repository at this point in the history
refactor: clean up code and improve UI and fix bugs
  • Loading branch information
chinkan authored Sep 15, 2024
2 parents cc62cf0 + 319842f commit f2a3130
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 65 deletions.
3 changes: 0 additions & 3 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import PromptFactory from './prompt-providers/prompt-factory.js';
import { getStorageData } from './utils/storage.js';

chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
console.log('Background script received message:', request);
if (request.action === 'summarize') {
handleSummarize(request, sendResponse);
return true; // 表示我們會異步發送回應
Expand All @@ -17,7 +16,6 @@ async function handleSummarize(request, sendResponse) {
'selectedLLMIndex',
'language',
]);
console.log('get from storage', result);

if (
!result.llmConfigs ||
Expand Down Expand Up @@ -45,7 +43,6 @@ async function handleSummarize(request, sendResponse) {
prompts.userPrompt,
prompts.systemPrompt
);
console.log('summary', summary);

if (summary && typeof summary.summary === 'string') {
sendResponse({ summary: summary.summary });
Expand Down
32 changes: 16 additions & 16 deletions options.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ <h1>About WizMuse</h1>
/>
<div class="profile-info">
<p
><i class="material-icons">info</i> Version:0.2
><i class="material-icons">info</i> Version:0.3
beta</p
>
<p
Expand Down Expand Up @@ -291,21 +291,21 @@ <h1>Prompts</h1>
</div>
</form>
</div>
<div id="history-page" style="display: none">
<h1>Summary History</h1>
<table id="history-table">
<thead>
<tr>
<th>URL</th>
<th>Title</th>
<th>Summarized Time</th>
<th>Summary</th>
<th>Actions</th>
</tr>
</thead>
<tbody></tbody>
</table>
</div>
</div>
<div id="history-page" style="display: none">
<h1>Summary History</h1>
<table id="history-table">
<thead>
<tr>
<th>URL</th>
<th>Title</th>
<th>Summarized Time</th>
<th>Summary</th>
<th>Actions</th>
</tr>
</thead>
<tbody></tbody>
</table>
</div>
</div>
<script type="module" src="options.js"></script>
Expand Down
52 changes: 28 additions & 24 deletions options.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ document.addEventListener('DOMContentLoaded', function () {
isFirstInstall: false,
llmConfigs: [
{
name: 'OpenAI',
name: 'OpenAI Example',
provider: 'openai',
apiKey: 'sk-proj-93345678901234567890', // example api key
model: 'gpt-4o',
Expand Down Expand Up @@ -126,7 +126,7 @@ document.addEventListener('DOMContentLoaded', function () {
languageSelect.value = result.language;
}

loadModels();
// loadModels();
}
if (result.selectedLLMIndex) {
defaultSelect.value = result.selectedLLMIndex;
Expand Down Expand Up @@ -380,13 +380,13 @@ document.addEventListener('DOMContentLoaded', function () {

function loadSummaryHistory() {
historyTable.innerHTML = '';
getStorageData(null).then((items) => {
for (let key in items) {
getStorageData(['histories']).then((items) => {
for (let key in items.histories) {
if (key.startsWith('http')) {
const data = items[key];
const data = items.histories[key];
const row = historyTable.insertRow();
row.innerHTML = `
<td>${key}</td>
<td><a href="${key}" target="_blank">${key}</a></td>
<td>${data.title}</td>
<td>${new Date(data.timestamp).toLocaleString()}</td>
<td>
Expand Down Expand Up @@ -416,42 +416,46 @@ document.addEventListener('DOMContentLoaded', function () {
const row = e.target.closest('tr');
const url = row.cells[0].textContent;
if (e.target.tagName === 'TD') {
getStorageData(url).then((result) => {
if (result[url]) {
alert(result[url].summary);
getStorageData(['histories']).then((result) => {
if (result.histories[url]) {
alert(result.histories[url].summary);
}
});
} else if (
(e.target.tagName === 'I' &&
e.target.parentElement.classList.contains('copy-btn')) ||
e.target.classList.contains('copy-btn')
) {
getStorageData(url).then((result) => {
if (result[url]) {
navigator.clipboard
.writeText(result[url].summary)
.then(() => {
alert('Summary copied to clipboard');
})
.catch((err) => {
console.error('Copy failed:', err);
alert('Copy failed, please copy manually');
});
getStorageData(['histories']).then((result) => {
if (result.histories[url]) {
setTimeout(() => {
navigator.clipboard
.writeText(result.histories[url].summary)
.then(() => {
alert('Summary copied to clipboard');
})
.catch((err) => {
console.error('Copy failed:', err);
alert('Copy failed, please copy manually');
});
}, 100);
}
});
} else if (
(e.target.tagName === 'I' &&
e.target.parentElement.classList.contains('delete-btn')) ||
e.target.classList.contains('delete-btn')
) {
removeStorageData(url).then(() => {
row.remove();
getStorageData(['histories']).then((result) => {
if (confirm('Are you sure you want to delete this history?')) {
delete result.histories[url];
setStorageData({ histories: result });
row.remove();
}
});
}
});

loadSummaryHistory();

const promptsTable = document
.getElementById('prompts-table')
.getElementsByTagName('tbody')[0];
Expand Down
45 changes: 29 additions & 16 deletions popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,12 @@ document.addEventListener('DOMContentLoaded', async function () {
handleError(response.error);
} else if (response && response.summary) {
await setStorageData({
[url]: {
summary: response.summary,
title: title,
timestamp: Date.now(),
histories: {
[url]: {
summary: response.summary,
title: title,
timestamp: Date.now(),
},
},
});
displaySummary(response.summary);
Expand Down Expand Up @@ -194,13 +196,21 @@ document.addEventListener('DOMContentLoaded', async function () {
regenerateContainer.appendChild(modelSelect);

// 創建提示選擇下拉選單
const promptSelect = createCustomSelect(
'Select a prompt',
[{ name: 'Use Default Prompt' }, ...result.prompts],
(prompt, index) => prompt.name,
true // 指示這是 promptSelect
);
regenerateContainer.appendChild(promptSelect);
let promptSelect;
let selectedPromptIndex = -1;
if (result.prompts && result.prompts.length > 0) {
const promptOptions = [
{ name: 'Use Default Prompt' },
...result.prompts,
];
promptSelect = createCustomSelect(
'Select a prompt',
promptOptions,
(prompt, index) => prompt.name,
true // 指示這是 promptSelect
);
regenerateContainer.appendChild(promptSelect);
}

const confirmButton = document.createElement('button');
confirmButton.textContent = 'Confirm';
Expand All @@ -209,9 +219,10 @@ document.addEventListener('DOMContentLoaded', async function () {
const selectedModelIndex =
modelSelect.querySelector('.option.selected')?.dataset
.value;
const selectedPromptIndex =
promptSelect.querySelector('.option.selected')?.dataset
.value || -1;
const selectedPromptIndex = promptSelect
? promptSelect.querySelector('.option.selected')?.dataset
.value || -1
: -1;
if (selectedModelIndex !== undefined) {
await callback(selectedModelIndex, selectedPromptIndex);
regenerateContainer.style.display = 'none';
Expand All @@ -225,12 +236,14 @@ document.addEventListener('DOMContentLoaded', async function () {
function enableConfirmButton() {
confirmButton.disabled = !(
modelSelect.querySelector('.option.selected') &&
promptSelect.querySelector('.option.selected')
(promptSelect
? promptSelect.querySelector('.option.selected')
: true)
);
}

// 為兩個選擇器添加事件監聽器
[modelSelect, promptSelect].forEach((select) => {
[modelSelect, promptSelect].filter(Boolean).forEach((select) => {
select
.querySelector('.selected-option')
.addEventListener('click', () => {
Expand Down
6 changes: 0 additions & 6 deletions utils/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,9 @@
export const setStorageData = (patch) => {
return new Promise((resolve, reject) => {
chrome.storage.sync.get(null, (result) => {
console.log('現有數據', result);
const newData = { ...result, ...patch };
console.log('新數據', newData);
chrome.storage.sync.set(newData, () => {
console.log('新數據已存儲', newData);
if (chrome.runtime.lastError) {
console.log('錯誤', chrome.runtime.lastError);
reject(chrome.runtime.lastError);
} else {
resolve();
Expand All @@ -22,10 +18,8 @@ export const setStorageData = (patch) => {

// 從Chrome同步存儲獲取數據
export const getStorageData = (key) => {
console.log('獲取鍵', key);
return new Promise((resolve, reject) => {
chrome.storage.sync.get(key, (result) => {
console.log('獲取結果', result);
if (chrome.runtime.lastError) {
reject(chrome.runtime.lastError);
} else {
Expand Down

0 comments on commit f2a3130

Please sign in to comment.