|
1 | 1 | <script> |
2 | 2 | import {onMount} from 'svelte'; |
3 | | - import chromeP from 'webext-polyfill-kinda'; |
4 | 3 | import Extension from './extension.svelte'; |
5 | 4 | import {focusNext, focusPrevious} from './lib/focus-next.js'; |
| 5 | + import prepareExtensionList from './lib/prepare-extension-list.js'; |
6 | 6 | import UndoStack from './lib/undo-stack.js'; |
7 | 7 | import optionsStorage from './options-storage.js'; |
8 | 8 |
|
9 | 9 | const getI18N = chrome.i18n.getMessage; |
10 | 10 | const undoStack = new UndoStack(window); |
11 | 11 |
|
12 | | - const myid = getI18N('@@extension_id'); |
13 | 12 | let extensions = []; |
14 | 13 | let searchValue = ''; |
15 | 14 |
|
|
41 | 40 | extensions = extensions; |
42 | 41 | } |
43 | 42 |
|
44 | | - function fillInTheBlanks(extension) { |
45 | | - extension.shown = true; |
46 | | - extension.indexedName = extension.name.toLowerCase(); |
47 | | - return extension; |
48 | | - } |
49 | | -
|
50 | 43 | function hideInfoMessage() { |
51 | 44 | localStorage.setItem('undo-info-message', 1); |
52 | 45 | showInfoMessage = false; |
|
88 | 81 | }); |
89 | 82 | } |
90 | 83 |
|
| 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 | +
|
91 | 110 | 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 | + }; |
124 | 128 | }); |
125 | 129 |
|
126 | 130 | // Toggle extra buttons on right click on the name |
|
0 commit comments