Skip to content

Commit

Permalink
feat: Add API for external extensions to call ff2mpv (#113)
Browse files Browse the repository at this point in the history
* feat: Add onMessageExternal to allow external applications to communicate with ff2mpv

* fix: Typo in console.warn function

Co-authored-by: William Woodruff <william@yossarian.net>

* refactor: Change log to debug

Co-authored-by: William Woodruff <william@yossarian.net>

---------

Co-authored-by: William Woodruff <william@yossarian.net>
  • Loading branch information
DanSM-5 and woodruffw authored Jan 22, 2024
1 parent 8b828f9 commit 4763f1e
Showing 1 changed file with 23 additions and 0 deletions.
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;
}
});
});

0 comments on commit 4763f1e

Please sign in to comment.