@@ -65,26 +65,26 @@ function compareByLastAccessDesc(a, b) {
6565
6666/** Mapping of {@link menuDefs} IDs to URL comparator functions */
6767let menuIdToComparator = {
68- "sort-by-url-asc" : compareByUrlAsc ,
69- "sort-by-url-desc" : compareByUrlDesc ,
70- "sort-by-domain-asc" : compareByDomainAsc ,
71- "sort-by-domain-desc" : compareByDomainDesc ,
72- "sort-by-last-access-asc" : compareByLastAccessAsc ,
73- "sort-by-last-access-desc" : compareByLastAccessDesc ,
74- "sort-by-title-asc" : compareByTitleAsc ,
75- "sort-by-title-desc" : compareByTitleDesc ,
76- } ;
68+ "sort-by-url-asc" : compareByUrlAsc ,
69+ "sort-by-url-desc" : compareByUrlDesc ,
70+ "sort-by-domain-asc" : compareByDomainAsc ,
71+ "sort-by-domain-desc" : compareByDomainDesc ,
72+ "sort-by-last-access-asc" : compareByLastAccessAsc ,
73+ "sort-by-last-access-desc" : compareByLastAccessDesc ,
74+ "sort-by-title-asc" : compareByTitleAsc ,
75+ "sort-by-title-desc" : compareByTitleDesc ,
76+ } ;
7777
7878/**
7979 * Settings Functions
8080 */
8181
8282/**
83- * Function called when the setting for auto-sorting changing.
8483 * Adds/removes listener for automatic tab sorting.
85- *
84+ * Called when the setting for auto-sorting changes, or when the extension is first loaded.
85+ *
8686 * @see settingsSortAutoHandler
87- *
87+ *
8888 * @param {boolean } newValue The new value of the auto-sorting setting
8989 */
9090function onSettingsSortAuto ( newValue ) {
@@ -100,9 +100,9 @@ function onSettingsSortAuto(newValue) {
100100}
101101
102102/**
103- * Function called when the setting for sorting pinned tabs changing.
104103 * No-op.
105- *
104+ * Called when the setting for sorting pinned tabs changes.
105+ *
106106 * @param {boolean } newValue The new value of the pinned tab sorting setting
107107 */
108108function onSettingsSortPinned ( newValue ) {
@@ -115,28 +115,39 @@ let settingsMenuIdToHandler = {
115115 "settings-sort-pinned" : onSettingsSortPinned ,
116116} ;
117117
118+ /**
119+ * Returns the settings stored in browser persistent storage,
120+ * initializing them if they aren't already there.
121+ *
122+ * @returns {Promise } A Promise containing the current settings
123+ */
124+ function initializeSettings ( ) {
125+ const defaultDict = {
126+ "last-comparator" : undefined ,
127+ "settings-sort-auto" : false ,
128+ "settings-sort-pinned" : false ,
129+ } ;
130+ return browser . storage . local . get ( defaultDict ) ;
131+ }
132+
118133/**
119134 * Listener for tabs.onCreated and tabs.onUpdated that automatically sorts tabs.
120- *
135+ *
121136 * @see onSettingsSortAuto
122137 */
123138function settingsSortAutoHandler ( ) {
124- browser . storage . local
125- . get ( {
126- "last-comparator" : undefined ,
127- "settings-sort-auto" : false ,
128- "settings-sort-pinned" : false ,
129- } )
130- . then ( ( settings ) => {
131- if ( menuIdToComparator [ settings [ "last-comparator" ] ] !== undefined ) {
132- return sortTabs (
133- menuIdToComparator [ settings [ "last-comparator" ] ] ,
134- settings
135- ) ;
136- } else {
137- console . warning ( "Tried to automatically sort tabs but couldn't find the last-comparator. Doing nothing instead." ) ;
138- }
139- } , onError ) ;
139+ initializeSettings ( ) . then ( ( settings ) => {
140+ if ( menuIdToComparator [ settings [ "last-comparator" ] ] !== undefined ) {
141+ return sortTabs (
142+ menuIdToComparator [ settings [ "last-comparator" ] ] ,
143+ settings
144+ ) ;
145+ } else {
146+ console . warning (
147+ "Tried to automatically sort tabs but couldn't find the last-comparator. Doing nothing instead."
148+ ) ;
149+ }
150+ } , onError ) ;
140151}
141152
142153/**
@@ -145,7 +156,7 @@ function settingsSortAutoHandler() {
145156
146157/**
147158 * Sorts tabs given a comparator function and the current tab-sorting settings
148- *
159+ *
149160 * @param {function } comparator The comparator function to compare URLs
150161 * @param settings The current tab-sorting settings
151162 * @returns {Promise } A Promise which will be fulfilled once tabs are sorted
@@ -171,7 +182,7 @@ function sortTabs(comparator, settings) {
171182
172183/**
173184 * Internal function for sorting a set of tabs
174- *
185+ *
175186 * @param {Tab[] } tabs The tabs which will be sorted
176187 * @param {function } comparator The comparator to use on URLs
177188 */
@@ -205,23 +216,22 @@ function sortTabsInternal(tabs, comparator) {
205216 }
206217}
207218
208- /**
219+ /**
209220 * "public" API - functions which are called from the popup in popup/sortabs.js
210221 */
211222
212-
213223/**
214224 * Called when a setting has changed.
215225 * Updates local storage, and handles any necessary background state changes
216226 * e.g. adding/removing listeners.
217- *
227+ *
218228 * @param {string } settingId The ID of the changed setting
219229 * @param {boolean } newValue The new value of the changed setting
220230 */
221231function settingChanged ( settingId , newValue ) {
222232 // First, call the handler
223233 return settingsMenuIdToHandler [ settingId ] ( newValue ) . then ( ( e ) => {
224- // Once that's finished, store the new value of the setting
234+ // Once that's finished, store the new value of the setting
225235 return browser . storage . local . set ( {
226236 [ settingId ] : newValue ,
227237 } ) ;
@@ -232,7 +242,7 @@ function settingChanged(settingId, newValue) {
232242 * Sort the tabs using the given comparator and current settings state.
233243 * @see menuIdToComparator
234244 * @see sortTabs
235- *
245+ *
236246 * @param {string } compName The ID of the comparator to use
237247 * @param settings Current tab-sorting settings
238248 * @returns {Promise } A Promise which will be fulfilled once tabs are sorted.
@@ -245,3 +255,10 @@ function sortTabsComparatorName(compName, settings) {
245255function onError ( error ) {
246256 console . trace ( error ) ;
247257}
258+
259+ // When the extension is loaded, check if the auto-sort setting is checked and immediately add handlers
260+ document . addEventListener ( "DOMContentLoaded" , ( evt ) => {
261+ initializeSettings ( ) . then ( ( settings ) => {
262+ onSettingsSortAuto ( settings [ "settings-sort-auto" ] ) ;
263+ } , onError ) ;
264+ } ) ;
0 commit comments