Compare commits

15 Commits

Author SHA1 Message Date
4b51c0e3f8 more README 2022-11-08 01:38:07 -05:00
463ce6f9c0 oops 2022-11-08 01:17:06 -05:00
39ebeccf9f readme edits 2022-11-08 01:16:23 -05:00
d3ca906f8f rewrite all in one commit lol 2022-11-08 01:05:10 -05:00
439ef97f29 Merge pull request 'Updating README' (#10) from readme into main
Reviewed-on: #10
2022-09-22 02:05:15 +00:00
a10f4a3ceb added more readme info 2022-09-21 22:03:02 -04:00
e11e73d8a6 updated README.md 2022-09-07 22:06:14 -04:00
ffcf3ad4b0 Merge pull request 'Add redirect handling' (#9) from redirects into main
Reviewed-on: #9
2022-09-06 01:36:39 +00:00
1f912360e7 added redirect handling 2022-09-05 21:33:31 -04:00
fecb5e103e Merge pull request 'Implement Extensions' (#5) from extensions into main
Reviewed-on: #5
2022-09-05 01:05:10 +00:00
9a1c33d0d1 complete refactor 2022-09-04 20:04:31 -04:00
8c9b55bb96 typo 2022-09-04 20:04:31 -04:00
bc18380fec added mime type-changing extension type 2022-09-04 20:04:31 -04:00
bce239da30 changed extensions dir 2022-09-04 20:04:31 -04:00
2cfd97c158 added extensions 2022-09-04 20:04:22 -04:00
5 changed files with 265 additions and 13 deletions

View File

@@ -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/"

View 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
View 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

View File

@@ -1 +0,0 @@
browser =

View File

@@ -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