diff --git a/Cargo.lock b/Cargo.lock index c23391c..2b5f00f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,12 +2,12 @@ # It is not intended for manual editing. [[package]] name = "autocfg" -version = "0.1.4" +version = "0.1.7" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "bitflags" -version = "1.1.0" +version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] @@ -17,12 +17,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "cc" -version = "1.0.37" +version = "1.0.50" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "cfg-if" -version = "0.1.9" +version = "0.1.10" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] @@ -40,22 +40,22 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "idna" -version = "0.1.5" +version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "matches 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", "unicode-bidi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", - "unicode-normalization 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-normalization 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "lazy_static" -version = "1.3.0" +version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "libc" -version = "0.2.58" +version = "0.2.66" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] @@ -68,49 +68,49 @@ name = "open-in-mpv" version = "0.1.0" dependencies = [ "byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)", - "openssl 0.10.23 (registry+https://github.com/rust-lang/crates.io-index)", - "percent-encoding 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", - "url 1.7.2 (registry+https://github.com/rust-lang/crates.io-index)", + "openssl 0.10.26 (registry+https://github.com/rust-lang/crates.io-index)", + "percent-encoding 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "url 2.1.1 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "openssl" -version = "0.10.23" +version = "0.10.26" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "bitflags 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", - "cfg-if 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", + "bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", "foreign-types 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", - "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", - "libc 0.2.58 (registry+https://github.com/rust-lang/crates.io-index)", - "openssl-sys 0.9.47 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)", + "openssl-sys 0.9.53 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "openssl-sys" -version = "0.9.47" +version = "0.9.53" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "autocfg 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", - "cc 1.0.37 (registry+https://github.com/rust-lang/crates.io-index)", - "libc 0.2.58 (registry+https://github.com/rust-lang/crates.io-index)", - "pkg-config 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", - "vcpkg 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)", + "autocfg 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", + "cc 1.0.50 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)", + "pkg-config 0.3.17 (registry+https://github.com/rust-lang/crates.io-index)", + "vcpkg 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "percent-encoding" -version = "1.0.1" +version = "2.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "pkg-config" -version = "0.3.14" +version = "0.3.17" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "smallvec" -version = "0.6.10" +version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] @@ -123,45 +123,45 @@ dependencies = [ [[package]] name = "unicode-normalization" -version = "0.1.8" +version = "0.1.12" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "smallvec 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)", + "smallvec 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "url" -version = "1.7.2" +version = "2.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "idna 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", + "idna 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "matches 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", - "percent-encoding 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", + "percent-encoding 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "vcpkg" -version = "0.2.7" +version = "0.2.8" source = "registry+https://github.com/rust-lang/crates.io-index" [metadata] -"checksum autocfg 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "0e49efa51329a5fd37e7c79db4621af617cd4e3e5bc224939808d076077077bf" -"checksum bitflags 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3d155346769a6855b86399e9bc3814ab343cd3d62c7e985113d46a0ec3c281fd" +"checksum autocfg 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "1d49d90015b3c36167a20fe2810c5cd875ad504b39cff3d4eae7977e6b7c1cb2" +"checksum bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" "checksum byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a7c3dd8985a7111efc5c80b44e23ecdd8c007de8ade3b96595387e812b957cf5" -"checksum cc 1.0.37 (registry+https://github.com/rust-lang/crates.io-index)" = "39f75544d7bbaf57560d2168f28fd649ff9c76153874db88bdbdfd839b1a7e7d" -"checksum cfg-if 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "b486ce3ccf7ffd79fdeb678eac06a9e6c09fc88d33836340becb8fffe87c5e33" +"checksum cc 1.0.50 (registry+https://github.com/rust-lang/crates.io-index)" = "95e28fa049fda1c330bcf9d723be7663a899c4679724b34c81e9f5a326aab8cd" +"checksum cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)" = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" "checksum foreign-types 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f6f339eb8adc052cd2ca78910fda869aefa38d22d5cb648e6485e4d3fc06f3b1" "checksum foreign-types-shared 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b" -"checksum idna 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "38f09e0f0b1fb55fdee1f17470ad800da77af5186a1a76c026b679358b7e844e" -"checksum lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bc5729f27f159ddd61f4df6228e827e86643d4d3e7c32183cb30a1c08f604a14" -"checksum libc 0.2.58 (registry+https://github.com/rust-lang/crates.io-index)" = "6281b86796ba5e4366000be6e9e18bf35580adf9e63fbe2294aadb587613a319" +"checksum idna 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "02e2673c30ee86b5b96a9cb52ad15718aa1f966f5ab9ad54a8b95d5ca33120a9" +"checksum lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" +"checksum libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)" = "d515b1f41455adea1313a4a2ac8a8a477634fbae63cc6100e3aebb207ce61558" "checksum matches 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "7ffc5c5338469d4d3ea17d269fa8ea3512ad247247c30bd2df69e68309ed0a08" -"checksum openssl 0.10.23 (registry+https://github.com/rust-lang/crates.io-index)" = "97c140cbb82f3b3468193dd14c1b88def39f341f68257f8a7fe8ed9ed3f628a5" -"checksum openssl-sys 0.9.47 (registry+https://github.com/rust-lang/crates.io-index)" = "75bdd6dbbb4958d38e47a1d2348847ad1eb4dc205dc5d37473ae504391865acc" -"checksum percent-encoding 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "31010dd2e1ac33d5b46a5b413495239882813e0369f8ed8a5e266f173602f831" -"checksum pkg-config 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)" = "676e8eb2b1b4c9043511a9b7bea0915320d7e502b0a079fb03f9635a5252b18c" -"checksum smallvec 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)" = "ab606a9c5e214920bb66c458cd7be8ef094f813f20fe77a54cc7dbfff220d4b7" +"checksum openssl 0.10.26 (registry+https://github.com/rust-lang/crates.io-index)" = "3a3cc5799d98e1088141b8e01ff760112bbd9f19d850c124500566ca6901a585" +"checksum openssl-sys 0.9.53 (registry+https://github.com/rust-lang/crates.io-index)" = "465d16ae7fc0e313318f7de5cecf57b2fbe7511fd213978b457e1c96ff46736f" +"checksum percent-encoding 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e" +"checksum pkg-config 0.3.17 (registry+https://github.com/rust-lang/crates.io-index)" = "05da548ad6865900e60eaba7f589cc0783590a92e940c26953ff81ddbab2d677" +"checksum smallvec 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "44e59e0c9fa00817912ae6e4e6e3c4fe04455e75699d06eedc7d85917ed8e8f4" "checksum unicode-bidi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "49f2bd0c6468a8230e1db229cff8029217cf623c767ea5d60bfbd42729ea54d5" -"checksum unicode-normalization 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "141339a08b982d942be2ca06ff8b076563cbe223d1befd5450716790d44e2426" -"checksum url 1.7.2 (registry+https://github.com/rust-lang/crates.io-index)" = "dd4e7c0d531266369519a4aa4f399d748bd37043b00bde1e4ff1f60a120b355a" -"checksum vcpkg 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)" = "33dd455d0f96e90a75803cfeb7f948768c08d70a6de9a8d2362461935698bf95" +"checksum unicode-normalization 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)" = "5479532badd04e128284890390c1e876ef7a993d0570b3597ae43dfa1d59afa4" +"checksum url 2.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "829d4a8476c35c9bf0bbce5a3b23f4106f79728039b726d292bb93bc106787cb" +"checksum vcpkg 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "3fc439f2794e98976c88a2a2dafce96b930fe8010b0a256b3c2199a773933168" diff --git a/Cargo.toml b/Cargo.toml index c2d0de9..a567e6f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,9 +7,9 @@ license = "GPL-3.0-or-later" build = false [dependencies] -percent-encoding = "1.0.1" -url = "1.7.2" +percent-encoding = "2.1.0" +url = "2.1.1" [build-dependencies] byteorder = "1.3.2" -openssl = "0.10.23" \ No newline at end of file +openssl = "0.10.26" \ No newline at end of file diff --git a/Chrome/README.md b/Chrome/README.md index f95060a..f470931 100644 --- a/Chrome/README.md +++ b/Chrome/README.md @@ -1,5 +1,3 @@ # Open In mpv (Chrome) -> WIP - This code is a copy of [Chrome_Open_In_IINA](https://github.com/iina/iina/tree/develop/browser/Chrome_Open_In_IINA) \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..ea3dddf --- /dev/null +++ b/README.md @@ -0,0 +1,9 @@ +# open-in-mpv +This is a simple web extension (for Chrome and Firefox) which helps open any video in the currently open tab in the [mpv player](https://mpv.io). + +The extension itself is a copy of the one from the awesome [iina](https://github.com/iina/iina), while the (bare) backend is written in Rust (any stable version will suffice). + +## Installation +> Compiled binaries and packed extensions can be found in the [releases page](https://github.com/Baldomo/open-in-mpv/releases). + +`open-in-mpv install-protocol` will create a custom `xdg-open` desktop file with a scheme handler for the `mpv://` protocol. This lets `xdg-open` call `open-in-mpv` with an encoded URI, so that it can be parsed and the information can be relayed to `mpv` - this logic follows how `iina` parses and opens custom URIs with the `iina://` protocol on Mac. `install-protocol.sh` has the same functionality. \ No newline at end of file diff --git a/build.rs b/build.rs index d1323c1..17b4ded 100644 --- a/build.rs +++ b/build.rs @@ -11,6 +11,8 @@ use openssl::sign::Signer; type Buffer = Vec; +// TODO: signing for the Chrome extension + fn main() { zip_dir("Chrome", "Chrome/ext.zip"); package_crx("./Chrome/ext.zip", "cert.pem"); diff --git a/src/lib.rs b/src/lib.rs index 5b8b467..ff26aea 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,5 @@ use std::vec::Vec; -use url::percent_encoding::percent_decode; +use percent_encoding::percent_decode; #[derive(Debug)] pub struct MpvOption { @@ -15,9 +15,6 @@ impl MpvOption { /// # Example /// ``` /// use open_in_mpv::MpvOption; - /// # use std::error::Error; - /// - /// # fn run() -> Result<(), Box> { /// let mo: MpvOption = MpvOption::new( /// "url", /// "1", @@ -26,9 +23,6 @@ impl MpvOption { /// ); /// /// print!("{:?}", mo); - /// # Ok(()) - /// # } - /// # run().unwrap(); /// ``` pub fn new(url: &str, fullscreen: &str, pip: &str, enqueue: &str) -> Self { Self { @@ -58,6 +52,7 @@ impl MpvOption { } } + /// Returns a Vec containing the argument list for a new mpv process /// /// Each parameter of MpvOption is bound to a set of defaults, e.g. `--ontop --no-border` etc. @@ -65,9 +60,6 @@ impl MpvOption { /// # Example /// ``` /// use open_in_mpv::MpvOption; - /// # use std::error::Error; - /// - /// # fn run() -> Result<(), Box> { /// let mo: MpvOption = MpvOption::new( /// "url", /// "1", @@ -76,9 +68,6 @@ impl MpvOption { /// ); /// /// print!("{:?}", mo.build_args()); - /// # Ok(()) - /// # } - /// # run().unwrap(); /// ``` pub fn build_args(self) -> Vec { let mut ret: Vec = Vec::new(); @@ -112,7 +101,12 @@ mod tests { let encoded_url: &str = "https%3A%2F%2Fst3x_plus.cdnfile.info%2Fuser592%2F5543369133e06eedecc4907bfcd0fd45%2FEP.1.360p.mp4%3Ftoken%3DZDpvjlchVpTP0yb_5AsaEw%26expires%3D1562085204%26title%3D(360P%2520-%2520mp4)%2520Terminator%2B4%253A%2BSalvation%2BHD-720p"; let expected_url: String = String::from("https://st3x_plus.cdnfile.info/user592/5543369133e06eedecc4907bfcd0fd45/EP.1.360p.mp4?token=ZDpvjlchVpTP0yb_5AsaEw&expires=1562085204&title=(360P%20-%20mp4)%20Terminator+4%3A+Salvation+HD-720p"); - let mo: MpvOption = MpvOption::new(encoded_url, "1", "0", "1"); + let mo: MpvOption = MpvOption::new( + encoded_url, + "1", + "0", + "1", + ); assert_eq!(mo.url, expected_url); assert_eq!(mo.fullscreen, true); diff --git a/src/main.rs b/src/main.rs index 311f1cc..8afa26c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,37 +1,97 @@ use open_in_mpv::MpvOption; -use std::{collections::HashMap, env, process::{Command, exit, Stdio}}; +use std::{env, fs}; +use std::collections::HashMap; +use std::process::{Command, exit, Stdio}; use url::Url; fn main() { // mpv:///open?url=XXXXXXX&full_screen=1&pip=1&enqueue=0 - let raw_url: String = env::args_os().nth(1).unwrap().into_string().unwrap(); - let parsed_url: Url = match Url::parse(&raw_url) { - Err(why) => panic!("{:?}", why), + let raw_arg: String = env::args_os().nth(1).unwrap().into_string().unwrap(); + + // If first argument is "install-protocol", install the protocol file + if raw_arg == "install-protocol" { + install_protocol(); + exit(0); + } + + // Parse the String in raw_arg + let parsed_url: Url = match Url::parse(&raw_arg) { Ok(parsed) => parsed, + Err(e) => { + println!("Error parsing url: {}", raw_arg); + println!("{}", e); + exit(1); + }, }; + // Check if url has the correct protocol format if parsed_url.scheme() != "mpv" { println!("Unsupported protocol: {}", parsed_url.scheme()); exit(1); } + // Check for supported methods of operation (just `open` for now) if parsed_url.path() != "/open" { println!("Unsupported method: {}", parsed_url.path()); exit(1); } + // Convert query into a HashMap let query: HashMap = parsed_url.query_pairs().into_owned().collect(); + if !query.contains_key("url") { + println!("Url cannot be empty!"); + exit(1); + } + + // Build a new MpvOption object + // Note: it's now safe to unwrap all the Option's with fallbacks let mo = MpvOption::new( query.get("url").unwrap(), - query.get("full_screen").unwrap(), - query.get("pip").unwrap(), - query.get("enqueue").unwrap(), + query.get("full_screen") + .as_deref() + .unwrap_or(&"0".to_owned()), + query.get("pip") + .as_deref() + .unwrap_or(&"0".to_owned()), + query.get("enqueue") + .as_deref() + .unwrap_or(&"0".to_owned()), ); + // Spawn a new mpv process Command::new("mpv") .args(mo.build_args()) .stdout(Stdio::null()) .spawn() .expect("failed to open mpv"); + + exit(0); +} + +// Installs the protocol file in "$HOME/.local/share/applications/open-in-mpv.desktop" +fn install_protocol() { + const PROTOCOL_FILE: &str = "[Desktop Entry] +Name=open-in-mpv +Exec=open-in-mpv %u +Type=Application +Terminal=false +NoDisplay=true +MimeType=x-scheme-handler/mpv +"; + + // Fetch $HOME + let mut file_path: String = String::new(); + match env::var("HOME") { + Ok(val) => file_path.push_str(&val), + Err(e) => { + println!("couldn't interpret HOME: {}", e); + return; + }, + } + + // Refine file path + file_path.push_str("/.local/share/applications/open-in-mpv.desktop"); + + fs::write(file_path, PROTOCOL_FILE).expect("Error writing file!"); }