diff --git a/Chrome/common.js b/Chrome/common.js index e5e9f3a..26173de 100644 --- a/Chrome/common.js +++ b/Chrome/common.js @@ -72,8 +72,10 @@ export function restoreOptions() { export function openInMPV(tabId, url, options = {}) { const baseURL = `mpv:///open?`; + // Encode video URL const params = [`url=${encodeURIComponent(url)}`]; + // Add playback options switch (options.mode) { case "fullScreen": @@ -83,22 +85,24 @@ export function openInMPV(tabId, url, options = {}) { case "enqueue": params.push("enqueue=1"); break; } + // Add new window option if (options.newWindow) { params.push("new_window=1"); } + // Add alternative player and user-defined custom flags - getOptions(items => { - params.push(`player=${items["mpvPlayer"]}`) - if (items["useCustomFlags"]) - params.push(`flags=${encodeURIComponent(items["customFlags"])}`); - }) + params.push(`player=${options.mpvPlayer}`); + if (options.useCustomFlags && options.customFlags !== "") + params.push(`flags=${encodeURIComponent(options.customFlags)}`); + const code = ` var link = document.createElement('a'); link.href='${baseURL}${params.join("&")}'; document.body.appendChild(link); link.click(); `; + console.log(code); chrome.tabs.executeScript(tabId, { code }); } @@ -115,6 +119,7 @@ export function updateBrowserAction() { if (tab.id === chrome.tabs.TAB_ID_NONE) { return; } openInMPV(tab.id, tab.url, { mode: options.iconActionOption, + ...options, }); }); }); diff --git a/Chrome/manifest.json b/Chrome/manifest.json index 213ab3f..29cd649 100644 --- a/Chrome/manifest.json +++ b/Chrome/manifest.json @@ -1,7 +1,7 @@ { "manifest_version": 2, "name": "Open In MPV", - "description": "Open videos and audios in mpv.", + "description": "Open videos and audio files in mpv.", "version": "1.0.0", "options_page": "options.html", "background": { diff --git a/Chrome/options.js b/Chrome/options.js index a57bdea..3e98780 100644 --- a/Chrome/options.js +++ b/Chrome/options.js @@ -1,10 +1,13 @@ import { restoreOptions, saveOptions, updateBrowserAction } from "./common.js"; -document.addEventListener("DOMContentLoaded", restoreOptions); - -Array.prototype.forEach.call(document.getElementsByTagName("input"), (el) => { +function listener(el) { el.addEventListener("change", () => { saveOptions(); updateBrowserAction(); }); -}); +} + +document.addEventListener("DOMContentLoaded", restoreOptions); + +Array.prototype.forEach.call(document.getElementsByTagName("input"), listener); +Array.prototype.forEach.call(document.getElementsByTagName("select"), listener); diff --git a/Chrome/popup.js b/Chrome/popup.js index d1240ba..38e8cd7 100644 --- a/Chrome/popup.js +++ b/Chrome/popup.js @@ -1,16 +1,19 @@ -import { openInMPV } from "./common.js"; +import { openInMPV, getOptions } from "./common.js"; Array.prototype.forEach.call(document.getElementsByClassName("menu-item"), (item) => { const mode = item.id.split("-")[1]; item.addEventListener("click", () => { - chrome.tabs.query({ currentWindow: true, active: true }, (tabs) => { - if (tabs.length === 0) { return; } - const tab = tabs[0]; - if (tab.id === chrome.tabs.TAB_ID_NONE) { return; } - console.log(mode) - openInMPV(tab.id, tab.url, { - mode, - newWindow: mode === "newWindow", + getOptions((options) => { + chrome.tabs.query({ currentWindow: true, active: true }, (tabs) => { + if (tabs.length === 0) { return; } + const tab = tabs[0]; + if (tab.id === chrome.tabs.TAB_ID_NONE) { return; } + console.log(mode) + openInMPV(tab.id, tab.url, { + mode, + newWindow: mode === "newWindow", + ...options, + }); }); }); }); diff --git a/Firefox/manifest.json b/Firefox/manifest.json index 31e7f1d..2305d0b 100644 --- a/Firefox/manifest.json +++ b/Firefox/manifest.json @@ -6,7 +6,7 @@ } }, "name": "Open In mpv", - "description": "Open videos and audios in mpv.", + "description": "Open videos and audio files in mpv.", "version": "1.0.0", "options_ui": { "page": "options.html" diff --git a/Makefile b/Makefile index 226e1c5..881df14 100644 --- a/Makefile +++ b/Makefile @@ -15,5 +15,10 @@ install: all uninstall: rm /usr/bin/open-in-mpv +firefox: + cp -t Firefox Chrome/{*.html,*.js,*.png,*.css} + pushd Firefox && zip ../Firefox.zip * && popd + @rm Firefox/{*.html,*.js,*.png,*.css} + clean: - rm open-in-mpv \ No newline at end of file + @rm open-in-mpv Firefox.zip \ No newline at end of file diff --git a/src/mpvopts.hpp b/src/mpvopts.hpp index 08dbd67..ec03da0 100644 --- a/src/mpvopts.hpp +++ b/src/mpvopts.hpp @@ -8,12 +8,17 @@ #include using std::string; +#include + string url_decode(const string encoded); string query_value(string query, string key); +string query_value(string query, string key, string fallback); class mpvoptions { private: string url; + string flags; + string player; bool fullscreen; bool pip; bool enqueue; @@ -32,8 +37,9 @@ public: }; mpvoptions::mpvoptions() { - // TODO: add custom flag options, custom player etc this->curlu = curl_url(); + this->flags = ""; + this->player = "mpv"; this->fullscreen = false; this->pip = false; this->enqueue = false; @@ -49,13 +55,16 @@ mpvoptions::~mpvoptions() { string mpvoptions::build_cmd() { std::ostringstream ret; - ret << "mpv "; + ret << this->player << " "; if (this->fullscreen) ret << "--fs "; if (this->pip) ret << "--ontop --no-border --autofit=384x216 --geometry=98\%:98\% "; + if (!this->flags.empty()) + ret << this->flags << " "; // NOTE: this is not needed for mpv (it always opens a new window), maybe for other players? // if (this->new_window) ret << "--new-window"; ret << this->url; + std::cout << ret.str() << std::endl; return ret.str(); } @@ -109,6 +118,8 @@ void mpvoptions::parse(const char *url) { string querystr(query); curl_free(query); this->url = url_decode(query_value(querystr, "url")); + this->flags = url_decode(query_value(querystr, "flags")); + this->player = query_value(querystr, "player", "mpv"); this->fullscreen = query_value(querystr, "fullscreen") == "1"; this->pip = query_value(querystr, "pip") == "1"; this->enqueue = query_value(querystr, "enqueue") == "1"; @@ -160,4 +171,10 @@ string query_value(string query, string key) { return query.substr(offset, query.find('&', pos) - offset); } +string query_value(string query, string key, string fallback) { + string ret = query_value(query, key); + if (ret.empty()) return fallback; + return ret; +} + #endif \ No newline at end of file