clear cache button added in the options page. see #675

This commit is contained in:
mic 2026-03-27 23:25:09 +01:00
parent a908efc88d
commit 681f0f0f18
5 changed files with 79 additions and 1 deletions

View file

@ -331,6 +331,31 @@
"message": "Manage your data placeholders", "message": "Manage your data placeholders",
"description": "" "description": ""
}, },
"prefs_cache_title": {
"message": "Cache Storage",
"description": "Title for cache management section in options"
},
"prefs_cache_storage_size": {
"message": "Cache size",
"description": "Label for cache storage size display"
},
"prefs_cache_clear_button": {
"message": "Clear Cache",
"description": "Button to clear all cached message data"
},
"prefs_cache_clear_confirm": {
"message": "Are you sure you want to clear all cached data (summaries, spam reports, translations)? This action cannot be undone.",
"description": "Confirmation dialog for clearing cache"
},
"prefs_cache_clear_done": {
"message": "$COUNT$ records removed.",
"description": "Message shown after cache is cleared",
"placeholders": {
"count": {
"content": "$1"
}
}
},
"prefsInfoTitle": { "prefsInfoTitle": {
"message": "Important Information", "message": "Important Information",
"description": "" "description": ""

View file

@ -344,4 +344,22 @@ export class taStorage {
return 0; return 0;
} }
} }
/**
* Remove all records (all keys with the storage prefix).
* @returns {Promise<number>} The number of deleted records.
*/
static async clearAllRecords() {
try {
let all = await messenger.storage.local.get(null);
let keysToDelete = Object.keys(all).filter(k => k.startsWith(taStorage.STORAGE_KEY_PREFIX));
if (keysToDelete.length > 0) {
await messenger.storage.local.remove(keysToDelete);
}
return keysToDelete.length;
} catch (e) {
console.error('[taStorage.clearAllRecords] error: ' + e);
return 0;
}
}
} }

View file

@ -827,6 +827,15 @@ export async function getLocalStorageUsedSpace(){
return formatBytes(customprompts_space); return formatBytes(customprompts_space);
} }
export async function getCacheStorageUsedSpace(){
let all = await browser.storage.local.get(null);
let cacheSpace = Object.entries(all)
.filter(([key]) => key.startsWith('msg:'))
.map(([key, value]) => key.length + JSON.stringify(value).length)
.reduce((acc, x) => acc + x, 0);
return formatBytes(cacheSpace);
}
function formatBytes(bytes, decimals = 2) { function formatBytes(bytes, decimals = 2) {
if (bytes === 0) return '0 Bytes'; if (bytes === 0) return '0 Bytes';
const step = 1024; const step = 1024;

View file

@ -241,6 +241,13 @@
</label> </label>
</td> </td>
</tr> </tr>
<tr>
<td><span class="opt_title">__MSG_prefs_cache_title__</span></td>
<td>
__MSG_prefs_cache_storage_size__: <span id="cache_storage_size"></span>
&nbsp;<button id="btnClearCache" class="btn_small">__MSG_prefs_cache_clear_button__</button>
</td>
</tr>
</table> </table>
<div id="btn_custom_prompts"> <div id="btn_custom_prompts">
<button id="btnManagePrompts">__MSG_prefs_OptionText_btnManagePrompts__</button> <button id="btnManagePrompts">__MSG_prefs_OptionText_btnManagePrompts__</button>

View file

@ -26,8 +26,10 @@ import {
isAPIKeyValue, isAPIKeyValue,
getConnectionType, getConnectionType,
setTomSelectBorder, setTomSelectBorder,
getMiczItUrl getMiczItUrl,
getCacheStorageUsedSpace
} from '../js/mzta-utils.js'; } from '../js/mzta-utils.js';
import { taStorage } from '../js/mzta-storage.js';
import { import {
injectConnectionUI, injectConnectionUI,
varConnectionUI, varConnectionUI,
@ -220,6 +222,11 @@ function resetMaxPromptLength(){
let maxPromptLength = document.getElementById('max_prompt_length'); let maxPromptLength = document.getElementById('max_prompt_length');
maxPromptLength.value = prefs_default.max_prompt_length; maxPromptLength.value = prefs_default.max_prompt_length;
browser.storage.sync.set({max_prompt_length: prefs_default.max_prompt_length}); browser.storage.sync.set({max_prompt_length: prefs_default.max_prompt_length});
}
async function updateCacheSize() {
let size = await getCacheStorageUsedSpace();
document.getElementById('cache_storage_size').textContent = size;
} }
document.addEventListener('DOMContentLoaded', async () => { document.addEventListener('DOMContentLoaded', async () => {
@ -368,6 +375,18 @@ document.addEventListener('DOMContentLoaded', async () => {
await browser.tabs.create({ url: "../pages/onboarding/onboarding.html" }); await browser.tabs.create({ url: "../pages/onboarding/onboarding.html" });
}); });
// Cache management
updateCacheSize();
document.getElementById('btnClearCache').addEventListener('click', async () => {
if (!confirm(browser.i18n.getMessage("prefs_cache_clear_confirm"))) {
return;
}
let count = await taStorage.clearAllRecords();
alert(browser.i18n.getMessage("prefs_cache_clear_done", [String(count)]));
updateCacheSize();
});
browser.runtime.getPlatformInfo().then(info => { browser.runtime.getPlatformInfo().then(info => {
taLog.log("OS: " + info.os); taLog.log("OS: " + info.os);
if ((info.os === "linux")&&(prefs_opt.chatgpt_win_height!=0)&&(prefs_opt.chatgpt_win_width!=0)){ if ((info.os === "linux")&&(prefs_opt.chatgpt_win_height!=0)&&(prefs_opt.chatgpt_win_width!=0)){