Skip to content

Commit b618052

Browse files
Merge pull request monomon#2 from theturboturnip/check-autorun-on-startup
Made apply autosort on startup + minor changes
2 parents eac74b5 + 1759100 commit b618052

2 files changed

Lines changed: 63 additions & 43 deletions

File tree

src/background.js

Lines changed: 55 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -65,26 +65,26 @@ function compareByLastAccessDesc(a, b) {
6565

6666
/** Mapping of {@link menuDefs} IDs to URL comparator functions */
6767
let 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
*/
9090
function 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
*/
108108
function 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
*/
123138
function 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
*/
221231
function 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) {
245255
function 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+
});

src/popup/sortabs.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,11 @@ function onError(error) {
125125
* initializing them if they aren't already there.
126126
*/
127127
function initializeSettings() {
128-
let defaultDict = settingsDefs.reduce(
129-
(acc, cur, idx, src) => Object.assign(acc, { [cur.id]: false }),
130-
{}
131-
);
128+
const defaultDict = {
129+
"last-comparator": undefined,
130+
"settings-sort-auto": false,
131+
"settings-sort-pinned": false,
132+
};
132133
return browser.storage.local.get(defaultDict);
133134
}
134135

@@ -225,7 +226,9 @@ function createPopup(settings) {
225226
);
226227
settingsButtons.forEach((button) => settingsGroup.appendChild(button));
227228

228-
const buttons = menuDefs.map((menuDef) => createSortButton(menuDef, settings));
229+
const buttons = menuDefs.map((menuDef) =>
230+
createSortButton(menuDef, settings)
231+
);
229232
const buttonGroup = document.createElement("div");
230233
buttons.forEach((button) => buttonGroup.appendChild(button));
231234

0 commit comments

Comments
 (0)