@@ -32,6 +32,22 @@ export async function getTabMarker(tabId: number) {
32
32
} ) ;
33
33
}
34
34
35
+ async function assignTabMarker ( tabId : number , marker : string ) {
36
+ return withTabMarkers ( ( { free, tabIdsToMarkers, markersToTabIds } ) => {
37
+ if ( ! free . includes ( marker ) ) {
38
+ throw new Error (
39
+ `Unable to assign marker ${ marker } as it's already in use`
40
+ ) ;
41
+ }
42
+
43
+ const markerIndex = free . indexOf ( marker ) ;
44
+ free . splice ( markerIndex , 1 ) ;
45
+
46
+ tabIdsToMarkers . set ( tabId , marker ) ;
47
+ markersToTabIds . set ( marker , tabId ) ;
48
+ } ) ;
49
+ }
50
+
35
51
export async function getTabIdForMarker ( marker : string ) {
36
52
return withTabMarkers ( ( { markersToTabIds } ) => {
37
53
const tabId = markersToTabIds . get ( marker ) ;
@@ -84,25 +100,32 @@ async function resetTabMarkers() {
84
100
export async function initTabMarkers ( ) {
85
101
await resetTabMarkers ( ) ;
86
102
87
- // We need to reload all tabs where the content script is not running in case
103
+ // We need to assign the tab markers to their corresponding tab id in case
88
104
// the user has the setting "Continue where you left off" enabled. If we don't
89
- // those tabs will have an invalid tab marker. We also can't reassign the tab
90
- // markers to those tabs since we can't get their title, so we don't know
91
- // which tab marker each one has.
105
+ // those tabs will have an invalid tab marker.
92
106
93
107
if ( ! ( await retrieve ( "includeTabMarkers" ) ) ) return ;
94
108
95
109
const tabs = await browser . tabs . query ( { } ) ;
96
110
111
+ const getMarkerFromTitle = ( title : string ) => {
112
+ return / ^ ( [ a - z ] { 1 , 2 } ) \| / i. exec ( title ) ?. [ 1 ] ?. toLowerCase ( ) ;
113
+ } ;
114
+
97
115
await Promise . allSettled (
98
- tabs . map ( async ( tab ) => {
116
+ tabs . map ( async ( { title, id } ) => {
117
+ if ( ! title || ! id ) return ;
118
+
119
+ const marker = getMarkerFromTitle ( title ) ;
120
+ if ( ! marker ) return ;
121
+
99
122
try {
100
- await sendRequestToContent (
101
- { type : "checkContentScriptRunning" } ,
102
- tab . id
103
- ) ;
123
+ await assignTabMarker ( id , marker ) ;
104
124
} catch {
105
- return browser . tabs . reload ( tab . id ) ;
125
+ // If the tab marker is already in use we reload the tab so it gets a
126
+ // new one. I'm not entirely sure if this is necessary but I leave it
127
+ // here just to be safe.
128
+ return browser . tabs . reload ( id ) ;
106
129
}
107
130
} )
108
131
) ;
0 commit comments