@@ -4,12 +4,17 @@ Creates the GPT buttons, sets the prompts, and displays the responses.
4
4
The user can also copy the code to their clipboard, clear the code, and open the settings page.
5
5
*/
6
6
7
- import {
8
- getChatGPTAccessToken ,
9
- ChatGPTProvider ,
10
- } from '../background/chatgpt/chatgpt.js' ;
11
-
12
- import { initializeTheme , toggleTheme } from '../utils/theme.js' ;
7
+ import { initializeTheme } from '../utils/theme.js' ;
8
+ import { OpenRouterProvider } from '../background/openrouter/openrouter.js' ;
9
+ import { ChatGPTProvider } from '../background/chatgpt/chatgpt.js' ;
10
+
11
+ // Add interface for ChatGPTProvider at the top level
12
+ interface AIProvider {
13
+ generateAnswer ( params : {
14
+ prompt : string ,
15
+ onEvent : ( arg : { type : string , data ?: { text : string } } ) => void
16
+ } ) : Promise < void > ;
17
+ }
13
18
14
19
/* Element selectors */
15
20
const selectors : { [ key : string ] : string } = {
@@ -75,7 +80,7 @@ function setInfoMessage(message: string, duration: number) {
75
80
} , duration ) ;
76
81
}
77
82
78
- function initActionButton ( buttonId : string , action : string , chatGPTProvider : ChatGPTProvider ) : void {
83
+ function initActionButton ( buttonId : string , action : string , chatGPTProvider : AIProvider ) : void {
79
84
const actionButton = document . getElementById ( buttonId ) ;
80
85
if ( ! actionButton ) return ;
81
86
actionButton . onclick = async ( ) => {
@@ -120,7 +125,7 @@ function formatResponseText(text: string): string {
120
125
}
121
126
122
127
function processCode (
123
- chatGPTProvider : ChatGPTProvider ,
128
+ chatGPTProvider : AIProvider ,
124
129
codeText : string ,
125
130
action : string ,
126
131
) : void {
@@ -200,64 +205,30 @@ function processCode(
200
205
} ) ;
201
206
}
202
207
203
- async function main ( ) : Promise < void > {
204
-
205
- initializeTheme ( ) ;
206
-
207
- await Promise . all ( [
208
+ async function loadStoredData ( ) : Promise < void > {
209
+ const [ analyzeCodeResponseStored , fixCodeResponseStored , lastAction ] = await Promise . all ( [
208
210
getFromStorage ( storageKeys . analyzeCodeResponse ) ,
209
211
getFromStorage ( storageKeys . fixCodeResponse ) ,
210
212
getFromStorage ( storageKeys . lastAction ) ,
211
- getFromStorage ( storageKeys . language ) ,
212
- ] ) ;
213
-
214
- // Load font size from storage
215
- let fontSizeElement = document . documentElement ; // Or any specific element you want to change the font size of
216
- chrome . storage . local . get ( 'fontSize' , function ( data ) {
217
- if ( data . fontSize ) {
218
- fontSizeElement . style . setProperty ( '--dynamic-font-size' , `${ data . fontSize } px` ) ;
219
- if ( parseInt ( data . fontSize ) >= 18 ) {
220
- const width = ( parseInt ( data . fontSize ) * 24 + 200 ) ;
221
- document . body . style . width = `${ width + 20 } px` ;
222
- fixCodeContainer && ( fixCodeContainer . style . maxWidth = `${ width } px` ) ;
223
- analyzeCodeResponse && ( analyzeCodeResponse . style . maxWidth = `${ width } px` ) ;
224
- }
225
-
226
- const sizes = document . getElementsByClassName ( 'material-button' ) ;
227
- for ( let i = 0 ; i < sizes . length ; i ++ ) {
228
- ( sizes [ i ] as HTMLElement ) . style . width = `${ data . fontSize * 13 } px` ;
229
- }
230
- }
231
- } ) ;
213
+ ] ) as [ string , string , string ] ;
232
214
233
- chrome . storage . local . get ( 'analyzeCodeResponse' , function ( data ) {
234
- if ( data . analyzeCodeResponse ) {
235
- analyzeCodeResponse && ( analyzeCodeResponse . innerHTML = data . analyzeCodeResponse ) ;
236
- ( window as any ) . Prism . highlightAll ( ) ;
237
- }
238
- } ) ;
215
+ if ( analyzeCodeResponseStored && lastAction === 'analyze' ) {
216
+ analyzeCodeResponse && ( analyzeCodeResponse . innerHTML = analyzeCodeResponseStored ) ;
217
+ analyzeCodeResponse ?. classList . remove ( 'hidden' ) ;
218
+ }
239
219
240
- chrome . storage . local . get ( 'fixCodeResponse' , function ( data ) {
241
- if ( data . fixCodeResponse ) {
242
- fixCodeResponse && ( fixCodeResponse . textContent = data . fixCodeResponse ) ;
243
- ( window as any ) . Prism . highlightAll ( ) ;
244
- }
245
- } ) ;
220
+ if ( fixCodeResponseStored && lastAction === 'fix' ) {
221
+ fixCodeResponse && ( fixCodeResponse . textContent = fixCodeResponseStored ) ;
222
+ fixCodeContainer ?. classList . remove ( 'hidden' ) ;
223
+ ( window as any ) . Prism . highlightAll ( ) ;
224
+ }
225
+ }
246
226
247
- chrome . storage . local . get ( 'lastAction' , function ( data ) {
248
- if ( data . lastAction ) {
249
- if ( data . lastAction === 'analyze' ) {
250
- analyzeCodeResponse && analyzeCodeResponse . classList . remove ( 'hidden' ) ;
251
- fixCodeContainer && fixCodeContainer . classList . add ( 'hidden' ) ;
252
- }
253
- else if ( data . lastAction === 'fix' ) {
254
- analyzeCodeResponse && analyzeCodeResponse . classList . add ( 'hidden' ) ;
255
- fixCodeContainer && fixCodeContainer . classList . remove ( 'hidden' ) ;
256
- }
257
- }
258
- } ) ;
227
+ async function main ( ) : Promise < void > {
228
+ initializeTheme ( ) ;
229
+ await loadStoredData ( ) ;
259
230
260
- // get name of current tab and set info message to it if its a leetcode problem
231
+ // get name of current tab and set info message
261
232
chrome . tabs . query ( { active : true , currentWindow : true } , ( tabs ) => {
262
233
const tab = tabs [ 0 ] ;
263
234
if ( tab . url && tab . url . includes ( 'leetcode.com/problems' ) ) {
@@ -273,25 +244,39 @@ async function main(): Promise<void> {
273
244
} ) ;
274
245
275
246
try {
276
- const accessToken = await getChatGPTAccessToken ( ) ;
277
- if ( accessToken ) {
278
- const chatGPTProvider = new ChatGPTProvider ( accessToken ) ;
279
- initActionButton ( 'get-complexity-btn' , 'analyze' , chatGPTProvider ) ;
280
- initActionButton ( 'fix-code-btn' , 'fix' , chatGPTProvider ) ;
281
- initCopyButton ( ) ;
282
- initClearButton ( ) ;
283
- elements [ 'getComplexityBtn' ] && elements [ 'getComplexityBtn' ] . classList . remove ( 'hidden' ) ;
284
- elements [ 'fixCodeBtn' ] && elements [ 'fixCodeBtn' ] . classList . remove ( 'hidden' ) ;
247
+ // Get OpenRouter API key from storage
248
+ const data = await chrome . storage . local . get ( 'openRouterApiKey' ) ;
249
+ if ( ! data . openRouterApiKey ) {
250
+ displayApiKeyMessage ( ) ;
251
+ return ;
285
252
}
286
- else {
287
- displayLoginMessage ( ) ;
253
+
254
+ // Verify API key is not empty string
255
+ if ( data . openRouterApiKey . trim ( ) === '' ) {
256
+ displayApiKeyMessage ( ) ;
257
+ return ;
288
258
}
289
- }
290
- catch ( error ) {
259
+
260
+ const openRouterProvider = new OpenRouterProvider ( data . openRouterApiKey ) ;
261
+ initActionButton ( 'get-complexity-btn' , 'analyze' , openRouterProvider ) ;
262
+ initActionButton ( 'fix-code-btn' , 'fix' , openRouterProvider ) ;
263
+ initCopyButton ( ) ;
264
+ initClearButton ( ) ;
265
+ elements [ 'getComplexityBtn' ] ?. classList . remove ( 'hidden' ) ;
266
+ elements [ 'fixCodeBtn' ] ?. classList . remove ( 'hidden' ) ;
267
+ } catch ( error ) {
291
268
handleError ( error as Error ) ;
292
269
}
293
270
}
294
271
272
+ function displayApiKeyMessage ( ) : void {
273
+ elements [ 'loginBtn' ] ?. classList . remove ( 'hidden' ) ;
274
+ if ( infoMessage ) {
275
+ infoMessage . textContent = 'Please add your OpenRouter API key in settings' ;
276
+ }
277
+ disableAllButtons ( true ) ;
278
+ }
279
+
295
280
function initCopyButton ( ) : void {
296
281
const copyButton = elements [ 'copyCodeBtn' ] ;
297
282
if ( ! copyButton ) return ;
@@ -348,9 +333,9 @@ chrome.runtime.onMessage.addListener((message) => {
348
333
} ) ;
349
334
350
335
/* Utility functions */
351
- function getFromStorage ( key : string ) {
336
+ function getFromStorage ( key : string ) : Promise < string > {
352
337
return new Promise ( ( resolve ) => {
353
- chrome . storage . local . get ( key , ( data ) => resolve ( data [ key ] ) ) ;
338
+ chrome . storage . local . get ( key , ( data ) => resolve ( data [ key ] || '' ) ) ;
354
339
} ) ;
355
340
}
356
341
0 commit comments