Skip to content

Commit 89ddaba

Browse files
authored
fix: Unexpected windowId behaviour (#218) (#497)
1 parent e389dc4 commit 89ddaba

File tree

2 files changed

+53
-10
lines changed

2 files changed

+53
-10
lines changed

src/background/handlers.ts

+9-10
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1+
import { checkWindowId, findFolder, getCustomDirectoryId } from '~/background/util.ts';
12
import { exchangeBars, install } from '~/background/service.ts';
2-
import { findFolder, getCustomDirectoryId } from '~/background/util.ts';
33
import { getActiveBar, getLastWorkspaceId, updateLastWorkspaceId } from '~/background/storage.ts';
44

55
const SHORTCUT_DELAY = 100;
6-
let MAIN_WINDOW_ID: number | undefined;
76

87
/**
98
* Handle changes to bookmarks.
@@ -58,10 +57,11 @@ export const handleRemove = async (id: string, removeInfo: { node: { title: stri
5857
* @param _info - Info about the activated tab.
5958
*/
6059
export const handleWorkspaceSwitch = async (_info: chrome.tabs.TabActiveInfo) => {
61-
if (MAIN_WINDOW_ID !== _info.windowId) {
62-
console.log('Tab is not in mainWindow. Do not chaning Bar.', MAIN_WINDOW_ID, _info.windowId);
60+
const validWindow = await checkWindowId(_info.windowId);
61+
if (!validWindow) {
6362
return;
6463
}
64+
6565
const lastWorkspaceId = await getLastWorkspaceId();
6666
const currentBar = await getActiveBar();
6767
const lastActiveBar = await getActiveBar(lastWorkspaceId);
@@ -75,13 +75,12 @@ export const handleWorkspaceSwitch = async (_info: chrome.tabs.TabActiveInfo) =>
7575
*
7676
* @param window - The newly created window object.
7777
*/
78-
export const handleWindowCreate = (window: chrome.windows.Window) => {
79-
console.log('MAIN_WINDOW_ID', MAIN_WINDOW_ID, 'currentWindowId', window.id);
80-
if (window.id && MAIN_WINDOW_ID === undefined && window.type === 'normal') {
81-
MAIN_WINDOW_ID = window.id;
82-
console.log('Setting mainWindowId:', MAIN_WINDOW_ID);
78+
export const handleWindowCreate = async (window: chrome.windows.Window) => {
79+
if (window.id && window.type === 'normal') {
80+
console.log('New Window created:', window.id);
81+
await checkWindowId(window.id);
8382
} else {
84-
console.log('Main Window is already set.');
83+
console.warn('Window is not normal:', window);
8584
}
8685
};
8786

src/background/util.ts

+44
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { type BookmarksBar } from 'bookmarks';
33
const CUSTOM_DIRECTORY = 'Bookmark Bars';
44
const CHROME_OTHER_BOOKMARKS_INDEX = 1;
55
const OPERA_OTHER_BOOKMARKS_INDEX = 7;
6+
let MAIN_WINDOW_ID: number | undefined;
67

78
/**
89
* Find a bookmarks folder by id.
@@ -93,3 +94,46 @@ export async function getCustomBars() {
9394
export function isOperaBrowser() {
9495
return navigator.userAgent.includes(' OPR/');
9596
}
97+
98+
export async function checkWindowId(windowId: number): Promise<boolean> {
99+
if (!windowId) {
100+
console.error('Window ID is not valid:', windowId);
101+
return false;
102+
}
103+
try {
104+
// If MAIN_WINDOW_ID is undefined, set it and return true
105+
if (MAIN_WINDOW_ID === undefined) {
106+
console.log('Setting MAIN_WINDOW_ID to', windowId);
107+
setMainWindowId(windowId);
108+
return true;
109+
}
110+
111+
// Attempt to fetch the current window
112+
const currentWindow = await chrome.windows.get(MAIN_WINDOW_ID);
113+
114+
// If fetched window doesn't have valid `id`, reset MAIN_WINDOW_ID
115+
if (!currentWindow.id) {
116+
console.log('MAIN_WINDOW_ID is not valid. Setting MAIN_WINDOW_ID to', windowId);
117+
setMainWindowId(windowId);
118+
return true;
119+
}
120+
121+
if (MAIN_WINDOW_ID !== windowId) {
122+
console.log('Tab is not in mainWindow. Do not chaning Bar.', MAIN_WINDOW_ID, windowId);
123+
return false;
124+
}
125+
126+
return true;
127+
} catch (err) {
128+
console.error('Error fetching or checking window:', err);
129+
130+
// Reset MAIN_WINDOW_ID in error cases
131+
console.log('Assigning a new MAIN_WINDOW_ID due to error:', windowId);
132+
setMainWindowId(windowId);
133+
return true;
134+
}
135+
}
136+
137+
function setMainWindowId(windowId: number) {
138+
MAIN_WINDOW_ID = windowId;
139+
}

0 commit comments

Comments
 (0)