diff --git a/public/manifest.json b/public/manifest.json index e42e9f1..9937da5 100644 --- a/public/manifest.json +++ b/public/manifest.json @@ -30,7 +30,6 @@ "storage", "contextMenus", "notifications", - "tabs", "offscreen" ], diff --git a/src/background/managers/completion-handler.ts b/src/background/managers/completion-handler.ts index fc4509f..432e7e9 100644 --- a/src/background/managers/completion-handler.ts +++ b/src/background/managers/completion-handler.ts @@ -4,6 +4,8 @@ import { addCompletedSession } from '../core/pomodoro-history'; import { settingsManager } from './settings-manager'; import { debugLogger } from '../services/debug-logger'; +const COMPLETION_PAGE_TAB_ID_KEY = 'completion_page_tab_id'; + export class CompletionHandler { private lastState: TimerState | null = null; @@ -134,16 +136,12 @@ export class CompletionHandler { } private async closeCompletionPages(): Promise { - const tabs = await chrome.tabs.query({ url: chrome.runtime.getURL('phaseComplete.html') }); - console.log('[DEBUG][CompletionHandler] closeCompletionPages:', { - timestamp: new Date().toISOString(), - tabsFound: tabs.length, - tabIds: tabs.map(t => t.id) - }); - - for (const tab of tabs) { - console.log('[DEBUG][CompletionHandler] Closing completion tab:', { tabId: tab.id }); - await chrome.tabs.remove(tab.id!); + // actually close only the last one + const completionTabId = await this.getAndClearCompletionPageTabId() + + if (completionTabId) { + console.log('[DEBUG][CompletionHandler] Closing completion tab:', { tabId: completionTabId }); + await chrome.tabs.remove(completionTabId); } } @@ -172,6 +170,10 @@ export class CompletionHandler { newTabId: newTab.id, newTabIndex: newTab.index }); + + if (typeof newTab.id !== 'undefined') { + await this.saveCompletionPageTabId(newTab.id); + } } catch (error) { console.error('[DEBUG][CompletionHandler] Error opening completion page:', error); // Fallback to opening without positioning @@ -185,6 +187,34 @@ export class CompletionHandler { } } + public async saveCompletionPageTabId(tabId: number): Promise { + try { + await chrome.storage.local.set({ [COMPLETION_PAGE_TAB_ID_KEY]: tabId }); + console.log('[DEBUG][CompletionHandler] Saved Completion Page Tab Id: ', tabId) + + } catch (error) { + console.error('Error saving Completion Page Tab ID:', error); + throw error; + } + } + + public async getAndClearCompletionPageTabId(): Promise { + try { + const result = await chrome.storage.local.get(COMPLETION_PAGE_TAB_ID_KEY); + const tabId = result[COMPLETION_PAGE_TAB_ID_KEY]; + + console.log('[DEBUG][CompletionHandler] Completion Page Tab Id Retrieved: ', tabId) + + + await chrome.storage.local.remove(COMPLETION_PAGE_TAB_ID_KEY); + + return tabId; + } catch (error) { + console.error('Error getting Completion Page Tab ID:', error); + throw error; + } + } + private clearNotifications(): void { // Clear any existing pomodoro completion notifications chrome.notifications.clear('pomodoro-complete');