Skip to content

Commit a839aed

Browse files
authored
Re-sort the list when it loses focus (#162)
1 parent 312a26f commit a839aed

File tree

4 files changed

+64
-44
lines changed

4 files changed

+64
-44
lines changed

package-lock.json

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
},
1616
"dependencies": {
1717
"webext-base-css": "^1.4.4",
18-
"webext-options-sync": "^4.2.4",
19-
"webext-polyfill-kinda": "^1.0.2"
18+
"webext-options-sync": "^4.2.4"
2019
},
2120
"devDependencies": {
2221
"@antfu/eslint-config": "^3.7.1",

source/app.svelte

Lines changed: 44 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
<script>
22
import {onMount} from 'svelte';
3-
import chromeP from 'webext-polyfill-kinda';
43
import Extension from './extension.svelte';
54
import {focusNext, focusPrevious} from './lib/focus-next.js';
5+
import prepareExtensionList from './lib/prepare-extension-list.js';
66
import UndoStack from './lib/undo-stack.js';
77
import optionsStorage from './options-storage.js';
88
99
const getI18N = chrome.i18n.getMessage;
1010
const undoStack = new UndoStack(window);
1111
12-
const myid = getI18N('@@extension_id');
1312
let extensions = [];
1413
let searchValue = '';
1514
@@ -41,12 +40,6 @@
4140
extensions = extensions;
4241
}
4342
44-
function fillInTheBlanks(extension) {
45-
extension.shown = true;
46-
extension.indexedName = extension.name.toLowerCase();
47-
return extension;
48-
}
49-
5043
function hideInfoMessage() {
5144
localStorage.setItem('undo-info-message', 1);
5245
showInfoMessage = false;
@@ -88,39 +81,50 @@
8881
});
8982
}
9083
84+
function handleUninstalled(deleted) {
85+
extensions = extensions.filter(({id}) => id !== deleted);
86+
}
87+
88+
async function handleInstalled(installed) {
89+
if (installed.type === 'extension') {
90+
extensions = prepareExtensionList(await chrome.management.getAll());
91+
}
92+
}
93+
94+
function handleEnabled(updated) {
95+
const extension = extensions.find(({id}) => id === updated.id);
96+
extension.enabled = true;
97+
extensions = extensions;
98+
}
99+
100+
function handleDisabled(updated) {
101+
const extension = extensions.find(({id}) => id === updated.id);
102+
extension.enabled = false;
103+
extensions = extensions;
104+
}
105+
106+
async function prepare() {
107+
extensions = prepareExtensionList(await chrome.management.getAll());
108+
}
109+
91110
onMount(async () => {
92-
const allExtensions = await chromeP.management.getAll();
93-
extensions = allExtensions
94-
.filter(({type, id}) => type === 'extension' && id !== myid)
95-
.sort((a, b) => {
96-
if (a.enabled === b.enabled) {
97-
return a.name.localeCompare(b.name); // Sort by name
98-
}
99-
100-
return a.enabled < b.enabled ? 1 : -1; // Sort by state
101-
})
102-
.map(extension => fillInTheBlanks(extension));
103-
104-
// Update list on global events
105-
chrome.management.onUninstalled.addListener(deleted => {
106-
extensions = extensions.filter(({id}) => id !== deleted);
107-
});
108-
chrome.management.onInstalled.addListener(installed => {
109-
if (installed.type === 'extension') {
110-
// Place new extension at the top
111-
extensions = [fillInTheBlanks(installed), ...extensions];
112-
}
113-
});
114-
chrome.management.onEnabled.addListener(updated => {
115-
const extension = extensions.find(({id}) => id === updated.id);
116-
extension.enabled = true;
117-
extensions = extensions;
118-
});
119-
chrome.management.onDisabled.addListener(updated => {
120-
const extension = extensions.find(({id}) => id === updated.id);
121-
extension.enabled = false;
122-
extensions = extensions;
123-
});
111+
await prepare();
112+
113+
// Add listeners
114+
chrome.management.onUninstalled.addListener(handleUninstalled);
115+
chrome.management.onInstalled.addListener(handleInstalled);
116+
chrome.management.onEnabled.addListener(handleEnabled);
117+
chrome.management.onDisabled.addListener(handleDisabled);
118+
window.addEventListener('blur', prepare);
119+
120+
// Cleanup function
121+
return () => {
122+
chrome.management.onUninstalled.removeListener(handleUninstalled);
123+
chrome.management.onInstalled.removeListener(handleInstalled);
124+
chrome.management.onEnabled.removeListener(handleEnabled);
125+
chrome.management.onDisabled.removeListener(handleDisabled);
126+
window.removeEventListener('blur', prepare);
127+
};
124128
});
125129
126130
// Toggle extra buttons on right click on the name
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function fillInTheBlanks(extension) {
2+
extension.shown = true;
3+
extension.indexedName = extension.name.toLowerCase();
4+
return extension;
5+
}
6+
7+
export default function prepareExtensionList(extensions) {
8+
return extensions
9+
.filter(({type, id}) => type === 'extension' && id !== chrome.runtime.id)
10+
.sort((a, b) => {
11+
if (a.enabled === b.enabled) {
12+
return a.name.localeCompare(b.name); // Sort by name
13+
}
14+
15+
return a.enabled < b.enabled ? 1 : -1; // Sort by state
16+
})
17+
.map(extension => fillInTheBlanks(extension));
18+
}

0 commit comments

Comments
 (0)