1+ import { checkAllowedStatus } from '@vue-devtools-unlocker/shared' ;
2+
13// Store Vue DevTools unlock status for each tab
24const tabStatus : Record < number , unknown > = { } ;
35
@@ -14,18 +16,41 @@ chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
1416// Listen for popup open event
1517chrome . runtime . onConnect . addListener ( ( port ) => {
1618 if ( port . name === 'popup' ) {
19+ port . postMessage ( {
20+ type : 'VueDevtoolsStatus' ,
21+ payload : {
22+ loading : true ,
23+ } ,
24+ } ) ;
1725 // When popup connects, query the current active tab
1826 chrome . tabs . query ( { active : true , currentWindow : true } , ( tabs ) => {
1927 if ( tabs [ 0 ] && tabs [ 0 ] . id ) {
2028 const tabId = tabs [ 0 ] . id ;
21- // Send tab status to popup
22- port . postMessage ( {
23- type : 'VueDevtoolsStatus' ,
24- payload : tabStatus [ tabId ] || {
25- success : false ,
26- message : 'No Vue application detected or DevTools not unlocked on this page' ,
27- } ,
28- } ) ;
29+ let retryCount = 0 ;
30+ const MAX_RETRIES = 5 ; // (5 * 500ms = 2500ms)
31+
32+ const checkStatus = ( ) => {
33+ if ( tabStatus [ tabId ] ) {
34+ port . postMessage ( {
35+ type : 'VueDevtoolsStatus' ,
36+ payload : tabStatus [ tabId ] ,
37+ } ) ;
38+ } else if ( retryCount < MAX_RETRIES ) {
39+ retryCount ++ ;
40+ setTimeout ( checkStatus , 500 ) ;
41+ } else {
42+ // 超时后发送错误状态
43+ port . postMessage ( {
44+ type : 'VueDevtoolsStatus' ,
45+ payload : {
46+ success : false ,
47+ message : 'Unable to detect Vue application status. Please refresh the page and try again.' ,
48+ } ,
49+ } ) ;
50+ }
51+ } ;
52+
53+ checkStatus ( ) ;
2954 }
3055 } ) ;
3156 }
@@ -34,3 +59,10 @@ chrome.runtime.onConnect.addListener((port) => {
3459chrome . tabs . onRemoved . addListener ( ( tabId ) => {
3560 delete tabStatus [ tabId ] ;
3661} ) ;
62+
63+ chrome . tabs . onUpdated . addListener ( ( tabId , changeInfo , tab ) => {
64+ delete tabStatus [ tabId ] ;
65+ if ( changeInfo . status === 'complete' && tab . url ) {
66+ checkAllowedStatus ( tabId , tab . url ) ;
67+ }
68+ } ) ;
0 commit comments