Skip to content

Commit 70acd5e

Browse files
committed
fix: using background js to open popup link #11
Signed-off-by: EINDEX <snowstarlbk@gmail.com>
1 parent 5b01f36 commit 70acd5e

File tree

5 files changed

+26
-14
lines changed

5 files changed

+26
-14
lines changed

src/components/LogseqBlock.tsx

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,7 @@ type LogseqBlockProps = {
1212
export const LogseqBlock = ({
1313
graph,
1414
block,
15-
isPopUp = false,
1615
}: LogseqBlockProps) => {
17-
const click = (e: any) => {
18-
if (isPopUp) setTimeout(window.close, 10);
19-
};
20-
2116
if (block.html) {
2217
return (
2318
<div className={`${styles.block}`}>
@@ -29,13 +24,11 @@ export const LogseqBlock = ({
2924
<a
3025
className={styles.toBlock}
3126
href={`logseq://graph/${graph}?block-id=${block.uuid}`}
32-
onClick={click}
3327
>
3428
<span className={'tie tie-block'}></span>
3529
To block
3630
</a>
3731
<LogseqPageLink
38-
isPopUp={isPopUp}
3932
graph={graph}
4033
page={block.page}
4134
></LogseqPageLink>

src/components/LogseqPage.tsx

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,7 @@ type LogseqPageLinkProps = {
1111
const LogseqPageLink = ({
1212
page,
1313
graph,
14-
isPopUp = false,
1514
}: LogseqPageLinkProps) => {
16-
const click = (e: any) => {
17-
if (isPopUp) setTimeout(window.close, 10);
18-
};
1915

2016
if (page === undefined || page?.name === undefined) {
2117
return <></>;
@@ -25,7 +21,6 @@ const LogseqPageLink = ({
2521
<a
2622
className={styles.logseqPageLink}
2723
href={`logseq://graph/${graph}?page=${page?.name}`}
28-
onClick={click}
2924
>
3025
<span className="tie tie-page"></span>
3126
{page?.name}

src/pages/background/index.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import Browser from 'webextension-polyfill';
33
import { getLogseqCopliotConfig } from '../../config';
44
import { removeUrlHash } from '@/utils';
55
import { setExtensionBadge } from './utils';
6-
import { debounce } from 'lodash';
6+
import { debounce, delay } from 'lodash';
77

88
const logseqClient = new LogseqClient();
99

@@ -20,12 +20,24 @@ Browser.runtime.onConnect.addListener((port) => {
2020
Browser.runtime.openOptionsPage();
2121
} else if (msg.type === 'quick-capture') {
2222
quickCapture(msg.data);
23+
} else if (msg.type === 'open-page') {
24+
openPage(msg.url);
2325
} else {
2426
console.debug(msg);
2527
}
2628
});
2729
});
2830

31+
const openPage = async (url: string) => {
32+
await delay(() => {}, 50); // delay 50 to back the active tab.
33+
34+
const tab = await Browser.tabs.query({ active: true, currentWindow: true });
35+
if (!tab) return;
36+
const activeTab = tab[0];
37+
if (activeTab.url !== url)
38+
await Browser.tabs.update(activeTab.id, { url: url });
39+
};
40+
2941
const quickCapture = async (data: string) => {
3042
const tab = await Browser.tabs.query({ active: true, currentWindow: true });
3143
if (!tab) return;

src/pages/popup/Popup.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import { LogseqBlock } from '@components/LogseqBlock';
1010

1111
import styles from './index.module.scss';
1212

13+
const connect = Browser.runtime.connect();
14+
1315
const client = new LogseqClient();
1416

1517
export default function Popup() {
@@ -30,6 +32,17 @@ export default function Popup() {
3032
const result = await client.blockSearch(url);
3133
if (result.status !== 200) return;
3234
setLogseqSearchResult(result.response!);
35+
setTimeout(() => {
36+
document.querySelectorAll('a').forEach((e) => {
37+
e.onclick = () => {
38+
connect.postMessage({
39+
type: 'open-page',
40+
url: e.href,
41+
});
42+
window.close();
43+
};
44+
});
45+
}, 100);
3346
});
3447
});
3548

src/pages/popup/index.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
<meta name="viewport" content="width=device-width, initial-scale=1" />
66
<title>Popups</title>
77
<link rel="stylesheet" href="popup.css">
8-
98
</head>
109

1110
<body>

0 commit comments

Comments
 (0)