This commit is contained in:
zhu
2026-05-09 17:24:41 +08:00
parent fd9cf1a52f
commit 37c22705cf
5 changed files with 34 additions and 19 deletions

View File

@@ -61,7 +61,7 @@ export const StartScanningCard = () => {
</button> </button>
), ),
} }
}, [extension.isFetching]) }, [extension.isFetching, extension.isInstalled])
/** /**
* 开始爬取 * 开始爬取

View File

@@ -17,7 +17,6 @@ const Page = () => {
<SubscriptionTip/> <SubscriptionTip/>
<div className={'space-y-4'}> <div className={'space-y-4'}>
{/*爬取*/}
<StartScanningCard/> <StartScanningCard/>
{/*检查Telegram连接*/} {/*检查Telegram连接*/}
<ConnectTelegramCard/> <ConnectTelegramCard/>

View File

@@ -3,16 +3,17 @@
import useSubscribeStore from "@/store/subscribe"; import useSubscribeStore from "@/store/subscribe";
import Header from "./_components/header"; import Header from "./_components/header";
import {useEffect} from "react"; import {useEffect} from "react";
import {detectExtension} from "@/utils/extension/detect_extension";
interface Props { interface Props {
children: React.ReactNode; children: React.ReactNode;
} }
export default function DashboardLayout({children}: Props) { export default function DashboardLayout({children}: Props) {
const {init} = useSubscribeStore(); const {init} = useSubscribeStore();
useEffect(() => { useEffect(() => {
init(); init();
detectExtension()
}, []); }, []);
return ( return (

View File

@@ -4,22 +4,37 @@ import useExtensionStore from "@/store/extension";
/** /**
* 检擦扩展是否安装 * 检擦扩展是否安装
*/ */
// 你的固定扩展 ID
const EXTENSION_ID = "bhnpckgpcfnoiphhknaakhfieihpocan";
declare const chrome: any;
export const detectExtension = () => { export const detectExtension = () => {
let timer: any = null; // 如果已经安装了,就不跑了
if (useExtensionStore.getState().isInstalled) return;
// 定义响应处理器 const timer = setInterval(() => {
const handleResponse = (event: any) => { // 1. 检查浏览器是否支持 chrome 插件环境
// 移除监听,释放内存 if (typeof chrome === 'undefined' || !chrome.runtime?.sendMessage) {
window.removeEventListener(STORE_SEND_EVENTS.PING, handleResponse); return;
clearTimeout(timer);
useExtensionStore.getState().setInstalled(true)
};
// 1. 开始监听扩展的回传信号
window.addEventListener(STORE_REPLY_EVENTS.PONG, handleResponse);
timer = setInterval(() => {
// 持续发送 PING 指令
window.dispatchEvent(new CustomEvent(STORE_SEND_EVENTS.PING));
}, 2000);
} }
// 2. 尝试给特定 ID 的扩展发送 PING
chrome.runtime.sendMessage(
EXTENSION_ID,
{type: STORE_SEND_EVENTS.PING},
(response: { success: any; }) => {
// 检查是否有错误(比如扩展没装或 ID 不对)
if (chrome.runtime.lastError) {
// 这里静默失败,继续轮询
return;
}
// 3. 收到正确回复
if (response && response.success) {
clearInterval(timer);
useExtensionStore.getState().setInstalled(true);
console.log("Extension detected via ID!");
}
}
);
}, 500); // 2秒轮询一次
};

View File

@@ -3,7 +3,7 @@
*/ */
export const STORE_SEND_EVENTS = { export const STORE_SEND_EVENTS = {
// 查询扩展是否安装 // 查询扩展是否安装
PING: "STORE_AI_EVT_WEB_PING", PING: "STORE_AI_PING",
}; };
/** /**