@@ -72,22 +72,25 @@ const setDevServerUrl = async (devServerUrl: string | null): Promise<void> => {
7272 await browser . storage . local . set ( { devServerUrl } )
7373}
7474
75- const toggleSidePanel = async ( req ?: any ) => {
76- const windowId : number = req ?. sender ?. tab ?. windowId
77-
78- if ( ! windowId ) return
79-
75+ const _toggleSidePanel = async ( tabId : number ) => {
8076 // !!! Workaround for user gesture error
8177 // We don't wait for the promise to resolve
8278 const isAlivePromise = SidePanel ( )
8379 . isAlive ( )
8480 . then ( ( ) => true )
8581 . catch ( ( ) => false )
8682
83+ // @ts -ignore
84+ browser . sidePanel . setOptions ( {
85+ tabId : tabId ,
86+ path : 'sidepanel.html' ,
87+ enabled : true ,
88+ } )
89+
8790 // Open the side panel in any way
8891 // Don't wait for promise here too
8992 // @ts -ignore
90- browser . sidePanel . open ( { windowId } )
93+ browser . sidePanel . open ( { tabId } )
9194
9295 const isAlive = await isAlivePromise
9396
@@ -97,6 +100,14 @@ const toggleSidePanel = async (req?: any) => {
97100 }
98101}
99102
103+ const toggleSidePanel = async ( req ?: any ) => {
104+ const tabId : number = req ?. sender ?. tab ?. id
105+
106+ if ( ! tabId ) return
107+
108+ await _toggleSidePanel ( tabId )
109+ }
110+
100111const bgFunctions = {
101112 near_signIn : near . signIn . bind ( near ) ,
102113 near_signOut : near . signOut . bind ( near ) ,
@@ -317,10 +328,6 @@ browser.runtime.onInstalled.addListener(async () => {
317328browser . tabs . onActivated . addListener ( ( { tabId } ) => mutationLinkListener ( tabId ) )
318329browser . tabs . onUpdated . addListener ( ( tabId ) => mutationLinkListener ( tabId ) )
319330
320- // Allows users to open the side panel by clicking on the action toolbar icon
321- // @ts -ignore
322- browser . sidePanel . setPanelBehavior ( { openPanelOnActionClick : true } ) . catch ( console . error )
323-
324331const portConnectListener = async ( port : browser . Runtime . Port ) => {
325332 if ( port . name === 'port-from-page' ) {
326333 const signInListener = ( params : any ) => port . postMessage ( { type : 'signedIn' , params } )
@@ -337,3 +344,37 @@ const portConnectListener = async (port: browser.Runtime.Port) => {
337344}
338345
339346browser . runtime . onConnect . addListener ( portConnectListener )
347+
348+ async function handleActionClick ( tab : browser . Tabs . Tab ) {
349+ if ( ! tab . id ) return
350+ await _toggleSidePanel ( tab . id )
351+ }
352+
353+ const updateAction = async ( tabId : number ) => {
354+ const tab = await browser . tabs . get ( tabId )
355+ // A normal site where the extension can work
356+ if ( tab . id && ( tab ?. url ?. startsWith ( 'https://' ) || tab ?. url ?. startsWith ( 'http://' ) ) ) {
357+ // The script may not be injected if the extension was just installed
358+ const isContentScriptInjected = await ContentScript ( tabId )
359+ . isAlive ( )
360+ . then ( ( ) => true )
361+ . catch ( ( ) => false )
362+
363+ if ( isContentScriptInjected ) {
364+ await browser . action . setPopup ( { tabId, popup : '' } )
365+ browser . action . onClicked . addListener ( handleActionClick )
366+ } else {
367+ const popupUrl = browser . runtime . getURL ( 'popup.html?page=no-cs-injected' )
368+ await browser . action . setPopup ( { tabId, popup : popupUrl } )
369+ browser . action . onClicked . removeListener ( handleActionClick )
370+ }
371+ } else {
372+ // If it's a system tab where the extension doesn't work
373+ const popupUrl = browser . runtime . getURL ( 'popup.html?page=unsupported-page' )
374+ await browser . action . setPopup ( { tabId, popup : popupUrl } )
375+ browser . action . onClicked . removeListener ( handleActionClick )
376+ }
377+ }
378+
379+ browser . tabs . onActivated . addListener ( ( { tabId } ) => updateAction ( tabId ) )
380+ browser . tabs . onUpdated . addListener ( ( tabId ) => updateAction ( tabId ) )
0 commit comments