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: Add API for external extensions to call ff2mpv #113

Merged
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
23 changes: 23 additions & 0 deletions ff2mpv.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ function onError(error) {
console.log(`${error}`);
}

const OPEN_VIDEO = 'openVideo';

function ff2mpv(url, options = []) {
browser.tabs.executeScript({
code: "video = document.getElementsByTagName('video');video[0].pause();"
Expand Down Expand Up @@ -96,4 +98,25 @@ 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;
}
});
});