Skip to content

Commit

Permalink
feat: Update context menu entry if no profiles (#115)
Browse files Browse the repository at this point in the history
* feat: Update context menu entry if no profiles

* refactor: Define TITLE global variable
  • Loading branch information
DanSM-5 authored Jan 28, 2024
1 parent a6fc5a7 commit 75d7bc5
Showing 1 changed file with 96 additions and 59 deletions.
155 changes: 96 additions & 59 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 @@ -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,
})
});
}
});

0 comments on commit 75d7bc5

Please sign in to comment.