Skip to content

Commit f23152e

Browse files
committed
chrome.windows are not working
1 parent 061ed4d commit f23152e

File tree

2 files changed

+87
-67
lines changed

2 files changed

+87
-67
lines changed

chrome-extension/src/background/tabs.ts

Lines changed: 47 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* eslint-disable @typescript-eslint/no-unused-vars */
22
import { ws } from '.';
3+
import { windowsAll } from './windows';
34

45
const getActiveTabForFocusedWindow = async (): Promise<chrome.tabs.Tab | null> => {
56
try {
@@ -26,37 +27,41 @@ const getAllTabs = async (): Promise<chrome.tabs.Tab[]> => {
2627
};
2728

2829
export const all = () => {
29-
getActiveTabForFocusedWindow().then(tab => {
30-
if (tab == null) {
31-
return;
32-
}
33-
const id = tab.id;
34-
const url = tab.url;
35-
const title = tab.title;
36-
const favIconUrl = tab.favIconUrl;
37-
const windowId = tab.windowId;
38-
const lastAccessed = Date.now() ?? tab.lastAccessed;
39-
const data = {
40-
logic: 'tab_actived',
41-
tab: { id, url, title, favIconUrl, windowId, lastAccessed },
42-
};
43-
ws?.send(JSON.stringify(data));
44-
});
45-
getAllTabs().then(tabs => {
46-
const data = {
47-
logic: 'tabs_all',
48-
tabs: tabs.map(tab => {
49-
const id = tab.id;
50-
const url = tab.url;
51-
const title = tab.title;
52-
const favIconUrl = tab.favIconUrl;
53-
const windowId = tab.windowId;
54-
const lastAccessed = tab.lastAccessed;
55-
return { id, url, title, favIconUrl, windowId, lastAccessed };
56-
}),
57-
};
58-
ws?.send(JSON.stringify(data));
59-
});
30+
try {
31+
getActiveTabForFocusedWindow().then(tab => {
32+
if (tab == null) {
33+
return;
34+
}
35+
const id = tab.id;
36+
const url = tab.url;
37+
const title = tab.title;
38+
const favIconUrl = tab.favIconUrl;
39+
const windowId = tab.windowId;
40+
const lastAccessed = Date.now() ?? tab.lastAccessed;
41+
const data = {
42+
logic: 'tab_actived',
43+
tab: { id, url, title, favIconUrl, windowId, lastAccessed },
44+
};
45+
ws?.send(JSON.stringify(data));
46+
});
47+
getAllTabs().then(tabs => {
48+
const data = {
49+
logic: 'tabs_all',
50+
tabs: tabs.map(tab => {
51+
const id = tab.id;
52+
const url = tab.url;
53+
const title = tab.title;
54+
const favIconUrl = tab.favIconUrl;
55+
const windowId = tab.windowId;
56+
const lastAccessed = tab.lastAccessed;
57+
return { id, url, title, favIconUrl, windowId, lastAccessed };
58+
}),
59+
};
60+
ws?.send(JSON.stringify(data));
61+
});
62+
} catch (error) {
63+
console.error('Error getting all tabs:', error);
64+
}
6065
};
6166

6267
export const tabsAll = async (tabs: chrome.tabs.Tab[]) => {
@@ -77,46 +82,57 @@ export const tabsAll = async (tabs: chrome.tabs.Tab[]) => {
7782

7883
const _onHighlighted = (activeInfo: chrome.tabs.TabHighlightInfo) => {
7984
all();
85+
windowsAll();
8086
};
8187

8288
const _onRemoved = (tabId: number, removeInfo: chrome.tabs.TabRemoveInfo) => {
8389
all();
90+
windowsAll();
8491
};
8592

8693
const _onUpdated = (tabId: number, changeInfo: chrome.tabs.TabChangeInfo, tab: chrome.tabs.Tab) => {
8794
all();
95+
windowsAll();
8896
};
8997

9098
const _onAttached = (tabId: number, attachInfo: chrome.tabs.TabAttachInfo) => {
9199
all();
100+
windowsAll();
92101
};
93102

94103
const _onMoved = (tabId: number, moveInfo: chrome.tabs.TabMoveInfo) => {
95104
all();
105+
windowsAll();
96106
};
97107

98108
const _onDetached = (tabId: number, detachInfo: chrome.tabs.TabDetachInfo) => {
99109
all();
110+
windowsAll();
100111
};
101112

102113
const _onCreated = (tab: chrome.tabs.Tab) => {
103114
all();
115+
windowsAll();
104116
};
105117

106118
const _onActivated = (activeInfo: chrome.tabs.TabActiveInfo) => {
107119
all();
120+
windowsAll();
108121
};
109122

110123
const _onReplaced = (addedTabId: number, removedTabId: number) => {
111124
all();
125+
windowsAll();
112126
};
113127

114128
const _onZoomChange = (zoomChangeInfo: chrome.tabs.ZoomChangeInfo) => {
115129
all();
130+
windowsAll();
116131
};
117132

118133
const _onFocusChanged = (windowId: number) => {
119134
all();
135+
windowsAll();
120136
};
121137

122138
const stopListenTabs = () => {

chrome-extension/src/background/windows.ts

Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -20,42 +20,46 @@ const getAllWindows = async (): Promise<chrome.windows.Window[]> => {
2020
};
2121

2222
export const windowsAll = () => {
23-
getActiveWindow().then(window => {
24-
const id = window?.id;
25-
const left = window?.left;
26-
const top = window?.top;
27-
const width = window?.width;
28-
const height = window?.height;
29-
const state = window?.state;
30-
const type = window?.type;
31-
const focused = window?.focused;
32-
const tabs = window?.tabs;
33-
tabsAll(tabs ?? []);
34-
const data = {
35-
logic: 'window_actived',
36-
window: { id, left, top, width, height, state, type, focused },
37-
};
38-
ws?.send(JSON.stringify(data));
39-
});
40-
getAllWindows().then(windows => {
41-
const data = {
42-
logic: 'windows_all',
43-
windows: windows.map(window => {
44-
const id = window.id;
45-
const left = window.left;
46-
const top = window.top;
47-
const width = window.width;
48-
const height = window.height;
49-
const state = window.state;
50-
const type = window.type;
51-
const focused = window.focused;
52-
const tabs = window.tabs;
53-
tabsAll(tabs ?? []);
54-
return { id, left, top, width, height, state, type, focused, tabs };
55-
}),
56-
};
57-
ws?.send(JSON.stringify(data));
58-
});
23+
try {
24+
getActiveWindow().then(window => {
25+
const id = window?.id;
26+
const left = window?.left;
27+
const top = window?.top;
28+
const width = window?.width;
29+
const height = window?.height;
30+
const state = window?.state;
31+
const type = window?.type;
32+
const focused = window?.focused;
33+
const tabs = window?.tabs;
34+
tabsAll(tabs ?? []);
35+
const data = {
36+
logic: 'window_actived',
37+
window: { id, left, top, width, height, state, type, focused },
38+
};
39+
ws?.send(JSON.stringify(data));
40+
});
41+
getAllWindows().then(windows => {
42+
const data = {
43+
logic: 'windows_all',
44+
windows: windows.map(window => {
45+
const id = window.id;
46+
const left = window.left;
47+
const top = window.top;
48+
const width = window.width;
49+
const height = window.height;
50+
const state = window.state;
51+
const type = window.type;
52+
const focused = window.focused;
53+
const tabs = window.tabs;
54+
tabsAll(tabs ?? []);
55+
return { id, left, top, width, height, state, type, focused, tabs };
56+
}),
57+
};
58+
ws?.send(JSON.stringify(data));
59+
});
60+
} catch (error) {
61+
console.error('Error getting all windows:', error);
62+
}
5963
};
6064

6165
const _onCreated = (window: chrome.windows.Window) => {

0 commit comments

Comments
 (0)