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; + } + }); });