Compare commits
10 Commits
0.2
...
4b51c0e3f8
| Author | SHA1 | Date | |
|---|---|---|---|
| 4b51c0e3f8 | |||
| 463ce6f9c0 | |||
| 39ebeccf9f | |||
| d3ca906f8f | |||
| 439ef97f29 | |||
| a10f4a3ceb | |||
| e11e73d8a6 | |||
| ffcf3ad4b0 | |||
| 1f912360e7 | |||
| fecb5e103e |
91
README.md
91
README.md
@@ -1,9 +1,88 @@
|
|||||||
# xdg-sanity
|
# xdg-sanity
|
||||||
|
|
||||||
This script is built to replace your default web browser in your desktop/XDG
|
The `xdg-sanity` script is built to replace your default web browser in your
|
||||||
settings. It intercepts http/s URIs sent to the default browser by XDG settings
|
desktop/XDG settings. It intercepts http/s URIs sent to the default browser by
|
||||||
and sends it to the appropriate application. For example, it will send
|
`xdg-open` and sends it to the appropriate application. For example, it will
|
||||||
image/jpeg MIME type files to your image viewer.
|
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
|
## Installation
|
||||||
links to it.
|
|
||||||
|
### 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
|
||||||
@@ -51,7 +51,9 @@ done
|
|||||||
if [ "$MIME" = "" ] || [ "$MIME" = "$1" ] || [ "$MIME" = "$INPUT" ]
|
if [ "$MIME" = "" ] || [ "$MIME" = "$1" ] || [ "$MIME" = "$INPUT" ]
|
||||||
then
|
then
|
||||||
echo "Determining MIME type of $INPUT:"
|
echo "Determining MIME type of $INPUT:"
|
||||||
MIME=$(curl -I -s "$INPUT" | sed -ne 's/^[cC]ontent-[tT]ype: //p' | sed -e 's/;.\+//g' | tr -d '\r')
|
# 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
|
echo $MIME
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|||||||
@@ -1,12 +0,0 @@
|
|||||||
#!/bin/sh
|
|
||||||
|
|
||||||
# EXT-TYPE=mime
|
|
||||||
|
|
||||||
TEST=$(echo $1 | sed -ne 's/^h.\+\/\///p' | sed -e 's/\/.*\+//g')
|
|
||||||
|
|
||||||
if [ "$TEST" = "teddit.net" ]
|
|
||||||
then
|
|
||||||
echo "text/html"
|
|
||||||
else
|
|
||||||
echo $1
|
|
||||||
fi
|
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
#!/bin/sh
|
|
||||||
|
|
||||||
# EXT-TYPE=replace
|
|
||||||
|
|
||||||
TEST=$(echo $1 | sed -ne 's/^h.\+\/\///p' | sed -e 's/\/.*\+//g')
|
|
||||||
|
|
||||||
if [ "$TEST" = "youtube.com" ]
|
|
||||||
then
|
|
||||||
echo $1 | sed -ne 's/youtube.com/piped.mint.lgbt/p'
|
|
||||||
else
|
|
||||||
echo $1
|
|
||||||
fi
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
browser =
|
|
||||||
Reference in New Issue
Block a user