From 37c22705cf9432c891e35c2075bece4e073f3fe1 Mon Sep 17 00:00:00 2001
From: zhu <1812073942@qq.com>
Date: Sat, 9 May 2026 17:24:41 +0800
Subject: [PATCH] 1
---
.../(home)/_components/start-scan.tsx | 2 +-
src/app/dashboard/(home)/page.tsx | 1 -
src/app/dashboard/layout.tsx | 3 +-
src/utils/extension/detect_extension.ts | 45 ++++++++++++-------
src/utils/extension/type.ts | 2 +-
5 files changed, 34 insertions(+), 19 deletions(-)
diff --git a/src/app/dashboard/(home)/_components/start-scan.tsx b/src/app/dashboard/(home)/_components/start-scan.tsx
index 9f7fe90..596b4e1 100644
--- a/src/app/dashboard/(home)/_components/start-scan.tsx
+++ b/src/app/dashboard/(home)/_components/start-scan.tsx
@@ -61,7 +61,7 @@ export const StartScanningCard = () => {
),
}
- }, [extension.isFetching])
+ }, [extension.isFetching, extension.isInstalled])
/**
* 开始爬取
diff --git a/src/app/dashboard/(home)/page.tsx b/src/app/dashboard/(home)/page.tsx
index 36ea195..0abb5b0 100644
--- a/src/app/dashboard/(home)/page.tsx
+++ b/src/app/dashboard/(home)/page.tsx
@@ -17,7 +17,6 @@ const Page = () => {
- {/*爬取*/}
{/*检查Telegram连接*/}
diff --git a/src/app/dashboard/layout.tsx b/src/app/dashboard/layout.tsx
index e5017cc..4a361b5 100644
--- a/src/app/dashboard/layout.tsx
+++ b/src/app/dashboard/layout.tsx
@@ -3,16 +3,17 @@
import useSubscribeStore from "@/store/subscribe";
import Header from "./_components/header";
import {useEffect} from "react";
+import {detectExtension} from "@/utils/extension/detect_extension";
interface Props {
children: React.ReactNode;
}
export default function DashboardLayout({children}: Props) {
-
const {init} = useSubscribeStore();
useEffect(() => {
init();
+ detectExtension()
}, []);
return (
diff --git a/src/utils/extension/detect_extension.ts b/src/utils/extension/detect_extension.ts
index 6a78509..e46299e 100644
--- a/src/utils/extension/detect_extension.ts
+++ b/src/utils/extension/detect_extension.ts
@@ -4,22 +4,37 @@ import useExtensionStore from "@/store/extension";
/**
* 检擦扩展是否安装
*/
+// 你的固定扩展 ID
+const EXTENSION_ID = "bhnpckgpcfnoiphhknaakhfieihpocan";
+declare const chrome: any;
export const detectExtension = () => {
- let timer: any = null;
+ // 如果已经安装了,就不跑了
+ if (useExtensionStore.getState().isInstalled) return;
- // 定义响应处理器
- const handleResponse = (event: any) => {
- // 移除监听,释放内存
- window.removeEventListener(STORE_SEND_EVENTS.PING, handleResponse);
- clearTimeout(timer);
- useExtensionStore.getState().setInstalled(true)
- };
+ const timer = setInterval(() => {
+ // 1. 检查浏览器是否支持 chrome 插件环境
+ if (typeof chrome === 'undefined' || !chrome.runtime?.sendMessage) {
+ return;
+ }
- // 1. 开始监听扩展的回传信号
- window.addEventListener(STORE_REPLY_EVENTS.PONG, handleResponse);
+ // 2. 尝试给特定 ID 的扩展发送 PING
+ chrome.runtime.sendMessage(
+ EXTENSION_ID,
+ {type: STORE_SEND_EVENTS.PING},
+ (response: { success: any; }) => {
+ // 检查是否有错误(比如扩展没装或 ID 不对)
+ if (chrome.runtime.lastError) {
+ // 这里静默失败,继续轮询
+ return;
+ }
- timer = setInterval(() => {
- // 持续发送 PING 指令
- window.dispatchEvent(new CustomEvent(STORE_SEND_EVENTS.PING));
- }, 2000);
-}
\ No newline at end of file
+ // 3. 收到正确回复
+ if (response && response.success) {
+ clearInterval(timer);
+ useExtensionStore.getState().setInstalled(true);
+ console.log("Extension detected via ID!");
+ }
+ }
+ );
+ }, 500); // 2秒轮询一次
+};
\ No newline at end of file
diff --git a/src/utils/extension/type.ts b/src/utils/extension/type.ts
index f21dda4..0be23e6 100644
--- a/src/utils/extension/type.ts
+++ b/src/utils/extension/type.ts
@@ -3,7 +3,7 @@
*/
export const STORE_SEND_EVENTS = {
// 查询扩展是否安装
- PING: "STORE_AI_EVT_WEB_PING",
+ PING: "STORE_AI_PING",
};
/**