From 3771b5a27c985ce45158d1e1381ca3d6c988ad97 Mon Sep 17 00:00:00 2001 From: daniel Date: Wed, 24 Jan 2024 10:23:14 -0500 Subject: [PATCH] refactor: Define TITLE global variable --- ff2mpv.js | 61 +++++++++++++++++++++++++++---------------------------- 1 file changed, 30 insertions(+), 31 deletions(-) diff --git a/ff2mpv.js b/ff2mpv.js index 9b17604..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({ @@ -44,8 +45,6 @@ async function submenuClicked(info) { } } -let os = ''; - function changeToMultiEntries() { // Remove single entry browser.contextMenus.remove('ff2mpv'); @@ -60,7 +59,7 @@ function changeToMultiEntries() { browser.contextMenus.create({ parentId: "ff2mpv", - title: os === "win" ? "Play in MP&V" : "Play in MPV (&W)", + title: TITLE, contexts, onclick: submenuClicked, }); @@ -73,7 +72,7 @@ function changeToSingleEntry() { // Add single entry browser.contextMenus.create({ id: "ff2mpv", - title: os === "win" ? "Play in MP&V" : "Play in MPV (&W)", + title: TITLE, contexts, onclick: submenuClicked, }); @@ -112,8 +111,33 @@ function updateProfile(profile) { }); } -getOS().then(async (_os) => { - os = _os; +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; + } +}); + +getOS().then(async (os) => { + TITLE = os === "win" ? "Play in MP&V" : "Play in MPV (&W)"; const profiles = await getProfiles(); @@ -132,29 +156,4 @@ getOS().then(async (_os) => { }) }); } - - 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; - } - }); });