1
This commit is contained in:
@@ -61,7 +61,7 @@ export const StartScanningCard = () => {
|
||||
</button>
|
||||
),
|
||||
}
|
||||
}, [extension.isFetching])
|
||||
}, [extension.isFetching, extension.isInstalled])
|
||||
|
||||
/**
|
||||
* 开始爬取
|
||||
|
||||
@@ -17,7 +17,6 @@ const Page = () => {
|
||||
<SubscriptionTip/>
|
||||
|
||||
<div className={'space-y-4'}>
|
||||
{/*爬取*/}
|
||||
<StartScanningCard/>
|
||||
{/*检查Telegram连接*/}
|
||||
<ConnectTelegramCard/>
|
||||
|
||||
@@ -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 (
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
// 3. 收到正确回复
|
||||
if (response && response.success) {
|
||||
clearInterval(timer);
|
||||
useExtensionStore.getState().setInstalled(true);
|
||||
console.log("Extension detected via ID!");
|
||||
}
|
||||
}
|
||||
);
|
||||
}, 500); // 2秒轮询一次
|
||||
};
|
||||
@@ -3,7 +3,7 @@
|
||||
*/
|
||||
export const STORE_SEND_EVENTS = {
|
||||
// 查询扩展是否安装
|
||||
PING: "STORE_AI_EVT_WEB_PING",
|
||||
PING: "STORE_AI_PING",
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user