diff options
| author | TheSiahxyz <164138827+TheSiahxyz@users.noreply.github.com> | 2025-02-10 08:24:09 +0900 |
|---|---|---|
| committer | TheSiahxyz <164138827+TheSiahxyz@users.noreply.github.com> | 2025-02-10 08:24:09 +0900 |
| commit | 8db391ce6ca07ec78780c782a204abee357d73d0 (patch) | |
| tree | 97df2788261b86c9e2cb1bb45116ebc51c55af71 /ar/.local/bin | |
| parent | 9a1d363908228a4d9fffec191aff106598a6ac27 (diff) | |
modified qutebrowser/config.py, modified bin/bookmarks, created greasemonkey/, created bin/umpv
Diffstat (limited to 'ar/.local/bin')
| -rwxr-xr-x | ar/.local/bin/bookmarks | 7 | ||||
| -rwxr-xr-x | ar/.local/bin/umpv | 103 |
2 files changed, 107 insertions, 3 deletions
diff --git a/ar/.local/bin/bookmarks b/ar/.local/bin/bookmarks index 306f47d..c365095 100755 --- a/ar/.local/bin/bookmarks +++ b/ar/.local/bin/bookmarks @@ -75,19 +75,19 @@ openwindow() { openinbrowser() { # Extract only the default part of the profile name case $BROWSER in - firefox) + *firefox*) profiles_ini_path="$HOME/.mozilla/firefox/profiles.ini" profile=$(awk '/\[Install/ {found=1} found && /^Default=/ {split($0, arr, "."); print arr[2]; exit}' "$profiles_ini_path") profile_dir=$(find ~/.mozilla/firefox -type d -name "*.$profile*" | head -n 1) db_path="$profile_dir/places.sqlite" ;; - librewolf) + *librewolf*) profiles_ini_path="$HOME/.librewolf/profiles.ini" profile=$(awk '/\[Install/ {found=1} found && /^Default=/ {split($0, arr, "."); print arr[2]; exit}' "$profiles_ini_path") profile_dir=$(find ~/.librewolf -type d -name "*.$profile*" | head -n 1) db_path="$profile_dir/places.sqlite" ;; - qutebrowser) + *qutebrowser*) profile_dir="${XDG_DATA_HOME:-${HOME}/.local/share}/qutebrowser" db_path="$profile_dir/history.sqlite" ;; @@ -115,6 +115,7 @@ openinbrowser() { ORDER BY b.title; " ;; + *qutebrowser*) geturls && openwindow && exit ;; esac ;; *history*) diff --git a/ar/.local/bin/umpv b/ar/.local/bin/umpv new file mode 100755 index 0000000..da4a767 --- /dev/null +++ b/ar/.local/bin/umpv @@ -0,0 +1,103 @@ +#!/usr/bin/env python3 + +""" +This script emulates "unique application" functionality. When starting +playback with this script, it will try to reuse an already running instance of +mpv (but only if that was started with umpv). Other mpv instances (not started +by umpv) are ignored, and the script doesn't know about them. + +This only takes filenames as arguments. Custom options can't be used; the script +interprets them as filenames. If mpv is already running, the files passed to +umpv are appended to mpv's internal playlist. If a file does not exist or is +otherwise not playable, mpv will skip the playlist entry when attempting to +play it (from the GUI perspective, it's silently ignored). + +If mpv isn't running yet, this script will start mpv and let it control the +current terminal. It will not write output to stdout/stderr, because this +will typically just fill ~/.xsession-errors with garbage. + +mpv will terminate if there are no more files to play, and running the umpv +script after that will start a new mpv instance. + +Note: you can supply custom mpv path and options with the MPV environment + variable. The environment variable will be split on whitespace, and the + first item is used as path to mpv binary and the rest is passed as options + _if_ the script starts mpv. If mpv is not started by the script (i.e. mpv + is already running), this will be ignored. +""" + +import os +import shlex +import socket +import string +import subprocess +import sys +from collections.abc import Iterable +from typing import BinaryIO + + +def is_url(filename: str) -> bool: + parts = filename.split("://", 1) + if len(parts) < 2: + return False + # protocol prefix has no special characters => it's an URL + allowed_symbols = string.ascii_letters + string.digits + "_" + prefix = parts[0] + return all(c in allowed_symbols for c in prefix) + +def get_socket_path() -> str: + if os.name == "nt": + return r"\\.\pipe\umpv" + + base_dir = ( + os.getenv("UMPV_SOCKET_DIR") or + os.getenv("XDG_RUNTIME_DIR") or + os.getenv("HOME") or + os.getenv("TMPDIR") + ) + + if not base_dir: + raise Exception("Could not determine a base directory for the socket. " + "Ensure that one of the following environment variables is set: " + "UMPV_SOCKET_DIR, XDG_RUNTIME_DIR, HOME or TMPDIR.") + + return os.path.join(base_dir, ".umpv") + +def send_files_to_mpv(conn: socket.socket | BinaryIO, files: Iterable[str]) -> None: + try: + send = conn.send if isinstance(conn, socket.socket) else conn.write + for f in files: + f = f.replace("\\", "\\\\").replace('"', '\\"').replace("\n", "\\n") + send(f'raw loadfile "{f}" append-play\n'.encode()) + except Exception: + print("mpv is terminating or the connection was lost.", file=sys.stderr) + sys.exit(1) + +def start_mpv(files: Iterable[str], socket_path: str) -> None: + mpv = "mpv" if os.name != "nt" else "mpv.exe" + mpv_command = shlex.split(os.getenv("MPV", mpv)) + mpv_command.extend([ + "--profile=builtin-pseudo-gui", + f"--input-ipc-server={socket_path}", + "--", + ]) + mpv_command.extend(files) + subprocess.Popen(mpv_command, start_new_session=True) + +def main() -> None: + files = (os.path.abspath(f) if not is_url(f) else f for f in sys.argv[1:]) + socket_path = get_socket_path() + + try: + if os.name == "nt": + with open(socket_path, "r+b", buffering=0) as pipe: + send_files_to_mpv(pipe, files) + else: + with socket.socket(socket.AF_UNIX) as sock: + sock.connect(socket_path) + send_files_to_mpv(sock, files) + except (FileNotFoundError, ConnectionRefusedError): + start_mpv(files, socket_path) + +if __name__ == "__main__": + main() |
