Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement context menu switcher #90

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions src/entries/background/main.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ const manifest = {
permissions: [
"storage",
"nativeMessaging",
"tabs"
"tabs",
"contextMenus"
],
background: {
scripts: ["src/entries/background/main.ts"],
Expand Down