Skip to content

Commit

Permalink
fix: Unexpected windowId behaviour (#218) (#497)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kamiikaze authored Feb 26, 2025
1 parent e389dc4 commit 89ddaba
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 10 deletions.
19 changes: 9 additions & 10 deletions src/background/handlers.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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);
Expand All @@ -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);
}
};

Expand Down
44 changes: 44 additions & 0 deletions src/background/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -93,3 +94,46 @@ export async function getCustomBars() {
export function isOperaBrowser() {
return navigator.userAgent.includes(' OPR/');
}

export async function checkWindowId(windowId: number): Promise<boolean> {
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;
}

0 comments on commit 89ddaba

Please sign in to comment.