11import { getChatGPTAccessToken } from './chatgpt/chatgpt.js' ;
22
3- chrome . runtime . onMessage . addListener ( ( request : any ) => {
4- if ( request . type === 'OPEN_LOGIN_PAGE' ) {
5- chrome . tabs . create ( { url : 'https://chat.openai.com' } ) ;
6- }
7- } ) ;
8-
93// Runs when the extension is installed
104chrome . runtime . onInstalled . addListener ( ( ) => {
115 const jsonUrl = chrome . runtime . getURL ( 'src/assets/data/leetcode_solutions.json' ) ;
@@ -37,15 +31,35 @@ chrome.runtime.onMessage.addListener(
3731 }
3832 // Append solutions/
3933 const newUrl = url + 'solutions/' ;
40- chrome . tabs . update ( tabs [ 0 ] . id , { url : newUrl } ) ;
34+ if ( tabs . length > 0 && tabs [ 0 ] . id ) {
35+ const tabId = tabs [ 0 ] . id ;
36+ const updateProperties = { url : newUrl } ;
37+ chrome . tabs . update ( tabId , updateProperties ) ;
38+ }
4139 }
4240 } ) ;
4341 sendResponse ( { result : "Success" } ) ;
4442 }
4543 }
4644) ;
4745
46+ chrome . runtime . onMessage . addListener ( ( request : any ) => {
47+ if ( request . type === 'OPEN_LOGIN_PAGE' ) {
48+ chrome . tabs . create ( { url : 'https://chat.openai.com' } ) ;
49+ }
50+ } ) ;
51+
52+ chrome . runtime . onMessage . addListener ( ( request , sender , sendResponse ) => {
53+ if ( request . type === 'GET_CHATGPT_ACCESS_TOKEN' ) {
54+ getChatGPTAccessToken ( ) . then ( ( accessToken ) => {
55+ sendResponse ( { accessToken : accessToken } ) ;
56+ } ) ;
57+ return true ;
58+ }
59+ } ) ;
60+
4861chrome . tabs . onUpdated . addListener ( ( tabId , changeInfo , tab ) => {
62+ // If descriptions tab is opened or updated, update the description
4963 let urlPattern = / ^ h t t p s : \/ \/ l e e t c o d e \. c o m \/ p r o b l e m s \/ .* \/ ( d e s c r i p t i o n \/ ) ? / ;
5064 if ( changeInfo . status === 'complete' && tab . url && tab . url . match ( urlPattern ) ) {
5165 setTimeout ( ( ) => {
@@ -55,6 +69,7 @@ chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
5569 } , 1000 ) ;
5670 }
5771
72+ // If solutions tab is opened or updated, add the video
5873 urlPattern = / ^ h t t p s : \/ \/ l e e t c o d e \. c o m \/ p r o b l e m s \/ .* \/ s o l u t i o n s \/ ? / ;
5974 if ( changeInfo . status === 'complete' && tab . url && tab . url . match ( urlPattern ) ) {
6075 setTimeout ( ( ) => {
@@ -64,19 +79,11 @@ chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
6479 } , 1000 ) ;
6580 }
6681
82+ // If problem tab is opened or updated, update the current problem title
6783 urlPattern = / ^ h t t p s : \/ \/ l e e t c o d e \. c o m \/ p r o b l e m s \/ .* \/ ? / ;
6884 if ( changeInfo . status === 'complete' && tab . url && tab . url . match ( urlPattern ) ) {
6985 setTimeout ( ( ) => {
7086 chrome . storage . local . set ( { 'currentLeetCodeProblemTitle' : tab . title || 'title' } ) ;
7187 } , 1000 ) ;
7288 }
73- } ) ;
74-
75- chrome . runtime . onMessage . addListener ( ( request , sender , sendResponse ) => {
76- if ( request . type === 'GET_CHATGPT_ACCESS_TOKEN' ) {
77- getChatGPTAccessToken ( ) . then ( ( accessToken ) => {
78- sendResponse ( { accessToken : accessToken } ) ;
79- } ) ;
80- return true ;
81- }
82- } ) ;
89+ } ) ;
0 commit comments