@@ -17,7 +17,6 @@ import { getServerBaseUrl, CapUrls } from "./utils/urls";
17
17
18
18
const baseUrl = getServerBaseUrl ( ) ;
19
19
20
- // Check for authentication token
21
20
async function checkAuthToken ( ) : Promise < string | null > {
22
21
try {
23
22
const cookie = await chrome . cookies . get ( {
@@ -41,13 +40,11 @@ async function checkAuthToken(): Promise<string | null> {
41
40
}
42
41
}
43
42
44
- // Verify token freshness and update if needed
45
43
async function verifyTokenFreshness ( ) : Promise < void > {
46
44
try {
47
45
const data = await chrome . storage . local . get ( "authData" ) ;
48
46
const authData = data . authData as AuthResponse | undefined ;
49
47
50
- // If no token exists or token is older than 1 minute, refresh it
51
48
if ( ! authData ?. timestamp || Date . now ( ) - authData . timestamp > 60000 ) {
52
49
await checkAuthToken ( ) ;
53
50
}
@@ -56,7 +53,6 @@ async function verifyTokenFreshness(): Promise<void> {
56
53
}
57
54
}
58
55
59
- // Redirect to login if needed
60
56
async function redirectToLogin ( ) : Promise < void > {
61
57
const tabs = await chrome . tabs . query ( { active : true , currentWindow : true } ) ;
62
58
const currentTab = tabs [ 0 ] ;
@@ -65,7 +61,6 @@ async function redirectToLogin(): Promise<void> {
65
61
}
66
62
}
67
63
68
- // Listen for cookie changes
69
64
chrome . cookies . onChanged . addListener ( async ( changeInfo : CookieChangeInfo ) => {
70
65
if (
71
66
changeInfo . cookie . domain . includes ( baseUrl ) &&
@@ -83,13 +78,9 @@ chrome.cookies.onChanged.addListener(async (changeInfo: CookieChangeInfo) => {
83
78
}
84
79
} ) ;
85
80
86
- // Check token freshness periodically
87
- setInterval ( verifyTokenFreshness , 30000 ) ; // Check every 30 seconds
88
-
89
- // Forward checklist messages to popup
81
+ setInterval ( verifyTokenFreshness , 30000 ) ;
90
82
function forwardToPopup ( message : ImportMessage ) : void {
91
83
try {
92
- // chrome.runtime.sendMessage returns void in MV3, not a Promise
93
84
chrome . runtime . sendMessage ( message , ( response ) => {
94
85
if ( chrome . runtime . lastError ) {
95
86
console . warn (
@@ -106,17 +97,14 @@ function forwardToPopup(message: ImportMessage): void {
106
97
}
107
98
}
108
99
109
- // Store message listeners to avoid duplication
110
100
const messageListeners = new Map ( ) ;
111
101
112
- // Handle messages from content scripts and popup
113
102
chrome . runtime . onMessage . addListener (
114
103
(
115
104
request : { action ?: string ; type ?: string ; [ key : string ] : any } ,
116
105
sender : chrome . runtime . MessageSender ,
117
106
sendResponse : ( response : any ) => void
118
107
) => {
119
- // Handle auth status requests from popup
120
108
if ( request . action === "getAuthStatus" ) {
121
109
verifyTokenFreshness ( )
122
110
. then ( ( ) => checkAuthToken ( ) )
@@ -130,26 +118,21 @@ chrome.runtime.onMessage.addListener(
130
118
console . error ( "Error getting auth status:" , error ) ;
131
119
sendResponse ( { token : null , error : String ( error ) } ) ;
132
120
} ) ;
133
- return true ; // Required for async response
121
+ return true ;
134
122
}
135
123
136
- // Handle checklist messages from content scripts
137
124
if ( request . type && request . type . startsWith ( "CAP_" ) ) {
138
- // Log all Cap messages for debugging
139
125
console . log ( `Received Cap message: ${ request . type } ` , request ) ;
140
126
141
- // Store selected videos in local storage
142
127
if ( request . type === "CAP_LOOM_VIDEOS_SELECTED" && request . videos ) {
143
128
chrome . storage . local . set ( { selectedVideos : request . videos } ) ;
144
129
145
- // Create a copy of the message to avoid mutation issues
146
130
const capMessage : ImportMessage = {
147
131
type : request . type ,
148
132
videos : [ ...request . videos ] ,
149
133
} ;
150
134
forwardToPopup ( capMessage ) ;
151
135
} else if ( request . type ) {
152
- // Forward all other Cap messages to the popup, ensuring type is defined
153
136
const capMessage : ImportMessage = {
154
137
type : request . type ,
155
138
...request ,
@@ -165,18 +148,13 @@ chrome.runtime.onMessage.addListener(
165
148
}
166
149
) ;
167
150
168
- // Allow external extensions to communicate with this extension
169
151
chrome . runtime . onMessageExternal . addListener (
170
152
(
171
153
request : { type : string ; [ key : string ] : any } ,
172
154
sender : chrome . runtime . MessageSender ,
173
155
sendResponse : ( response : any ) => void
174
156
) => {
175
- // Verify the sender is trusted if needed
176
- // if (sender.id !== TRUSTED_EXTENSION_ID) return false;
177
-
178
157
if ( request . type && request . type . startsWith ( "CAP_" ) ) {
179
- // Make a copy of the message with a guaranteed type property
180
158
const capMessage : ImportMessage = {
181
159
...request ,
182
160
} ;
0 commit comments