From 9aa59b31f3d91aa529cc5a4cb63ff1cc640f1258 Mon Sep 17 00:00:00 2001 From: retiolus Date: Fri, 13 Oct 2023 13:32:16 +0200 Subject: [PATCH] implement context menu switcher --- src/entries/background/main.ts | 60 ++++++++++++++++++++++++++++++++++ src/manifest.ts | 3 +- 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/src/entries/background/main.ts b/src/entries/background/main.ts index 3998372..2642f19 100644 --- a/src/entries/background/main.ts +++ b/src/entries/background/main.ts @@ -1,6 +1,7 @@ import browser from "webextension-polyfill"; import type {Action} from "webextension-polyfill"; import {initNative} from "~/lib/native"; +import {nativeLaunchProfile} from "~/lib/native"; import { EXTENSION_ID, fetchAndRoundAvatarAsCanvas, REQUEST_TYPE_CLOSE_MANAGER, STORAGE_CACHE_CUSTOM_AVATARS, STORAGE_CACHE_GLOBAL_OPTIONS, @@ -57,6 +58,65 @@ async function handleEvent(event) { } } +// Function to handle a specific profile selection from the context menu +async function handleProfileSelection(info, tab) { + console.log("Profile selected from context menu."); + console.log(info); + console.log(tab); + + const selectedProfile = info.menuItemId.split('-')[1]; // Extract profile name from the ID + + const urlToOpen = info.pageUrl; + console.log(`URL to open with profile: ${urlToOpen}`); + + // Fetch the list of profiles from storage + const profileListData = await browser.storage.local.get(STORAGE_CACHE_PROFILE_LIST); + + if (profileListData && profileListData[STORAGE_CACHE_PROFILE_LIST]) { + // Find the profile with the name entered by the user + const desiredProfile = profileListData[STORAGE_CACHE_PROFILE_LIST].profiles.find(profile => profile.name === selectedProfile); + + if (desiredProfile) { + // Use the ID of the selected profile to call nativeLaunchProfile + await nativeLaunchProfile(desiredProfile.id, urlToOpen); + console.log(`Profile "${selectedProfile}" launched successfully with URL: ${urlToOpen}`); + } else { + console.error(`Profile "${selectedProfile}" not found!`); + } + } else { + console.error('Failed to retrieve profiles from storage!'); + } +} + +// Create a context menu +browser.contextMenus.create({ + id: "switch-profile", + title: "Open with another profile...", + contexts: ["page"] +}); + +// Add profiles to the context menu +async function addProfilesToContextMenu() { + const profileListData = await browser.storage.local.get(STORAGE_CACHE_PROFILE_LIST); + + if (profileListData && profileListData[STORAGE_CACHE_PROFILE_LIST]) { + for (const profile of profileListData[STORAGE_CACHE_PROFILE_LIST].profiles) { + browser.contextMenus.create({ + id: `profile-${profile.name}`, + title: profile.name, + parentId: "switch-profile", + contexts: ["page"], + onclick: handleProfileSelection + }); + } + } else { + console.error('Failed to retrieve profiles from storage!'); + } +} + +// Call the function to add profiles to the context menu +addProfilesToContextMenu(); + initNative(handleEvent); // Listen for when the winfocus page is launched and close it as fast as possible diff --git a/src/manifest.ts b/src/manifest.ts index 03c3fbe..75ef253 100644 --- a/src/manifest.ts +++ b/src/manifest.ts @@ -5,7 +5,8 @@ const manifest = { permissions: [ "storage", "nativeMessaging", - "tabs" + "tabs", + "contextMenus" ], background: { scripts: ["src/entries/background/main.ts"],