@@ -17,7 +17,6 @@ import { getServerBaseUrl, CapUrls } from "./utils/urls";
1717
1818const baseUrl = getServerBaseUrl ( ) ;
1919
20- // Check for authentication token
2120async function checkAuthToken ( ) : Promise < string | null > {
2221 try {
2322 const cookie = await chrome . cookies . get ( {
@@ -41,13 +40,11 @@ async function checkAuthToken(): Promise<string | null> {
4140 }
4241}
4342
44- // Verify token freshness and update if needed
4543async function verifyTokenFreshness ( ) : Promise < void > {
4644 try {
4745 const data = await chrome . storage . local . get ( "authData" ) ;
4846 const authData = data . authData as AuthResponse | undefined ;
4947
50- // If no token exists or token is older than 1 minute, refresh it
5148 if ( ! authData ?. timestamp || Date . now ( ) - authData . timestamp > 60000 ) {
5249 await checkAuthToken ( ) ;
5350 }
@@ -56,7 +53,6 @@ async function verifyTokenFreshness(): Promise<void> {
5653 }
5754}
5855
59- // Redirect to login if needed
6056async function redirectToLogin ( ) : Promise < void > {
6157 const tabs = await chrome . tabs . query ( { active : true , currentWindow : true } ) ;
6258 const currentTab = tabs [ 0 ] ;
@@ -65,7 +61,6 @@ async function redirectToLogin(): Promise<void> {
6561 }
6662}
6763
68- // Listen for cookie changes
6964chrome . cookies . onChanged . addListener ( async ( changeInfo : CookieChangeInfo ) => {
7065 if (
7166 changeInfo . cookie . domain . includes ( baseUrl ) &&
@@ -83,13 +78,9 @@ chrome.cookies.onChanged.addListener(async (changeInfo: CookieChangeInfo) => {
8378 }
8479} ) ;
8580
86- // Check token freshness periodically
87- setInterval ( verifyTokenFreshness , 30000 ) ; // Check every 30 seconds
88-
89- // Forward checklist messages to popup
81+ setInterval ( verifyTokenFreshness , 30000 ) ;
9082function forwardToPopup ( message : ImportMessage ) : void {
9183 try {
92- // chrome.runtime.sendMessage returns void in MV3, not a Promise
9384 chrome . runtime . sendMessage ( message , ( response ) => {
9485 if ( chrome . runtime . lastError ) {
9586 console . warn (
@@ -106,17 +97,14 @@ function forwardToPopup(message: ImportMessage): void {
10697 }
10798}
10899
109- // Store message listeners to avoid duplication
110100const messageListeners = new Map ( ) ;
111101
112- // Handle messages from content scripts and popup
113102chrome . runtime . onMessage . addListener (
114103 (
115104 request : { action ?: string ; type ?: string ; [ key : string ] : any } ,
116105 sender : chrome . runtime . MessageSender ,
117106 sendResponse : ( response : any ) => void
118107 ) => {
119- // Handle auth status requests from popup
120108 if ( request . action === "getAuthStatus" ) {
121109 verifyTokenFreshness ( )
122110 . then ( ( ) => checkAuthToken ( ) )
@@ -130,26 +118,21 @@ chrome.runtime.onMessage.addListener(
130118 console . error ( "Error getting auth status:" , error ) ;
131119 sendResponse ( { token : null , error : String ( error ) } ) ;
132120 } ) ;
133- return true ; // Required for async response
121+ return true ;
134122 }
135123
136- // Handle checklist messages from content scripts
137124 if ( request . type && request . type . startsWith ( "CAP_" ) ) {
138- // Log all Cap messages for debugging
139125 console . log ( `Received Cap message: ${ request . type } ` , request ) ;
140126
141- // Store selected videos in local storage
142127 if ( request . type === "CAP_LOOM_VIDEOS_SELECTED" && request . videos ) {
143128 chrome . storage . local . set ( { selectedVideos : request . videos } ) ;
144129
145- // Create a copy of the message to avoid mutation issues
146130 const capMessage : ImportMessage = {
147131 type : request . type ,
148132 videos : [ ...request . videos ] ,
149133 } ;
150134 forwardToPopup ( capMessage ) ;
151135 } else if ( request . type ) {
152- // Forward all other Cap messages to the popup, ensuring type is defined
153136 const capMessage : ImportMessage = {
154137 type : request . type ,
155138 ...request ,
@@ -165,18 +148,13 @@ chrome.runtime.onMessage.addListener(
165148 }
166149) ;
167150
168- // Allow external extensions to communicate with this extension
169151chrome . runtime . onMessageExternal . addListener (
170152 (
171153 request : { type : string ; [ key : string ] : any } ,
172154 sender : chrome . runtime . MessageSender ,
173155 sendResponse : ( response : any ) => void
174156 ) => {
175- // Verify the sender is trusted if needed
176- // if (sender.id !== TRUSTED_EXTENSION_ID) return false;
177-
178157 if ( request . type && request . type . startsWith ( "CAP_" ) ) {
179- // Make a copy of the message with a guaranteed type property
180158 const capMessage : ImportMessage = {
181159 ...request ,
182160 } ;
0 commit comments