Skip to content

Commit 196b374

Browse files
Merge pull request #24 from ut-code/feature/updatesidepanel
ページ遷移でミッションが変わらないように+判定整備
2 parents db8a654 + 81b6fa6 commit 196b374

File tree

3 files changed

+45
-13
lines changed

3 files changed

+45
-13
lines changed

public/manifest.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"manifest_version": 3,
3-
"name": "My SidePanel Extension",
3+
"name": "ブラウザハック入門ミッション",
44
"version": "1.0.0",
5-
"description": "Chrome extension with side panel (React + Vite + TypeScript)",
5+
"description": "ブラウザハック入門のミッション用拡張機能",
66
"background": {
77
"service_worker": "background.js"
88
},
@@ -12,7 +12,7 @@
1212
"side_panel": {
1313
"default_path": "src/sidepanel/index.html"
1414
},
15-
"permissions": ["sidePanel", "tabs"],
15+
"permissions": ["sidePanel", "tabs", "storage"],
1616
"host_permissions": ["<all_urls>"],
1717
"content_scripts": [
1818
{

src/content.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// content.tsx
22
export {}
33

4+
const PAGE_KEY = `${location.origin}${location.pathname}`
5+
46
function isDisplayed(el: Element | null): boolean {
57
if (!el) return false
68
if (!el.isConnected) return false
@@ -63,7 +65,11 @@ function sendMessage() {
6365
lastJson = currentJson
6466
for (const p of ports) {
6567
try {
66-
p.postMessage({ type: 'DOM_VALUE_UPDATE', values })
68+
p.postMessage({
69+
type: 'DOM_VALUE_UPDATE',
70+
pageKey: PAGE_KEY,
71+
values,
72+
})
6773
} catch {}
6874
}
6975
}
@@ -73,23 +79,26 @@ chrome.runtime.onConnect.addListener((port) => {
7379
if (port.name !== 'sidepanel') return
7480
ports.add(port)
7581

76-
// 接続直後に現在の状態を送る
7782
const initialValues = getCurrentValues()
7883
lastJson = JSON.stringify(initialValues)
79-
port.postMessage({ type: 'DOM_VALUE_UPDATE', values: initialValues })
84+
port.postMessage({
85+
type: 'DOM_VALUE_UPDATE',
86+
pageKey: PAGE_KEY,
87+
values: initialValues,
88+
})
8089

8190
port.onDisconnect.addListener(() => ports.delete(port))
8291
})
8392

8493
// DOM変更を監視し、まとめて通知
8594
const notify = debounce(sendMessage, 200)
86-
8795
const mo = new MutationObserver(() => notify())
8896
mo.observe(document.documentElement, {
8997
childList: true,
9098
subtree: true,
9199
characterData: true,
92100
attributes: true,
93101
})
102+
94103
// 定期的に送信
95104
setInterval(sendMessage, 300)

src/sidepanel/App.tsx

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,44 @@ const ANSWERS = {
1313
}
1414

1515
function App() {
16-
// 現在の問題番号
1716
const [step, setStep] = useState(1)
18-
// 判定結果
1917
const [result, setResult] = useState<CheckResult | null>(null)
2018
const [error, setError] = useState<string | null>(null)
2119

22-
// イベントリスナー内で最新の step を参照するための Ref
2320
const stepRef = useRef(step)
24-
// 各ステップの初期値を保持するベースライン
2521
const baselineRef = useRef<{ p1: string | null; p2: string | null; p3Visible: boolean | null; p4: string | null }>({
2622
p1: null,
2723
p2: null,
2824
p3Visible: null,
2925
p4: null,
3026
})
27+
const pageKeyRef = useRef<string | null>(null)
28+
29+
// 追加: ステップの永続化
30+
useEffect(() => {
31+
chrome.storage.local.get(['currentStep']).then(({ currentStep }) => {
32+
if (typeof currentStep === 'number') {
33+
setStep(currentStep)
34+
stepRef.current = currentStep
35+
}
36+
})
37+
}, [])
38+
39+
useEffect(() => {
40+
chrome.storage.local.set({ currentStep: step })
41+
}, [step])
3142

32-
// state が変わったら ref も更新しておく
43+
// イベントリスナー内で最新の step を参照するための Ref
3344
useEffect(() => {
3445
stepRef.current = step
3546
}, [step])
3647

3748
useEffect(() => {
49+
let disposed = false
3850
let port: chrome.runtime.Port | null = null
3951

4052
const connect = async () => {
53+
if (disposed) return
4154
try {
4255
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true })
4356
if (!tab?.id) return
@@ -49,6 +62,12 @@ function App() {
4962
const { p1, p2, p3Visible, p4 } = msg.values
5063
const currentStep = stepRef.current
5164

65+
if (msg.pageKey !== pageKeyRef.current) {
66+
pageKeyRef.current = msg.pageKey ?? null
67+
baselineRef.current = { p1: null, p2: null, p3Visible: null, p4: null }
68+
setResult(null)
69+
}
70+
5271
if (currentStep === 1) {
5372
// 初回メッセージでベースラインを記録
5473
if (baselineRef.current.p1 === null) {
@@ -109,9 +128,12 @@ function App() {
109128

110129
port.onDisconnect.addListener(() => {
111130
baselineRef.current = { p1: null, p2: null, p3Visible: null, p4: null }
131+
pageKeyRef.current = null
112132
port = null
113133
setResult(null)
114-
setStep(1)
134+
if (!disposed) {
135+
setTimeout(connect, 200)
136+
}
115137
})
116138
} catch (e: any) {
117139
setError(e?.message ?? String(e))
@@ -121,6 +143,7 @@ function App() {
121143
connect()
122144

123145
return () => {
146+
disposed = true
124147
if (port) {
125148
try { port.disconnect() } catch {}
126149
}

0 commit comments

Comments
 (0)