From 4763f1e9a45b3803323b7b30c08682dc070e2018 Mon Sep 17 00:00:00 2001 From: Eduardo D Sanchez Date: Mon, 22 Jan 2024 18:25:58 -0500 Subject: [PATCH] feat: Add API for external extensions to call ff2mpv (#113) * feat: Add onMessageExternal to allow external applications to communicate with ff2mpv * fix: Typo in console.warn function Co-authored-by: William Woodruff * refactor: Change log to debug Co-authored-by: William Woodruff --------- Co-authored-by: William Woodruff --- ff2mpv.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/ff2mpv.js b/ff2mpv.js index 584fe24..02c5eae 100644 --- a/ff2mpv.js +++ b/ff2mpv.js @@ -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();" @@ -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; + } + }); });