From 75d7bc53ba05078db9aad2bae5634b9868e74895 Mon Sep 17 00:00:00 2001 From: Eduardo D Sanchez Date: Sun, 28 Jan 2024 12:18:21 -0500 Subject: [PATCH] feat: Update context menu entry if no profiles (#115) * feat: Update context menu entry if no profiles * refactor: Define TITLE global variable --- ff2mpv.js | 155 +++++++++++++++++++++++++++++++++--------------------- 1 file changed, 96 insertions(+), 59 deletions(-) diff --git a/ff2mpv.js b/ff2mpv.js index 02c5eae..21f6973 100644 --- a/ff2mpv.js +++ b/ff2mpv.js @@ -5,6 +5,7 @@ function onError(error) { } const OPEN_VIDEO = 'openVideo'; +let TITLE; function ff2mpv(url, options = []) { browser.tabs.executeScript({ @@ -32,91 +33,127 @@ 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) { +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: TITLE, + 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: TITLE, 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, - }); + }) +} - const profiles = await getProfiles(); +async function deleteProfile(menuItemId) { + browser.contextMenus.remove(menuItemId); - profiles.forEach(profile => { - browser.contextMenus.create({ - parentId: "ff2mpv", - id: profile.id, - title: profile.name, - contexts, - onclick: submenuClicked, - }) - }); + const profiles = (await getProfiles()) + .filter(pf => pf.id !== menuItemId); - browser.browserAction.onClicked.addListener((tab) => { - ff2mpv(tab.url); + if (profiles.length === 0) { + changeToSingleEntry(); + } +} + +function updateProfile(profile) { + browser.contextMenus.update(profile.id, { + title: profile.name, }); +} - // Messages sent with browser.runtime.sendMessage (https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/runtime/sendMessage) from external applications will be handle here. - // ref: https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/runtime/onMessageExternal - browser.runtime.onMessageExternal.addListener((request, sender, sendResponse) => { - if (!request) { - console.warn('No request in external message'); +browser.browserAction.onClicked.addListener((tab) => { + ff2mpv(tab.url); +}); + +// Messages sent with browser.runtime.sendMessage (https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/runtime/sendMessage) from external applications will be handle here. +// ref: https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/runtime/onMessageExternal +browser.runtime.onMessageExternal.addListener((request, sender, sendResponse) => { + if (!request) { + console.warn('No request in external message'); + return; + } + + const { type, url } = request; + console.debug('Request from:', sender); + + switch (type) { + case OPEN_VIDEO: + ff2mpv(url); + return sendResponse('ok'); + default: + console.warn('No handler for external type:', type); return; - } + } +}); - const { type, url } = request; - console.debug('Request from:', sender); +getOS().then(async (os) => { + TITLE = os === "win" ? "Play in MP&V" : "Play in MPV (&W)"; - switch (type) { - case OPEN_VIDEO: - ff2mpv(url); - return sendResponse('ok'); - default: - console.warn('No handler for external type:', type); - return; - } - }); + const profiles = await getProfiles(); + + if (profiles.length === 0) { + changeToSingleEntry(); + } else { + changeToMultiEntries(); + + profiles.forEach(profile => { + browser.contextMenus.create({ + parentId: "ff2mpv", + id: profile.id, + title: profile.name, + contexts, + onclick: submenuClicked, + }) + }); + } });