Compare commits
15 Commits
0.1
...
4b51c0e3f8
| Author | SHA1 | Date | |
|---|---|---|---|
| 4b51c0e3f8 | |||
| 463ce6f9c0 | |||
| 39ebeccf9f | |||
| d3ca906f8f | |||
| 439ef97f29 | |||
| a10f4a3ceb | |||
| e11e73d8a6 | |||
| ffcf3ad4b0 | |||
| 1f912360e7 | |||
| fecb5e103e | |||
| 9a1c33d0d1 | |||
| 8c9b55bb96 | |||
| bc18380fec | |||
| bce239da30 | |||
| 2cfd97c158 |
91
README.md
91
README.md
@@ -1,9 +1,88 @@
|
||||
# xdg-sanity
|
||||
|
||||
This script is built to replace your default web browser in your desktop/XDG
|
||||
settings. It intercepts http/s URIs sent to the default browser by XDG settings
|
||||
and sends it to the appropriate application. For example, it will send
|
||||
image/jpeg MIME type files to your image viewer.
|
||||
The `xdg-sanity` script is built to replace your default web browser in your
|
||||
desktop/XDG settings. It intercepts http/s URIs sent to the default browser by
|
||||
`xdg-open` and sends it to the appropriate application. For example, it will
|
||||
send `image/jpeg` MIME type files to your image viewer.
|
||||
|
||||
Add your default web browser to `/etc/xdg-sanity.conf` so the script can forward
|
||||
links to it.
|
||||
## Installation
|
||||
|
||||
### Arch Linux
|
||||
|
||||
I maintain a package [on the
|
||||
AUR](https://aur.archlinux.org/packages/xdg-sanity).
|
||||
|
||||
### From Source
|
||||
|
||||
Dependencies:
|
||||
- `curl(1)`
|
||||
- `xdg-utils(1)` or `handlr(1)`
|
||||
- `tomcat(1)`
|
||||
|
||||
You can get `tomcat` [here](https://git.tebibyte.media/emma/tomcat)
|
||||
|
||||
Instructions:
|
||||
Clone this repository and move the `xdg-sanity` binary wherever your operating
|
||||
system stores locally-installed binaries. This is usually `/usr/local/bin` or
|
||||
`$HOME/.local/bin` for your user. Make sure the installation location is in your
|
||||
`$PATH`.
|
||||
|
||||
Create an `xdg-sanity.desktop` file either manually or with `gendesk(1)`,
|
||||
placing it where your OS stores locally-installed `.desktop` files, which is
|
||||
usually `/usr/local/share/applications` or `$XDG_DATA_HOME/applications` for
|
||||
your user. Set your default web browser to that `.desktop` file with
|
||||
`xdg-settings(1)` or an equivalent.
|
||||
|
||||
### Configuration
|
||||
|
||||
This program uses [TOML](https://toml.io/en/v1.0.0) for its configuration. The
|
||||
configuration file is set up like this:
|
||||
```
|
||||
$ cat $XDG_CONFIG_HOME/xdg-sanity.toml
|
||||
[tools]
|
||||
browser = "firefox"
|
||||
xdg = "handlr launch"
|
||||
```
|
||||
|
||||
The options available for `xdg` are `handlr launch` if you use `handlr(1)` and
|
||||
`xdg-open` if you use `xdg-utils(1)`.
|
||||
|
||||
### Usage
|
||||
|
||||
`xdg-sanity [RESOURCE]`
|
||||
|
||||
Open links from applications outside your web browser as normal. Alternatively,
|
||||
you can call `xdg-sanity` directly with the only argument accepted being a URI.
|
||||
|
||||
#### Extensions
|
||||
|
||||
Extensions are written using TOML and are stored in either
|
||||
`/usr/share/xdg-sanity` when installed from a package manager,
|
||||
`/usr/local/share/xdg-sanity` when installed locally, or
|
||||
`$XDG_DATA_HOME/xdg-sanity` for your user.
|
||||
|
||||
There are two types of extensions: MIME and replace. MIME extensions are parsed
|
||||
first and replace the MIME type of the content being fetched. Replace extensions
|
||||
change the URI passed to the command to another.
|
||||
|
||||
The type of the extension depends on the file name. MIME extensions should have
|
||||
a name ending in `-mime.toml` and replace extensions should have
|
||||
`-replace.toml`.
|
||||
|
||||
Here's what a MIME extension looks like:
|
||||
```
|
||||
cat $XDG_DATA_HOME/xdg-sanity/youtube-mime.toml
|
||||
[replace]
|
||||
urls = [ "youtube.com", "youtu.be" ]
|
||||
|
||||
[with]
|
||||
mime = "video/vnd.youtube.yt"
|
||||
```
|
||||
and here's what a replace extension looks like:
|
||||
```
|
||||
cat $XDG_DATA_HOME/xdg-sanity/youtube-replace.toml
|
||||
[replace]
|
||||
urls = [ "youtube.com", "youtu.be" ]
|
||||
|
||||
[with]
|
||||
url = "https://piped.mint.lgbt/"
|
||||
|
||||
7
example-extensions/youtube-mime.toml
Normal file
7
example-extensions/youtube-mime.toml
Normal file
@@ -0,0 +1,7 @@
|
||||
# xdg-sanity mime extension
|
||||
|
||||
[replace] # replaces these urls
|
||||
urls = [ "youtube.com", "youtu.be" ]
|
||||
|
||||
[with] # replaces the mime type of the above
|
||||
mime = "video/vnd.youtube.yt"
|
||||
118
xdg-sanity
Executable file
118
xdg-sanity
Executable file
@@ -0,0 +1,118 @@
|
||||
#!/bin/sh
|
||||
|
||||
set -e
|
||||
|
||||
argv0="$0"
|
||||
|
||||
# grabs configuration files
|
||||
CONFIG="$XDG_CONFIG_HOME"/xdg-sanity.toml
|
||||
if ! test -e "$CONFIG"; then
|
||||
touch "$CONFIG"
|
||||
CONFIG=/etc/xdg-sanity.toml
|
||||
if ! test -e "$CONFIG"; then
|
||||
exit 66 # sysexits(3) EX_NOINPUT
|
||||
fi
|
||||
fi
|
||||
|
||||
|
||||
# check if usage is valid
|
||||
if ! test -n "$1"; then
|
||||
printf "Usage: %s [resource...]\n" "$argv0" 1>&2
|
||||
exit 64 # sysexits(3) EX_USAGE
|
||||
fi
|
||||
|
||||
# check if we have curl(1)
|
||||
if ! command -v curl >/dev/null 2>&1; then
|
||||
printf "%s: Missing dependency: curl(1)\n" "$argv0" 1>&2
|
||||
exit 71 # sysexits(3) EX_OSERR
|
||||
fi
|
||||
|
||||
# check if we have tomcat(1)
|
||||
if ! command -v tomcat >/dev/null 2>&1; then
|
||||
printf "%s: Missing dependency: stoml(1)\n" "$argv0" 1>&2
|
||||
exit 71 # sysexits(3) EX_OSERR
|
||||
fi
|
||||
|
||||
# set the XDG_COMMAND
|
||||
test -n "$XDG_COMMAND" || XDG_COMMAND="$(tomcat tools.xdg $CONFIG)"
|
||||
! command -v handlr >/dev/null 2>&1 && ! command -v xdg-open >/dev/null 2>&1 \
|
||||
&& XDG_COMMAND=false \
|
||||
|| true
|
||||
|
||||
|
||||
# check if we have a BROWSER
|
||||
test -n "$BROWSER" || BROWSER="$(tomcat tools.browser $CONFIG)"
|
||||
if ! test -n "$BROWSER"; then
|
||||
printf "\
|
||||
%s: \$BROWSER not filled.
|
||||
Please place the path to your preferred browser's executable in
|
||||
$XDG_CONFIG_HOME/xdg-sanity.conf or /etc/xdg-sanity.conf
|
||||
" "$argv0" 1>&2
|
||||
exit 71 # sysexits(3) EX_OSERR
|
||||
fi
|
||||
|
||||
while test -n "$1"; do
|
||||
URL="$1"
|
||||
|
||||
# use curl(1) to write out the request header's content_type,
|
||||
# strip everything after the first semicolon,
|
||||
# chop off any weird whitespace remnants
|
||||
MIME="$(curl -Ls -o /dev/null -w '%{content_type}' "$1" | sed 's/\;.*//' |\
|
||||
xargs echo)"
|
||||
|
||||
# get the pattern for the extensions to MATCH
|
||||
MATCH=$(printf "%s\n" "$URL" | sed -ne 's/^h.\+\/\///p' |\
|
||||
sed -e 's/\/.*\+//g')
|
||||
|
||||
# run through MIME extensions
|
||||
for file in \
|
||||
"$XDG_DATA_HOME"/xdg-sanity/*-mime.toml \
|
||||
/usr/share/xdg-sanity/*-mime.toml \
|
||||
/usr/local/share/xdg-sanity/*-mime.toml \
|
||||
/dev/null
|
||||
do
|
||||
i=$(tomcat replace.urls "$file" | sed 's/ /\n/g' | xargs wc -l)
|
||||
while ! [ "$i" = 0 ]; do
|
||||
if [ "$MATCH" = "$(tomcat replace.urls[$i] $file)" ]; then
|
||||
MIME=$(tomcat with.mime "$file")
|
||||
fi
|
||||
i=$(dc -e "$i 1 - p")
|
||||
done
|
||||
done
|
||||
|
||||
# and the replace extensions
|
||||
for file in \
|
||||
"$XDG_DATA_HOME"/xdg-sanity/*-replace.toml \
|
||||
/usr/share/xdg-sanity/*-replace.toml \
|
||||
/usr/local/share/xdg-sanity/*-replace.toml \
|
||||
/dev/null
|
||||
do
|
||||
i=$(tomcat replace.urls "$file" | sed 's/ /\n/g' | xargs wc -l)
|
||||
while ! [ "$i" = 0 ]; do
|
||||
if [ "$MATCH" = "$(tomcat replace.urls[$i] $file)" ]; then
|
||||
URL=$(tomcat with.url "$file")
|
||||
fi
|
||||
i=$(dc -e "$i 1 - p")
|
||||
done
|
||||
done
|
||||
|
||||
# these commands may fail; this is intentional
|
||||
if [ "$MIME" = "text/html" ]; then
|
||||
"$BROWSER" "$URL"
|
||||
else
|
||||
case "$(command -v $XDG_COMMAND)" in
|
||||
*/handlr )
|
||||
"XDG_COMMAND" "$MIME" -- "$URL"
|
||||
;;
|
||||
*/xdg-open )
|
||||
"$(xdg-mime query default $MIME)" "$URL"
|
||||
;;
|
||||
false )
|
||||
exit 69 # sysexits(3) EX_UNAVAILABLE
|
||||
;;
|
||||
esac
|
||||
fi
|
||||
shift
|
||||
done
|
||||
|
||||
exit 0
|
||||
@@ -1 +0,0 @@
|
||||
browser =
|
||||
@@ -1,17 +1,66 @@
|
||||
#!/bin/sh
|
||||
|
||||
echo "Determining MIME type of $1:"
|
||||
INPUT=$(echo $1)
|
||||
echo "Loading configuration..."
|
||||
CONFIG=$(cat /etc/xdg-sanity/xdg-sanity.conf)
|
||||
BROWSER=$(echo $CONFIG | sed -ne 's/^browser *= *//p')
|
||||
|
||||
MIME=$(curl -I -s "$1" | sed -n -e 's/^[cC]ontent-[tT]ype: //p' | sed -e 's/;.\+//g' | tr -d '\r')
|
||||
if [ "$BROWSER" = "" ]
|
||||
then
|
||||
echo "Please place the path to your default browser's executable in /etc/xdg-sanity/xdg-sanity.conf"
|
||||
exit
|
||||
else
|
||||
echo "Found default browser $BROWSER"
|
||||
fi
|
||||
|
||||
echo "Loading extensions..."
|
||||
for EXT in /etc/xdg-sanity/extensions/*.sh
|
||||
do
|
||||
|
||||
if [ "$EXT" = "/etc/xdg-sanity/extensions/*.sh" ]
|
||||
then
|
||||
echo "No extensions to load"
|
||||
else
|
||||
|
||||
for EXT in /etc/xdg-sanity/extensions/*.sh
|
||||
do
|
||||
TYPE=$(cat $EXT | sed -ne 's/^# EXT-TYPE=//p' | tr -d '\n')
|
||||
echo "Found $TYPE extension $EXT"
|
||||
|
||||
if [ "$TYPE" = "replace" ]
|
||||
then
|
||||
echo "Modifying $INPUT..."
|
||||
INPUT=$($EXT "$INPUT")
|
||||
echo "Got $INPUT"
|
||||
|
||||
else
|
||||
if [ "$TYPE" = "mime" ]
|
||||
then
|
||||
echo "Modifying MIME type..."
|
||||
MIME=$($EXT "$INPUT")
|
||||
echo "Got $MIME"
|
||||
|
||||
fi
|
||||
fi
|
||||
|
||||
done
|
||||
fi
|
||||
|
||||
done
|
||||
|
||||
if [ "$MIME" = "" ] || [ "$MIME" = "$1" ] || [ "$MIME" = "$INPUT" ]
|
||||
then
|
||||
echo "Determining MIME type of $INPUT:"
|
||||
# Determines HTTP code, might use for something else?
|
||||
# CODE=$(curl -fLIs "$INPUT" | sed -ne 's/ [[:space:]]*$//p' | sed -ne 's|^HTTP/.\+ ||p')
|
||||
MIME=$(curl -fLIs "$INPUT" | sed -ne 's/^[cC]ontent-[tT]ype: //p' | sed -e 's/;.\+//p' | tail -n1 | tr -d '\r')
|
||||
echo $MIME
|
||||
|
||||
BROWSER=$(cat /etc/xdg-sanity.conf | sed -n -e 's/^browser = //p' )
|
||||
fi
|
||||
|
||||
if [ "$MIME" = "text/html" ]
|
||||
then
|
||||
$BROWSER $1
|
||||
$BROWSER $INPUT
|
||||
|
||||
else
|
||||
handlr launch "$MIME" -- "$1"
|
||||
handlr launch "$MIME" -- "$INPUT"
|
||||
fi
|
||||
Reference in New Issue
Block a user