Skip to content

Commit eb00af7

Browse files
committed
Enhance tab and window data handling in Chrome extension
- Updated the `getActiveTabForFocusedWindow` function to exclude pinned tabs for more accurate active tab retrieval. - Added a new `tabsAll` function to send detailed information about all tabs in the current window. - Integrated `tabsAll` function into the window data structure to provide comprehensive tab context. - Adjusted tab data extraction to ensure proper handling of null values and default last accessed time.
1 parent 0ddf33c commit eb00af7

File tree

4 files changed

+39
-11
lines changed

4 files changed

+39
-11
lines changed

chrome-extension/src/background/tabs.ts

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@ import { ws } from '.';
33

44
const getActiveTabForFocusedWindow = async (): Promise<chrome.tabs.Tab | null> => {
55
try {
6-
const [activeTab] = await chrome.tabs.query({ active: true, currentWindow: true, lastFocusedWindow: true });
6+
const [activeTab] = await chrome.tabs.query({
7+
active: true,
8+
currentWindow: true,
9+
lastFocusedWindow: true,
10+
pinned: false,
11+
});
712
return activeTab;
813
} catch (error) {
914
console.error('Error getting active tab:', error);
@@ -22,12 +27,16 @@ const getAllTabs = async (): Promise<chrome.tabs.Tab[]> => {
2227

2328
const all = () => {
2429
getActiveTabForFocusedWindow().then(tab => {
25-
const id = tab?.id;
26-
const url = tab?.url;
27-
const title = tab?.title;
28-
const favIconUrl = tab?.favIconUrl;
29-
const windowId = tab?.windowId;
30-
const lastAccessed = tab?.lastAccessed;
30+
if (tab == null) {
31+
console.log('getActiveTabForFocusedWindow', tab);
32+
return;
33+
}
34+
const id = tab.id;
35+
const url = tab.url;
36+
const title = tab.title;
37+
const favIconUrl = tab.favIconUrl;
38+
const windowId = tab.windowId;
39+
const lastAccessed = Date.now() ?? tab.lastAccessed;
3140
const data = {
3241
logic: 'tab_actived',
3342
tab: { id, url, title, favIconUrl, windowId, lastAccessed },
@@ -51,6 +60,22 @@ const all = () => {
5160
});
5261
};
5362

63+
export const tabsAll = async (tabs: chrome.tabs.Tab[]) => {
64+
const data = {
65+
logic: 'tabs_all',
66+
tabs: tabs.map(tab => {
67+
const id = tab.id;
68+
const url = tab.url;
69+
const title = tab.title;
70+
const favIconUrl = tab.favIconUrl;
71+
const windowId = tab.windowId;
72+
const lastAccessed = tab.lastAccessed;
73+
return { id, url, title, favIconUrl, windowId, lastAccessed };
74+
}),
75+
};
76+
ws?.send(JSON.stringify(data));
77+
};
78+
5479
const _onHighlighted = (activeInfo: chrome.tabs.TabHighlightInfo) => {
5580
all();
5681
};

chrome-extension/src/background/windows.ts

Lines changed: 3 additions & 0 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 { tabsAll } from './tabs';
34

45
const getActiveWindow = async (): Promise<chrome.windows.Window | null> => {
56
try {
@@ -30,6 +31,7 @@ const all = () => {
3031
const type = window?.type;
3132
const focused = window?.focused;
3233
const tabs = window?.tabs;
34+
tabsAll(tabs ?? []);
3335
const data = {
3436
logic: 'window_actived',
3537
window: { id, left, top, width, height, state, type, focused },
@@ -49,6 +51,7 @@ const all = () => {
4951
const type = window.type;
5052
const focused = window.focused;
5153
const tabs = window.tabs;
54+
tabsAll(tabs ?? []);
5255
return { id, left, top, width, height, state, type, focused, tabs };
5356
}),
5457
};

pages/content-ui/src/DashboardEntry.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ export const DashboardEntry: React.FC<{
7171
onMouseLeave={() => setIsHovered(false)}>
7272
{icon}
7373
<div style={{ display: 'flex', flexDirection: 'column', marginLeft: '4px' }}>
74-
<div style={{ fontSize: '0.9rem', fontWeight: 600, lineHeight: '1.3' }}>RWKV</div>
75-
<div style={{ fontSize: '0.9rem', fontWeight: 600, lineHeight: '1.3' }}>离线翻译</div>
74+
<div style={{ fontSize: '12px' }}>RWKV</div>
75+
<div style={{ fontSize: '14px', fontWeight: 600 }}>离线翻译</div>
7676
</div>
7777
</div>
7878
);

pages/content-ui/src/components/Base.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ export const Base: React.FC<{
7171
{icon}
7272
</div>
7373
<div style={{ display: 'flex', flexDirection: 'column' }}>
74-
<div style={{ fontSize: '0.9rem', fontWeight: 600, lineHeight: '1.3' }}>{title}</div>
75-
<div style={{ fontSize: '0.8rem', opacity: 0.7 }}>{value}</div>
74+
<div style={{ fontSize: '12px' }}>{title}</div>
75+
<div style={{ fontSize: '14px', fontWeight: 600 }}>{value}</div>
7676
</div>
7777
</div>
7878
);

0 commit comments

Comments
 (0)