Skip to content

Commit 812b45d

Browse files
committed
feat: integrate @webext-core/messaging for improved message handling in background and content scripts
Signed-off-by: EINDEX <snowstarlbk@gmail.com>
1 parent 117b96e commit 812b45d

9 files changed

Lines changed: 266 additions & 90 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"@radix-ui/react-switch": "^1.2.5",
3838
"@radix-ui/react-tooltip": "^1.2.7",
3939
"@tabler/icons-react": "^2.47.0",
40+
"@webext-core/messaging": "^2.3.0",
4041
"class-variance-authority": "^0.7.1",
4142
"clsx": "^2.1.1",
4243
"date-fns": "^2.30.0",

pnpm-lock.yaml

Lines changed: 45 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/entrypoints/background/index.ts

Lines changed: 69 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { changeOptionsHostToHostNameAndPort } from './upgrade';
66
import { getLogseqService } from './logseq/tool';
77
import { SearchEngineConfigMigration } from '@/utils/migration';
88
import { templates } from '@/utils/storage';
9+
import { onMessage, sendMessage } from '@/types/messaging';
910

1011
export default defineBackground({
1112
// Set manifest options
@@ -19,49 +20,76 @@ export default defineBackground({
1920
main() {
2021
// Executed when background is loaded, CANNOT BE ASYNC
2122

22-
browser.runtime.onConnect.addListener((port) => {
23-
port.onMessage.addListener((msg) => {
24-
if (msg.type === 'query') {
25-
const promise = new Promise(async () => {
26-
const logseqService = await getLogseqService();
27-
const searchRes = await logseqService.search(msg.query);
28-
console.debug('search result', searchRes);
29-
port.postMessage(searchRes);
30-
});
31-
32-
promise.catch((err) => console.error(err));
33-
} else if (msg.type === 'open-options') {
34-
browser.runtime.openOptionsPage();
35-
} else {
36-
console.debug(msg);
37-
}
38-
});
23+
// Register message handlers using @webext-core/messaging
24+
onMessage('logseq:search', async ({ data }) => {
25+
const logseqService = await getLogseqService();
26+
const searchRes = await logseqService.search(data);
27+
console.debug('search result', searchRes);
28+
return searchRes;
3929
});
4030

41-
browser.runtime.onMessage.addListener((msg, sender) => {
42-
if (msg.type === 'open-options') {
43-
browser.runtime.openOptionsPage();
44-
} else if (msg.type === 'clip-with-selection') {
45-
quickCapture(msg.data);
46-
} else if (msg.type === 'clip-page') {
47-
quickCapture('');
48-
} else if (msg.type === 'open-page') {
49-
openPage(msg.url);
50-
} else if (msg.type === 'change-block-marker') {
51-
changeBlockMarker(msg.uuid, msg.marker);
52-
} else {
53-
console.debug(msg);
54-
}
31+
onMessage('logseq:urlSearch', async ({ data }) => {
32+
const logseqService = await getLogseqService();
33+
const url = new URL(data.url);
34+
const searchRes = await logseqService.urlSearch(url, data.options);
35+
return searchRes;
36+
});
37+
38+
onMessage('app:openOptions', async () => {
39+
browser.runtime.openOptionsPage();
40+
});
41+
42+
onMessage('logseq:clipWithSelection', async ({ data }) => {
43+
await quickCapture(data);
44+
});
45+
46+
onMessage('logseq:clipPage', async () => {
47+
await quickCapture('');
48+
});
49+
50+
onMessage('app:openPage', async ({ data }) => {
51+
await openPage(data.url);
52+
});
53+
54+
onMessage('logseq:changeBlockMarker', async ({ data }) => {
55+
const result = await changeBlockMarker(data.uuid, data.marker);
56+
return result;
5557
});
5658

5759
const changeBlockMarker = async (uuid: string, marker: string) => {
5860
const tab = await getCurrentTab();
5961
if (!tab) {
60-
return;
62+
return {
63+
type: 'change-block-marker-result',
64+
uuid,
65+
status: 'error',
66+
marker,
67+
msg: 'No active tab found',
68+
};
6169
}
6270
const logseqService = await getLogseqService();
6371
const result = await logseqService.changeBlockMarker(uuid, marker);
64-
browser.tabs.sendMessage(tab.id!, result);
72+
73+
// Send result to content script
74+
await sendMessage(
75+
'content:blockMarkerChanged',
76+
{
77+
type: 'change-block-marker-result',
78+
uuid,
79+
status: 'success',
80+
marker,
81+
msg: result.msg,
82+
},
83+
{ tabId: tab.id! },
84+
);
85+
86+
return {
87+
type: 'change-block-marker-result',
88+
uuid,
89+
status: 'success',
90+
marker,
91+
msg: result.msg,
92+
};
6593
};
6694

6795
const getCurrentTab = async () => {
@@ -180,7 +208,13 @@ export default defineBackground({
180208
}
181209

182210
browser.contextMenus.onClicked.addListener((info, tab) => {
183-
browser.tabs.sendMessage(tab!.id!, { type: info.menuItemId }, {});
211+
if (info.menuItemId === 'clip-with-selection') {
212+
sendMessage('content:quickCaptureWithSelection', undefined, {
213+
tabId: tab!.id!,
214+
});
215+
} else if (info.menuItemId === 'clip-page') {
216+
sendMessage('content:quickCapturePage', undefined, { tabId: tab!.id! });
217+
}
184218
});
185219

186220
browser.runtime.onInstalled.addListener(async (event) => {
@@ -198,7 +232,7 @@ export default defineBackground({
198232

199233
browser.commands.onCommand.addListener((command, tab) => {
200234
if (command === 'clip' && tab !== undefined) {
201-
browser.tabs.sendMessage(tab.id!, { type: 'clip' });
235+
sendMessage('content:quickCapture', undefined, { tabId: tab.id! });
202236
}
203237
});
204238

src/entrypoints/content/LogseqCopliot.tsx

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ import React from 'react';
22
import { LogseqSearchResult } from '@/types/logseqBlock';
33
import { LogseqResponseType } from '@/entrypoints/background/logseq/client';
44
import LogseqCopilot from '@/components/LogseqCopilot';
5-
import { browser, type Browser } from 'wxt/browser';
5+
import { browser } from 'wxt/browser';
66
import { Button } from '@/components/ui/button';
77
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
88
import { Badge } from '@/components/ui/badge';
99
import { Skeleton } from '@/components/ui/skeleton';
1010
import { IconSettings, IconAlertCircle, IconExternalLink } from '@tabler/icons-react';
1111
import { ThemeProvider } from '@/components/ThemeProvider';
12+
import { sendMessage, onMessage } from '@/types/messaging';
1213
import '@/assets/globals.css';
1314

1415
// Import theme test function for debugging
@@ -20,41 +21,45 @@ if (import.meta.env.DEV) {
2021
}
2122

2223
type LogseqCopliotProps = {
23-
connect: Browser.runtime.Port;
24+
query: string;
2425
};
2526

2627
type LoadingState = 'loading' | 'success' | 'error' | 'disconnected';
2728

28-
const LogseqCopliotContent = ({ connect }: LogseqCopliotProps) => {
29+
const LogseqCopliotContent = ({ query }: LogseqCopliotProps) => {
2930
const [state, setState] = React.useState<LoadingState>('loading');
3031
const [errorMessage, setErrorMessage] = React.useState<string>('');
3132
const [logseqSearchResult, setLogseqSearchResult] = React.useState<LogseqSearchResult>();
3233

3334
React.useEffect(() => {
34-
const handleMessage = (resp: LogseqResponseType<LogseqSearchResult>) => {
35-
if (resp.msg === 'success') {
36-
setState('success');
37-
setLogseqSearchResult(resp.response);
38-
setErrorMessage('');
39-
} else if (resp.msg === 'Loading...') {
40-
setState('loading');
41-
setErrorMessage('');
42-
} else {
35+
const performSearch = async () => {
36+
setState('loading');
37+
setErrorMessage('');
38+
39+
try {
40+
const result = await sendMessage('logseq:search', query);
41+
42+
if (result.msg === 'success') {
43+
setState('success');
44+
setLogseqSearchResult(result.response);
45+
setErrorMessage('');
46+
} else {
47+
setState('error');
48+
setErrorMessage(result.msg);
49+
setLogseqSearchResult(undefined);
50+
}
51+
} catch (err) {
4352
setState('error');
44-
setErrorMessage(resp.msg);
53+
setErrorMessage(err instanceof Error ? err.message : 'Search failed');
4554
setLogseqSearchResult(undefined);
4655
}
4756
};
4857

49-
connect.onMessage.addListener(handleMessage);
50-
51-
return () => {
52-
connect.onMessage.removeListener(handleMessage);
53-
};
54-
}, [connect]);
58+
performSearch();
59+
}, [query]);
5560

5661
const handleOpenOptions = () => {
57-
browser.runtime.sendMessage({ type: 'open-options' });
62+
sendMessage('app:openOptions');
5863
};
5964

6065
const handleOpenLogseq = () => {
@@ -235,10 +240,10 @@ const LogseqCopliotContent = ({ connect }: LogseqCopliotProps) => {
235240
);
236241
};
237242

238-
export const LogseqCopliot = ({ connect }: LogseqCopliotProps) => {
243+
export const LogseqCopliot = ({ query }: LogseqCopliotProps) => {
239244
return (
240245
<ThemeProvider>
241-
<LogseqCopliotContent connect={connect} />
246+
<LogseqCopliotContent query={query} />
242247
</ThemeProvider>
243248
);
244249
};

0 commit comments

Comments
 (0)