11export { }
22
3- type CheckResult = { ok : boolean ; details : string }
4- // 判定する関数
5- function checkAnswer ( ) : CheckResult {
3+
4+ // -> 要素の「現在の値」を取得するだけの関数に変更
5+ function getActualValue ( ) : string {
66 const el = document . querySelector < HTMLElement > ( '[data-check]' )
7- if ( ! el ) return { ok : false , details : 'data-check 要素が見つかりません' }
8- const expected = el . getAttribute ( 'data-check' ) ?? ''
9- const actual = ( el . textContent ?? '' ) . trim ( )
10- return actual === expected
11- ? { ok : true , details : `一致: "${ actual } "` }
12- : { ok : false , details : `不一致: expected="${ expected } ", actual="${ actual } "` }
7+ if ( ! el ) return '' // 見つからなければ空文字を返す
8+ return ( el . textContent ?? '' ) . trim ( )
139}
1410
1511// デバウンス
1612const debounce = < T extends ( ...a : any [ ] ) => void > ( fn : T , delay : number ) => {
17- let t : number | undefined // タイマーの識別子
13+ let t : number | undefined
1814 return ( ...args : Parameters < T > ) => {
1915 if ( t ) clearTimeout ( t )
2016 // @ts -ignore
@@ -24,19 +20,25 @@ const debounce = <T extends (...a: any[]) => void>(fn: T, delay: number) => {
2420
2521const ports = new Set < chrome . runtime . Port > ( )
2622
27- // サイドパネルからの Port 接続のみ対応
2823chrome . runtime . onConnect . addListener ( ( port ) => {
2924 if ( port . name !== 'sidepanel' ) return
3025 ports . add ( port )
31- // 接続直後に最新判定を送る
32- port . postMessage ( { type : 'UPDATE' , result : checkAnswer ( ) } )
26+
27+ // 接続直後に「現在の値」を送る (判定結果ではない)
28+ port . postMessage ( { type : 'DOM_VALUE_UPDATE' , actual : getActualValue ( ) } )
29+
3330 port . onDisconnect . addListener ( ( ) => ports . delete ( port ) )
3431} )
35- // DevTools等でのDOM変更を監視し、判定結果をサイドパネルへ通知
32+
33+ // DevTools等でのDOM変更を監視し、「現在の値」をサイドパネルへ通知
3634const notify = debounce ( ( ) => {
37- const result = checkAnswer ( )
35+ // 判定(checkAnswer)はしない
36+ const actual = getActualValue ( )
3837 for ( const p of ports ) {
39- try { p . postMessage ( { type : 'UPDATE' , result } ) } catch { }
38+ try {
39+ // 判定結果ではなく、「現在の値」をそのまま送る
40+ p . postMessage ( { type : 'DOM_VALUE_UPDATE' , actual } )
41+ } catch { }
4042 }
4143} , 200 ) // 200ms ディレイ
4244
0 commit comments