Skip to content

Commit

Permalink
feat: Update context menu entry if no profiles
Browse files Browse the repository at this point in the history
  • Loading branch information
DanSM-5 committed Jan 23, 2024
1 parent a6fc5a7 commit 74721a2
Showing 1 changed file with 76 additions and 38 deletions.
114 changes: 76 additions & 38 deletions ff2mpv.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,68 +32,106 @@ async function getOptions(id) {
}

async function submenuClicked(info) {
switch (info.parentMenuItemId) {
case "ff2mpv":
/* These should be mutually exclusive, but,
if they aren't, this is a reasonable priority.
*/
url = info.linkUrl || info.srcUrl || info.selectionText || info.frameUrl;
if (url) {
const options = await getOptions(info.menuItemId);
ff2mpv(url, options);
}
break;
if (info.parentMenuItemId === 'ff2mpv' || info.menuItemId === 'ff2mpv') {
/* These should be mutually exclusive, but,
if they aren't, this is a reasonable priority.
*/
const url = info.linkUrl || info.srcUrl || info.selectionText || info.frameUrl;
if (url) {
const options = await getOptions(info.menuItemId);
ff2mpv(url, options);
}
}
}

function createProfile(profile) {
let os = '';

function changeToMultiEntries() {
// Remove single entry
browser.contextMenus.remove('ff2mpv');

// Add sub context menu
browser.contextMenus.create({
parentId: "ff2mpv",
id: profile.id,
title: profile.name,
id: "ff2mpv",
title: "Profiles",
contexts,
onclick: submenuClicked,
})
}

function deleteProfile(menuItemId) {
browser.contextMenus.remove(menuItemId);
}
});

function updateProfile(profile) {
browser.contextMenus.update(profile.id, {
title: profile.name,
browser.contextMenus.create({
parentId: "ff2mpv",
title: os === "win" ? "Play in MP&V" : "Play in MPV (&W)",
contexts,
onclick: submenuClicked,
});
}

getOS().then(async (os) => {
var title = os == "win" ? "Play in MP&V" : "Play in MPV (&W)";
function changeToSingleEntry() {
// Remove sub context menu
browser.contextMenus.remove('ff2mpv');

// Add single entry
browser.contextMenus.create({
id: "ff2mpv",
title: "Profiles",
title: os === "win" ? "Play in MP&V" : "Play in MPV (&W)",
contexts,
onclick: submenuClicked,
});
}

async function createProfile(profile) {
const profiles = await getProfiles();

if (profiles.length === 0) {
changeToMultiEntries();
}

// Default entry
browser.contextMenus.create({
parentId: "ff2mpv",
title,
id: profile.id,
title: profile.name,
contexts,
onclick: submenuClicked,
})
}

async function deleteProfile(menuItemId) {
browser.contextMenus.remove(menuItemId);

const profiles = (await getProfiles())
.filter(pf => pf.id !== menuItemId);

if (profiles.length === 0) {
changeToSingleEntry();
}
}

function updateProfile(profile) {
browser.contextMenus.update(profile.id, {
title: profile.name,
});
}

getOS().then(async (_os) => {
os = _os;

const profiles = await getProfiles();

profiles.forEach(profile => {
browser.contextMenus.create({
parentId: "ff2mpv",
id: profile.id,
title: profile.name,
contexts,
onclick: submenuClicked,
})
});
if (profiles.length === 0) {
changeToSingleEntry();
} else {
changeToMultiEntries();

profiles.forEach(profile => {
browser.contextMenus.create({
parentId: "ff2mpv",
id: profile.id,
title: profile.name,
contexts,
onclick: submenuClicked,
})
});
}

browser.browserAction.onClicked.addListener((tab) => {
ff2mpv(tab.url);
Expand Down

0 comments on commit 74721a2

Please sign in to comment.