Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Update context menu entry if no profiles #115

Merged
merged 2 commits into from
Jan 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
})
});
}
});