11import { render } from "preact" ;
22import { Sidebar } from "@/components/Sidebar" ;
33import { currentUrl , quotedSelection } from "@/lib/messaging" ;
4+ import { showQuotePopupOnSelection } from "@/lib/settings" ;
45import "@/lib/styles.css" ;
56import { queryClient , appPersister } from "@/lib/queryClient" ;
67import { QueryClientProvider } from "@tanstack/react-query" ;
@@ -18,38 +19,47 @@ export async function setupTabListener() {
1819 try {
1920 const currentWindow = await browser . windows . getCurrent ( ) ;
2021 extensionWindowId = currentWindow . id ;
21-
22- const [ tab ] = await browser . tabs . query ( { active : true , windowId : extensionWindowId } ) ;
22+
23+ const [ tab ] = await browser . tabs . query ( {
24+ active : true ,
25+ windowId : extensionWindowId ,
26+ } ) ;
2327 if ( tab ?. id && tab . url ) {
2428 activeTabId = tab . id ;
2529 currentUrl . value = tab . url ;
2630 }
27-
28- browser . tabs . onActivated . addListener ( async ( activeInfo : Browser . tabs . TabActiveInfo ) => {
29- if ( activeInfo . windowId === extensionWindowId ) {
30- activeTabId = activeInfo . tabId ;
31- const tab = await browser . tabs . get ( activeInfo . tabId ) ;
32- if ( tab . url ) {
33- currentUrl . value = tab . url ;
34- } else if ( tab . pendingUrl ) {
35- currentUrl . value = tab . pendingUrl ;
36- } else {
37- currentUrl . value = "" ;
31+
32+ browser . tabs . onActivated . addListener (
33+ async ( activeInfo : Browser . tabs . TabActiveInfo ) => {
34+ if ( activeInfo . windowId === extensionWindowId ) {
35+ activeTabId = activeInfo . tabId ;
36+ const tab = await browser . tabs . get ( activeInfo . tabId ) ;
37+ if ( tab . url ) {
38+ currentUrl . value = tab . url ;
39+ } else if ( tab . pendingUrl ) {
40+ currentUrl . value = tab . pendingUrl ;
41+ } else {
42+ currentUrl . value = "" ;
43+ }
3844 }
39- }
40- } ) ;
41-
42- browser . tabs . onUpdated . addListener ( ( tabId : number , changeInfo : Browser . tabs . TabChangeInfo ) => {
43- if ( tabId === activeTabId && changeInfo . url ) {
44- currentUrl . value = changeInfo . url ;
45- }
46- } ) ;
45+ } ,
46+ ) ;
4747
48- browser . runtime . onMessage . addListener ( ( message : ContentScriptSelectionMessage ) => {
49- if ( message . from === "content" && message . type === "SELECTION" ) {
50- quotedSelection . value = message . data . selection || null ;
51- }
52- } ) ;
48+ browser . tabs . onUpdated . addListener (
49+ ( tabId : number , changeInfo : Browser . tabs . TabChangeInfo ) => {
50+ if ( tabId === activeTabId && changeInfo . url ) {
51+ currentUrl . value = changeInfo . url ;
52+ }
53+ } ,
54+ ) ;
55+
56+ browser . runtime . onMessage . addListener (
57+ ( message : ContentScriptSelectionMessage ) => {
58+ if ( message . from === "content" && message . type === "SELECTION" ) {
59+ quotedSelection . value = message . data . selection || null ;
60+ }
61+ } ,
62+ ) ;
5363 } catch ( error ) {
5464 console . error ( "Error setting up tab listener:" , error ) ;
5565 }
@@ -64,6 +74,26 @@ function App() {
6474 ) ;
6575}
6676
77+ browser . runtime . onMessage . addListener (
78+ (
79+ message : {
80+ type : "PING_SIDEPANEL" ;
81+ from : "content" ;
82+ } ,
83+ sender : Browser . runtime . MessageSender ,
84+ sendResponse : ( response ?: object ) => void ,
85+ ) => {
86+ if ( message . type === "PING_SIDEPANEL" ) {
87+ if ( showQuotePopupOnSelection . value ) {
88+ sendResponse ( { type : "PONG_SIDEPANEL" , from : "sidepanel" , showPopup : true } ) ;
89+ } else {
90+ sendResponse ( { type : "PONG_SIDEPANEL" , from : "sidepanel" , showPopup : false } ) ;
91+ }
92+ return true ;
93+ }
94+ } ,
95+ ) ;
96+
6797( async ( ) => {
6898 try {
6999 await setupTabListener ( ) ;
@@ -72,5 +102,5 @@ function App() {
72102 } catch ( error ) {
73103 console . error ( "Error during initial setup or restore:" , error ) ;
74104 }
75- render ( < App /> , document . getElementById ( "app" ) ! ) ;
76- } ) ( ) ;
105+ render ( < App /> , document . getElementById ( "app" ) ! ) ;
106+ } ) ( ) ;
0 commit comments