From 681f0f0f18a718fea2d073d70c06dba2f7787f65 Mon Sep 17 00:00:00 2001 From: mic Date: Fri, 27 Mar 2026 23:25:09 +0100 Subject: [PATCH] clear cache button added in the options page. see #675 --- _locales/en/messages.json | 25 +++++++++++++++++++++++++ js/mzta-storage.js | 18 ++++++++++++++++++ js/mzta-utils.js | 9 +++++++++ options/mzta-options.html | 7 +++++++ options/mzta-options.js | 21 ++++++++++++++++++++- 5 files changed, 79 insertions(+), 1 deletion(-) diff --git a/_locales/en/messages.json b/_locales/en/messages.json index 620a03a0..15824bc7 100644 --- a/_locales/en/messages.json +++ b/_locales/en/messages.json @@ -331,6 +331,31 @@ "message": "Manage your data placeholders", "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": { "message": "Important Information", "description": "" diff --git a/js/mzta-storage.js b/js/mzta-storage.js index a94695e8..b5699618 100644 --- a/js/mzta-storage.js +++ b/js/mzta-storage.js @@ -344,4 +344,22 @@ export class taStorage { return 0; } } + + /** + * Remove all records (all keys with the storage prefix). + * @returns {Promise} 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; + } + } } diff --git a/js/mzta-utils.js b/js/mzta-utils.js index 985b9d44..8aad2068 100644 --- a/js/mzta-utils.js +++ b/js/mzta-utils.js @@ -827,6 +827,15 @@ export async function getLocalStorageUsedSpace(){ 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) { if (bytes === 0) return '0 Bytes'; const step = 1024; diff --git a/options/mzta-options.html b/options/mzta-options.html index fb792f86..0033a590 100644 --- a/options/mzta-options.html +++ b/options/mzta-options.html @@ -241,6 +241,13 @@ + + __MSG_prefs_cache_title__ + + __MSG_prefs_cache_storage_size__: +   + +
diff --git a/options/mzta-options.js b/options/mzta-options.js index 289a5430..51baa538 100644 --- a/options/mzta-options.js +++ b/options/mzta-options.js @@ -26,8 +26,10 @@ import { isAPIKeyValue, getConnectionType, setTomSelectBorder, - getMiczItUrl + getMiczItUrl, + getCacheStorageUsedSpace } from '../js/mzta-utils.js'; +import { taStorage } from '../js/mzta-storage.js'; import { injectConnectionUI, varConnectionUI, @@ -220,6 +222,11 @@ function resetMaxPromptLength(){ let maxPromptLength = document.getElementById('max_prompt_length'); maxPromptLength.value = 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 () => { @@ -368,6 +375,18 @@ document.addEventListener('DOMContentLoaded', async () => { 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 => { taLog.log("OS: " + info.os); if ((info.os === "linux")&&(prefs_opt.chatgpt_win_height!=0)&&(prefs_opt.chatgpt_win_width!=0)){