Updated extension to support using any mpv-compatible player and custom flags, simplified javascript, added query_value overload with fallback string

This commit is contained in:
Baldomo
2020-12-19 13:51:53 +01:00
parent 8659ddeeee
commit 47c646ce51
7 changed files with 56 additions and 23 deletions

View File

@@ -72,8 +72,10 @@ export function restoreOptions() {
export function openInMPV(tabId, url, options = {}) { export function openInMPV(tabId, url, options = {}) {
const baseURL = `mpv:///open?`; const baseURL = `mpv:///open?`;
// Encode video URL // Encode video URL
const params = [`url=${encodeURIComponent(url)}`]; const params = [`url=${encodeURIComponent(url)}`];
// Add playback options // Add playback options
switch (options.mode) { switch (options.mode) {
case "fullScreen": case "fullScreen":
@@ -83,22 +85,24 @@ export function openInMPV(tabId, url, options = {}) {
case "enqueue": case "enqueue":
params.push("enqueue=1"); break; params.push("enqueue=1"); break;
} }
// Add new window option // Add new window option
if (options.newWindow) { if (options.newWindow) {
params.push("new_window=1"); params.push("new_window=1");
} }
// Add alternative player and user-defined custom flags // Add alternative player and user-defined custom flags
getOptions(items => { params.push(`player=${options.mpvPlayer}`);
params.push(`player=${items["mpvPlayer"]}`) if (options.useCustomFlags && options.customFlags !== "")
if (items["useCustomFlags"]) params.push(`flags=${encodeURIComponent(options.customFlags)}`);
params.push(`flags=${encodeURIComponent(items["customFlags"])}`);
})
const code = ` const code = `
var link = document.createElement('a'); var link = document.createElement('a');
link.href='${baseURL}${params.join("&")}'; link.href='${baseURL}${params.join("&")}';
document.body.appendChild(link); document.body.appendChild(link);
link.click(); link.click();
`; `;
console.log(code);
chrome.tabs.executeScript(tabId, { code }); chrome.tabs.executeScript(tabId, { code });
} }
@@ -115,6 +119,7 @@ export function updateBrowserAction() {
if (tab.id === chrome.tabs.TAB_ID_NONE) { return; } if (tab.id === chrome.tabs.TAB_ID_NONE) { return; }
openInMPV(tab.id, tab.url, { openInMPV(tab.id, tab.url, {
mode: options.iconActionOption, mode: options.iconActionOption,
...options,
}); });
}); });
}); });

View File

@@ -1,7 +1,7 @@
{ {
"manifest_version": 2, "manifest_version": 2,
"name": "Open In MPV", "name": "Open In MPV",
"description": "Open videos and audios in mpv.", "description": "Open videos and audio files in mpv.",
"version": "1.0.0", "version": "1.0.0",
"options_page": "options.html", "options_page": "options.html",
"background": { "background": {

View File

@@ -1,10 +1,13 @@
import { restoreOptions, saveOptions, updateBrowserAction } from "./common.js"; import { restoreOptions, saveOptions, updateBrowserAction } from "./common.js";
document.addEventListener("DOMContentLoaded", restoreOptions); function listener(el) {
Array.prototype.forEach.call(document.getElementsByTagName("input"), (el) => {
el.addEventListener("change", () => { el.addEventListener("change", () => {
saveOptions(); saveOptions();
updateBrowserAction(); updateBrowserAction();
}); });
}); }
document.addEventListener("DOMContentLoaded", restoreOptions);
Array.prototype.forEach.call(document.getElementsByTagName("input"), listener);
Array.prototype.forEach.call(document.getElementsByTagName("select"), listener);

View File

@@ -1,16 +1,19 @@
import { openInMPV } from "./common.js"; import { openInMPV, getOptions } from "./common.js";
Array.prototype.forEach.call(document.getElementsByClassName("menu-item"), (item) => { Array.prototype.forEach.call(document.getElementsByClassName("menu-item"), (item) => {
const mode = item.id.split("-")[1]; const mode = item.id.split("-")[1];
item.addEventListener("click", () => { item.addEventListener("click", () => {
chrome.tabs.query({ currentWindow: true, active: true }, (tabs) => { getOptions((options) => {
if (tabs.length === 0) { return; } chrome.tabs.query({ currentWindow: true, active: true }, (tabs) => {
const tab = tabs[0]; if (tabs.length === 0) { return; }
if (tab.id === chrome.tabs.TAB_ID_NONE) { return; } const tab = tabs[0];
console.log(mode) if (tab.id === chrome.tabs.TAB_ID_NONE) { return; }
openInMPV(tab.id, tab.url, { console.log(mode)
mode, openInMPV(tab.id, tab.url, {
newWindow: mode === "newWindow", mode,
newWindow: mode === "newWindow",
...options,
});
}); });
}); });
}); });

View File

@@ -6,7 +6,7 @@
} }
}, },
"name": "Open In mpv", "name": "Open In mpv",
"description": "Open videos and audios in mpv.", "description": "Open videos and audio files in mpv.",
"version": "1.0.0", "version": "1.0.0",
"options_ui": { "options_ui": {
"page": "options.html" "page": "options.html"

View File

@@ -15,5 +15,10 @@ install: all
uninstall: uninstall:
rm /usr/bin/open-in-mpv 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: clean:
rm open-in-mpv @rm open-in-mpv Firefox.zip

View File

@@ -8,12 +8,17 @@
#include <string> #include <string>
using std::string; using std::string;
#include <iostream>
string url_decode(const string encoded); string url_decode(const string encoded);
string query_value(string query, string key); string query_value(string query, string key);
string query_value(string query, string key, string fallback);
class mpvoptions { class mpvoptions {
private: private:
string url; string url;
string flags;
string player;
bool fullscreen; bool fullscreen;
bool pip; bool pip;
bool enqueue; bool enqueue;
@@ -32,8 +37,9 @@ public:
}; };
mpvoptions::mpvoptions() { mpvoptions::mpvoptions() {
// TODO: add custom flag options, custom player etc
this->curlu = curl_url(); this->curlu = curl_url();
this->flags = "";
this->player = "mpv";
this->fullscreen = false; this->fullscreen = false;
this->pip = false; this->pip = false;
this->enqueue = false; this->enqueue = false;
@@ -49,13 +55,16 @@ mpvoptions::~mpvoptions() {
string mpvoptions::build_cmd() { string mpvoptions::build_cmd() {
std::ostringstream ret; std::ostringstream ret;
ret << "mpv "; ret << this->player << " ";
if (this->fullscreen) ret << "--fs "; if (this->fullscreen) ret << "--fs ";
if (this->pip) ret << "--ontop --no-border --autofit=384x216 --geometry=98\%:98\% "; 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? // NOTE: this is not needed for mpv (it always opens a new window), maybe for other players?
// if (this->new_window) ret << "--new-window"; // if (this->new_window) ret << "--new-window";
ret << this->url; ret << this->url;
std::cout << ret.str() << std::endl;
return ret.str(); return ret.str();
} }
@@ -109,6 +118,8 @@ void mpvoptions::parse(const char *url) {
string querystr(query); string querystr(query);
curl_free(query); curl_free(query);
this->url = url_decode(query_value(querystr, "url")); 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->fullscreen = query_value(querystr, "fullscreen") == "1";
this->pip = query_value(querystr, "pip") == "1"; this->pip = query_value(querystr, "pip") == "1";
this->enqueue = query_value(querystr, "enqueue") == "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); 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 #endif