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:
@@ -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,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@@ -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": {
|
||||
|
@@ -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);
|
||||
|
@@ -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,
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@@ -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"
|
||||
|
7
Makefile
7
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
|
||||
@rm open-in-mpv Firefox.zip
|
@@ -8,12 +8,17 @@
|
||||
#include <string>
|
||||
using std::string;
|
||||
|
||||
#include <iostream>
|
||||
|
||||
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
|
Reference in New Issue
Block a user