@@ -2,7 +2,43 @@ chrome.runtime.onInstalled.addListener(() => {
22 console . log ( "拡張機能がインストールされました!" ) ;
33} ) ;
44
5- // ツールバーアイコンをクリックしたときにサイドパネルを開く
6- chrome . action . onClicked . addListener ( async ( tab ) => {
7- await chrome . sidePanel . open ( { windowId : tab . windowId , tabId : tab . id } ) ;
5+
6+ const targetURL = 'https://browser-hack.utcode.net/'
7+ function isTargetPage ( url ?: string ) : boolean {
8+ if ( ! url ) return false
9+ return url . startsWith ( targetURL )
10+ }
11+
12+ async function updatePanelAvailability ( tabId : number ) {
13+ try {
14+ const tab = await chrome . tabs . get ( tabId ) ;
15+ if ( isTargetPage ( tab . url ) ) {
16+ await chrome . sidePanel . setOptions ( {
17+ tabId,
18+ path : 'src/sidepanel/index.html' ,
19+ enabled : true
20+ } ) ;
21+ } else {
22+ await chrome . sidePanel . setOptions ( {
23+ tabId,
24+ enabled : false
25+ } ) ;
26+ }
27+ } catch ( e ) {
28+ console . log ( 'Failed to update side panel for tab:' , tabId , e ) ;
29+ }
30+ }
31+
32+ chrome . tabs . onUpdated . addListener ( ( tabId , changeInfo , tab ) => {
33+ if ( changeInfo . status === 'complete' && tab . url ) {
34+ updatePanelAvailability ( tabId ) ;
35+ }
836} ) ;
37+
38+ chrome . tabs . onActivated . addListener ( async ( { tabId } ) => {
39+ updatePanelAvailability ( tabId ) ;
40+ } ) ;
41+
42+ chrome . sidePanel
43+ . setPanelBehavior ( { openPanelOnActionClick : true } )
44+ . catch ( ( error ) => console . error ( error ) ) ;
0 commit comments