登陆信息同步和接口联调

This commit is contained in:
2026-05-14 10:47:51 +08:00
parent 84a2a3993a
commit c86c570896
8 changed files with 272 additions and 87 deletions
+38 -2
View File
@@ -6,8 +6,11 @@
import styleText from "data-text:~components/SidebarPanel.scss"
import type { PlasmoCSConfig, PlasmoGetStyle } from "plasmo"
import { useEffect, useState } from "react"
import { useEffect, useState, useRef } from "react"
import { SidebarPanel } from "~components/SidebarPanel"
import { getCookieValue } from "~utils/cookie"
import { findJobBySourceUrl } from "~api/dataApi"
import type { JobInfo } from "~lib/types"
/** Content Script 配置:匹配所有网页 */
export const config: PlasmoCSConfig = {
@@ -31,8 +34,38 @@ export const getStyle: PlasmoGetStyle = () => {
function Sidebar() {
/** 侧边栏是否可见 */
const [visible, setVisible] = useState(false)
/** 当前标签页的来源链接 */
const [sourceUrl, setSourceUrl] = useState("")
/** 岗位信息(在 sidebar 层查询,传给 SidebarPanel */
const [jobInfo, setJobInfo] = useState<JobInfo | null>(null)
/** 是否已自动打开过面板(避免重复打开) */
const autoOpenedRef = useRef(false)
useEffect(() => {
// 页面打开时获取 Token 和当前页面 URL 作为 sourceUrl
const currentUrl = window.location.href
setSourceUrl(currentUrl)
console.log("[OfferPie] 当前页面 sourceUrl:", currentUrl)
getCookieValue("Token").then((token) => {
console.log("[OfferPie] 页面加载时获取到的 Token:", token)
if (!token) return
// 有 token 时,先查询岗位信息,有岗位才自动打开面板
findJobBySourceUrl(currentUrl).then((data) => {
console.log("[OfferPie] 岗位信息:", data)
if (data && data.id) {
setJobInfo(data)
if (!autoOpenedRef.current) {
autoOpenedRef.current = true
setVisible(true)
}
}
}).catch((err) => {
console.warn("[OfferPie] 查询岗位信息失败:", err)
})
})
/**
* 消息监听器:接收来自 background 或 popup 的消息
* 当收到 TOGGLE_SIDEBAR 消息时,切换侧边栏的显示状态
@@ -40,6 +73,9 @@ function Sidebar() {
const handler = (message: any) => {
if (message.type === "TOGGLE_SIDEBAR") {
setVisible((prev) => !prev)
getCookieValue("Token").then((token) => {
console.log("[OfferPie] 点击插件按钮时获取到的 Token:", token)
})
}
}
chrome.runtime.onMessage.addListener(handler)
@@ -50,7 +86,7 @@ function Sidebar() {
// 不可见时不渲染任何内容
if (!visible) return null
return <SidebarPanel onClose={() => setVisible(false)} />
return <SidebarPanel sourceUrl={sourceUrl} jobInfo={jobInfo} onClose={() => setVisible(false)} />
}
export default Sidebar