@@ -1115,6 +1115,7 @@ async function onAttached(tabId, attachInfo) {
11151115 try {
11161116 log ( 'tabs.onAttached, id: ' , tabId , attachInfo ) ;
11171117 let tab = Tab . get ( tabId ) ;
1118+ await ensureSessionTabValueStored ( tab ) ;
11181119 let attachedTab = await browser . tabs . get ( tabId ) . catch ( ApiTabs . createErrorHandler ( ApiTabs . handleMissingTabError ) ) ;
11191120 if ( ! attachedTab ) {
11201121 // We sometimes fail to get window and tab via API if it is opened
@@ -1212,6 +1213,26 @@ async function onAttached(tabId, attachInfo) {
12121213 }
12131214}
12141215
1216+ // Workaround for https://github.com/piroor/treestyletab/issues/3918
1217+ async function ensureSessionTabValueStored ( tab ) {
1218+ const [ oldId , states ] = await Promise . all ( [
1219+ browser . sessions . getTabValue ( tab . id , Constants . kPERSISTENT_ID ) . catch ( ApiTabs . createErrorHandler ( ) ) ,
1220+ browser . sessions . getTabValue ( tab . id , Constants . kPERSISTENT_STATES ) . catch ( ApiTabs . createErrorHandler ( ) ) ,
1221+ ] ) ;
1222+ if ( ( ! tab . $TST . uniqueId || oldId !== undefined ) &&
1223+ ( ! tab . $TST . $lastPermanentStates || states !== undefined ) )
1224+ return ;
1225+
1226+ log ( `ensureSessionTabValueStored: The tab ${ tab . id } has been moved from another window, and lost its session data unexpectedly, so now we try to save them again for safety.` ) ;
1227+ await Promise . all ( [
1228+ tab . $TST . uniqueId && browser . sessions . setTabValue ( tab . id , Constants . kPERSISTENT_ID , {
1229+ id : tab . $TST . uniqueId . id ,
1230+ tabId : tab . id ,
1231+ } ) . catch ( ApiTabs . createErrorSuppressor ( ) ) ,
1232+ tab . $TST . $lastPermanentStates && browser . sessions . setTabValue ( tab . id , Constants . kPERSISTENT_STATES , tab . $TST . $lastPermanentStates ) . catch ( ApiTabs . createErrorSuppressor ( ) ) ,
1233+ ] ) ;
1234+ }
1235+
12151236async function onDetached ( tabId , detachInfo ) {
12161237 if ( mPromisedStarted )
12171238 await mPromisedStarted ;
0 commit comments