|
1 |
| -import type { RouteLocationNormalized } from 'vue-router'; |
2 |
| - |
3 |
| -import { ContextMenuId, ContextMenus } from '~/models/context/context-menu.model'; |
4 |
| -import { Route, RouterStorageKey } from '~/models/router.model'; |
5 |
| -import { action } from '~/utils/browser/borwser-action.utils'; |
| 1 | +import { ContextMenusHooks, installContextMenus } from '~/models/context/context-menu-hooks.model'; |
| 2 | +import { isContextMenuId } from '~/models/context/context-menu.model'; |
| 3 | +import { MessageType } from '~/models/message/message-type.model'; |
6 | 4 | import { context } from '~/utils/browser/browser-context.utils';
|
7 | 5 | import { runtime } from '~/utils/browser/browser-runtime.utils';
|
8 |
| -import { storage } from '~/utils/browser/browser-storage.utils'; |
9 |
| -import { createTab } from '~/utils/browser/browser.utils'; |
10 | 6 |
|
11 | 7 | console.debug('Background script started');
|
12 | 8 |
|
13 | 9 | runtime?.onInstalled.addListener(async () => {
|
| 10 | + await installContextMenus(); |
| 11 | + |
14 | 12 | console.debug('Extension installed');
|
15 |
| - if (!context) return; |
16 |
| - await Promise.all(Object.values(ContextMenus).map(m => context!.create(m))); |
| 13 | +}); |
17 | 14 |
|
18 |
| - console.debug('Context menus created'); |
| 15 | +runtime?.onMessage.addListener(async message => { |
| 16 | + if (message.type === MessageType.ContextMenu) { |
| 17 | + await installContextMenus(message.enabled); |
| 18 | + } |
19 | 19 | });
|
20 | 20 |
|
21 | 21 | context?.onClicked.addListener(async info => {
|
22 |
| - console.debug('Context menu event', info); |
23 |
| - |
24 |
| - if (!info?.selectionText) return; |
25 |
| - await storage.local.set(RouterStorageKey.LastRoute, { |
26 |
| - name: Route.Search, |
27 |
| - query: { search: info.selectionText }, |
28 |
| - } satisfies Partial<RouteLocationNormalized>); |
29 |
| - |
30 |
| - if (info.menuItemId !== ContextMenuId.OpenInSideTrakt) return; |
31 |
| - |
32 |
| - if (action?.openPopup) { |
33 |
| - await action.openPopup(); |
34 |
| - } else if (runtime) { |
35 |
| - await createTab({ |
36 |
| - url: runtime.getURL('views/options/index.html'), |
37 |
| - }); |
| 22 | + if (isContextMenuId(info.menuItemId)) { |
| 23 | + return ContextMenusHooks[info.menuItemId](info); |
38 | 24 | }
|
| 25 | + console.error('Unknown context menu event', info); |
39 | 26 | });
|
0 commit comments