@@ -294,3 +294,38 @@ browser.runtime.onInstalled.addListener(async () => {
294294
295295browser . tabs . onActivated . addListener ( ( { tabId } ) => mutationLinkListener ( tabId ) )
296296browser . tabs . onUpdated . addListener ( ( tabId ) => mutationLinkListener ( tabId ) )
297+
298+ const overlayPopupOpen = async ( tab : browser . Tabs . Tab ) => {
299+ if ( ! tab . id ) return
300+ await browser . tabs . sendMessage ( tab . id , { type : 'TOGGLE_OVERLAY' } )
301+ }
302+
303+ const updateAction = async ( tabId : number ) => {
304+ const tab = await browser . tabs . get ( tabId )
305+
306+ // A normal site where the extension can work
307+ if ( tab . id && ( tab ?. url ?. startsWith ( 'https://' ) || tab ?. url ?. startsWith ( 'http://' ) ) ) {
308+ // The script may not be injected if the extension was just installed
309+ const isContentScriptInjected = await browser . tabs
310+ . sendMessage ( tab . id , { type : 'PING' } ) // The CS must reply 'PONG'
311+ . then ( ( ) => true )
312+ . catch ( ( ) => false )
313+
314+ if ( isContentScriptInjected ) {
315+ await browser . action . setPopup ( { tabId, popup : '' } )
316+ browser . action . onClicked . addListener ( overlayPopupOpen )
317+ } else {
318+ const popupUrl = browser . runtime . getURL ( 'popup.html?page=no-cs-injected' )
319+ await browser . action . setPopup ( { tabId, popup : popupUrl } )
320+ browser . action . onClicked . removeListener ( overlayPopupOpen )
321+ }
322+ } else {
323+ // If it's a system tab where the extension doesn't work
324+ const popupUrl = browser . runtime . getURL ( 'popup.html?page=unsupported-page' )
325+ await browser . action . setPopup ( { tabId, popup : popupUrl } )
326+ browser . action . onClicked . removeListener ( overlayPopupOpen )
327+ }
328+ }
329+
330+ browser . tabs . onActivated . addListener ( ( { tabId } ) => updateAction ( tabId ) )
331+ browser . tabs . onUpdated . addListener ( ( tabId ) => updateAction ( tabId ) )
0 commit comments