From 1cdc1cc6b92bcde0ff677caa2cf0dc65c4ebf09e Mon Sep 17 00:00:00 2001 From: Kamikaze Date: Thu, 13 Feb 2025 12:59:05 +0100 Subject: [PATCH] fix: Unexpected windowId behaviour (#218) --- src/background/handlers.ts | 19 ++++++++-------- src/background/util.ts | 44 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 10 deletions(-) diff --git a/src/background/handlers.ts b/src/background/handlers.ts index 68b24d62..3535f8ad 100644 --- a/src/background/handlers.ts +++ b/src/background/handlers.ts @@ -1,9 +1,8 @@ +import { checkWindowId, findFolder, getCustomDirectoryId } from '~/background/util.ts'; import { exchangeBars, install } from '~/background/service.ts'; -import { findFolder, getCustomDirectoryId } from '~/background/util.ts'; import { getActiveBar, getLastWorkspaceId, updateLastWorkspaceId } from '~/background/storage.ts'; const SHORTCUT_DELAY = 100; -let MAIN_WINDOW_ID: number | undefined; /** * Handle changes to bookmarks. @@ -58,10 +57,11 @@ export const handleRemove = async (id: string, removeInfo: { node: { title: stri * @param _info - Info about the activated tab. */ export const handleWorkspaceSwitch = async (_info: chrome.tabs.TabActiveInfo) => { - if (MAIN_WINDOW_ID !== _info.windowId) { - console.log('Tab is not in mainWindow. Do not chaning Bar.', MAIN_WINDOW_ID, _info.windowId); + const validWindow = await checkWindowId(_info.windowId); + if (!validWindow) { return; } + const lastWorkspaceId = await getLastWorkspaceId(); const currentBar = await getActiveBar(); const lastActiveBar = await getActiveBar(lastWorkspaceId); @@ -75,13 +75,12 @@ export const handleWorkspaceSwitch = async (_info: chrome.tabs.TabActiveInfo) => * * @param window - The newly created window object. */ -export const handleWindowCreate = (window: chrome.windows.Window) => { - console.log('MAIN_WINDOW_ID', MAIN_WINDOW_ID, 'currentWindowId', window.id); - if (window.id && MAIN_WINDOW_ID === undefined && window.type === 'normal') { - MAIN_WINDOW_ID = window.id; - console.log('Setting mainWindowId:', MAIN_WINDOW_ID); +export const handleWindowCreate = async (window: chrome.windows.Window) => { + if (window.id && window.type === 'normal') { + console.log('New Window created:', window.id); + await checkWindowId(window.id); } else { - console.log('Main Window is already set.'); + console.warn('Window is not normal:', window); } }; diff --git a/src/background/util.ts b/src/background/util.ts index 5c3606da..c778ad2e 100644 --- a/src/background/util.ts +++ b/src/background/util.ts @@ -3,6 +3,7 @@ import { type BookmarksBar } from 'bookmarks'; const CUSTOM_DIRECTORY = 'Bookmark Bars'; const CHROME_OTHER_BOOKMARKS_INDEX = 1; const OPERA_OTHER_BOOKMARKS_INDEX = 7; +let MAIN_WINDOW_ID: number | undefined; /** * Find a bookmarks folder by id. @@ -93,3 +94,46 @@ export async function getCustomBars() { export function isOperaBrowser() { return navigator.userAgent.includes(' OPR/'); } + +export async function checkWindowId(windowId: number): Promise { + if (!windowId) { + console.error('Window ID is not valid:', windowId); + return false; + } + try { + // If MAIN_WINDOW_ID is undefined, set it and return true + if (MAIN_WINDOW_ID === undefined) { + console.log('Setting MAIN_WINDOW_ID to', windowId); + setMainWindowId(windowId); + return true; + } + + // Attempt to fetch the current window + const currentWindow = await chrome.windows.get(MAIN_WINDOW_ID); + + // If fetched window doesn't have valid `id`, reset MAIN_WINDOW_ID + if (!currentWindow.id) { + console.log('MAIN_WINDOW_ID is not valid. Setting MAIN_WINDOW_ID to', windowId); + setMainWindowId(windowId); + return true; + } + + if (MAIN_WINDOW_ID !== windowId) { + console.log('Tab is not in mainWindow. Do not chaning Bar.', MAIN_WINDOW_ID, windowId); + return false; + } + + return true; + } catch (err) { + console.error('Error fetching or checking window:', err); + + // Reset MAIN_WINDOW_ID in error cases + console.log('Assigning a new MAIN_WINDOW_ID due to error:', windowId); + setMainWindowId(windowId); + return true; + } +} + +function setMainWindowId(windowId: number) { + MAIN_WINDOW_ID = windowId; +}