Skip to content

Commit

Permalink
refactor: Define TITLE global variable
Browse files Browse the repository at this point in the history
  • Loading branch information
DanSM-5 committed Jan 24, 2024
1 parent 74721a2 commit 3771b5a
Showing 1 changed file with 30 additions and 31 deletions.
61 changes: 30 additions & 31 deletions ff2mpv.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ function onError(error) {
}

const OPEN_VIDEO = 'openVideo';
let TITLE;

function ff2mpv(url, options = []) {
browser.tabs.executeScript({
Expand Down Expand Up @@ -44,8 +45,6 @@ async function submenuClicked(info) {
}
}

let os = '';

function changeToMultiEntries() {
// Remove single entry
browser.contextMenus.remove('ff2mpv');
Expand All @@ -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,
});
Expand All @@ -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,
});
Expand Down Expand Up @@ -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();

Expand All @@ -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;
}
});
});

0 comments on commit 3771b5a

Please sign in to comment.