literate-dotfiles/dotfiles/qutebrowser.org

540 lines
16 KiB
Org Mode

#+TITLE: Qutebrowser Configuration
:DRAWER:
#+STARTUP: overview
#+LANGUAGE: en
#+EMAIL: dehaeze.thomas@gmail.com
#+AUTHOR: Dehaeze Thomas
#+HTML_LINK_HOME: ./index.html
#+HTML_LINK_UP: ./index.html
#+HTML_HEAD: <link rel="stylesheet" type="text/css" href="./css/htmlize.css"/>
#+HTML_HEAD: <link rel="stylesheet" type="text/css" href="./css/readtheorg.css"/>
#+HTML_HEAD: <script type="text/javascript" src="./js/jquery.min.js"></script>
#+HTML_HEAD: <script type="text/javascript" src="./js/bootstrap.min.js"></script>
#+HTML_HEAD: <script type="text/javascript" src="./js/jquery.stickytableheaders.min.js"></script>
#+HTML_HEAD: <script type="text/javascript" src="./js/readtheorg.js"></script>
#+PROPERTY: header-args+ :comments none
#+PROPERTY: header-args+ :mkdirp yes
#+PROPERTY: header-args+ :tangle ~/.config/qutebrowser/config.py
:END:
* Import
#+begin_src python
from qutebrowser.config.configfiles import ConfigAPI
from qutebrowser.config.config import ConfigContainer
import sys, os
#+end_src
* General config
#+begin_src python
c.aliases = {'w': 'session-save', 'q': 'quit', 'wq': 'quit --save'}
c.auto_save.session = False
c.backend = 'webengine'
c.confirm_quit = ['downloads']
#+END_SRC
* Content
#+begin_src python
c.content.autoplay = False
c.content.notifications = False
c.content.geolocation = 'ask'
c.content.javascript.alert = True
c.content.javascript.can_access_clipboard = True
c.content.media_capture = 'ask'
c.content.pdfjs = False
c.content.proxy = 'system'
#+END_SRC
* Editor
#+begin_src python
c.editor.command = ['/usr/bin/termite', '--class="Floating"', '-e', 'nvim {}']
#+end_src
* Downloads
#+begin_src python
c.downloads.location.directory = '$HOME/Downloads/'
c.downloads.location.prompt = True
c.downloads.location.remember = True
c.downloads.location.suggestion = 'path'
c.downloads.position = 'bottom'
c.downloads.remove_finished = 10000
#+END_SRC
* Fonts
#+begin_src python
c.fonts.default_family = ["Hack Nerd Font Mono", "DejaVu Sans Mono", "Monaco"]
c.fonts.prompts = '10pt monospace'
c.fonts.statusbar = '10pt monospace'
c.fonts.tabs = '10pt monospace'
#+END_SRC
* Input
#+begin_src python
c.input.insert_mode.auto_enter = True
c.input.insert_mode.auto_leave = True
c.input.insert_mode.auto_load = False
c.input.insert_mode.plugins = True
#+END_SRC
* New Instance Options
#+begin_src python
c.new_instance_open_target = 'tab'
c.new_instance_open_target_window = 'last-focused'
#+END_SRC
* Spell Check
#+begin_src python
c.spellcheck.languages = ['en-US', 'fr-FR']
#+END_SRC
* Status Bar
#+begin_src python
# Hide the statusbar unless a message is shown.
c.statusbar.hide = False
c.statusbar.position = 'bottom'
# List of widgets displayed in the statusbar.
# Valid values:
# - url: Current page URL.
# - scroll: Percentage of the current page position like `10%`.
# - scroll_raw: Raw percentage of the current page position like `10`.
# - history: Display an arrow when possible to go back/forward in history.
# - tabs: Current active tab, e.g. `2`.
# - keypress: Display pressed keys when composing a vi command.
# - progress: Progress bar for the current page loading.
c.statusbar.widgets = ['keypress', 'url', 'scroll', 'history', 'progress']
#+END_SRC
* Tabs
#+begin_src python
# Open new tabs (middleclick/ctrl+click) in the background.
c.tabs.background = True
# Mouse button with which to close tabs.
c.tabs.close_mouse_button = 'right'
# How to behave when the last tab is closed.
c.tabs.last_close = 'close'
# Switch between tabs using the mouse wheel.
c.tabs.mousewheel_switching = False
# Position of new tabs opened from another tab.
# Valid values:
# - prev: Before the current tab.
# - next: After the current tab.
# - first: At the beginning.
# - last: At the end.
c.tabs.new_position.related = 'next'
# Position of new tabs which aren't opened from another tab.
# Valid values:
# - prev: Before the current tab.
# - next: After the current tab.
# - first: At the beginning.
# - last: At the end.
c.tabs.new_position.unrelated = 'last'
# Position of the tab bar.
c.tabs.position = 'left'
# Which tab to select when the focused tab is removed.
c.tabs.select_on_remove = 'next'
# Width (in pixels or as percentage of the window) of the tab bar if it's vertical.
c.tabs.width = 30
# Wrap when changing tabs.
c.tabs.wrap = False
#+END_SRC
* Urls and Search Engines
#+begin_src python
c.url.searchengines = {
'DEFAULT': 'https://www.duckduckgo.org/?q={}',
'aw': 'https://wiki.archlinux.org/?search={}',
'wi': 'https://en.wikipedia.org/wiki/Special:Search?search={}',
'go': 'https://www.google.com/search?q={}',
'gc': 'https://scholar.google.fr/scholar?hl=fr&as_sdt=0%2C5&q={}&btnG=',
'gm': 'https://www.google.com/maps/search/{}/',
'yt': 'https://www.youtube.com/results?search_query={}',
'gh': 'https://github.com/search?q={}',
're': 'https://www.reddit.com/search?q={}',
'lb': 'http://188.240.208.184/search.php?req={}',
'la': 'http://188.240.208.184/scimag/index.php?s={}',
'sm': 'https://www.openstreetmap.org/search?query={}',
'am': 'https://www.amazon.fr/s?k={}',
'md': 'https://fr.mathworks.com/help/search.html?qdoc={}&submitsearch=',
}
#+end_src
#+begin_src python
c.url.start_pages = ['qute://bookmarks']
#+END_SRC
* Normal Bindings
Zooming
#+begin_src python
config.bind('+', 'zoom-in')
config.bind('-', 'zoom-out')
config.bind('=', 'zoom')
#+end_src
Inputs blocks
#+begin_src python
config.bind(';t', 'hint inputs')
config.bind('gi', 'hint inputs --first')
#+end_src
Opening New Window
#+begin_src python
config.bind('<Ctrl-N>', 'open -w')
#+end_src
History
#+begin_src python
config.bind('H', 'back')
config.bind('L', 'forward')
#+end_src
Move tabs around
#+begin_src python
config.bind('d', 'tab-close')
config.bind('<', 'tab-move -')
config.bind('>', 'tab-move +')
config.bind('gO', 'tab-give') # Open current tab in a new window
config.bind('J', 'tab-next')
config.bind('K', 'tab-prev')
#+end_src
Bookmarks and Quickmark
#+begin_src python
config.bind('M', 'bookmark-add')
config.bind('m', 'quickmark-save')
#+end_src
Open New Pages/Tabs
#+begin_src python
config.bind('o', 'set-cmd-text -s :open')
config.bind('O', 'set-cmd-text -s :open -t')
#+end_src
Open new page/tab with clipboard content
#+begin_src python
config.bind('pp', 'open -- {clipboard}')
config.bind('pP', 'open -t -- {clipboard}')
#+end_src
Link Hinting
#+begin_src python
config.bind('f', 'hint')
config.bind('F', 'hint all tab')
#+end_src
Yanking url
#+begin_src python
config.bind('yy', 'yank')
#+END_SRC
Go to specific websites
#+begin_src python
config.bind('gy', 'open -t https://www.youtube.com/feed/subscriptions')
config.bind('gf', 'open -t https://feedly.com/i/my')
config.bind('gr', 'open -t https://www.reddit.com/')
config.bind('gb', 'open qute://bookmarks')
config.bind('gh', 'open qute://history')
config.bind('gs', 'open qute://settings')
#+end_src
* Custom Bindings
Create a Password for the current website
#+begin_src python
config.bind(',P', 'spawn --userscript ~/.config/qutebrowser/userscripts/add-passowrd.sh')
#+end_src
Use Pass to fill password and username (configuration is done in =~/.config/qutebrowser/password_fill_rc=)
#+begin_src python
config.bind(',p', 'spawn --userscript password_fill')
#+END_SRC
Open Youtube video using =mpv=
#+begin_src python
config.bind(',m', 'spawn --detach mpv --force-window yes {url}')
config.bind(',M', 'hint links spawn --detach mpv --force-window yes {hint-url}')
#+END_SRC
Download Youtube video / entire playlist
#+begin_src python
config.bind(',v', 'spawn --userscript ~/.config/qutebrowser/userscripts/yt-download.sh {url}')
config.bind(',V', 'hint links spawn --userscript ~/.config/qutebrowser/userscripts/yt-download.sh {hint-url}')
#+END_SRC
Add page to Bookmarks using =buku=
#+begin_src python
config.bind(',b', 'spawn --userscript ~/.config/qutebrowser/userscripts/buku-add.sh')
config.bind(',B', 'spawn --userscript ~/.config/qutebrowser/userscripts/buku-rofi.sh')
#+end_src
Open bookmark using =buku=
#+begin_src python
config.bind(',o', 'spawn ~/.local/bin/bukurun')
#+end_src
Org Capture
#+begin_src python
config.bind(',r', 'spawn --userscript ~/.config/qutebrowser/userscripts/org-capture.sh')
config.bind(',R', 'hint links userscript ~/.config/qutebrowser/userscripts/org-capture.sh')
#+end_src
Download a torrent
#+begin_src python
config.bind(',t', 'hint links spawn torrent-add {hint-url}')
#+end_src
Annotate with Hypothesis
#+begin_src python
config.bind(',a', "jseval javascript:(function(){window.hypothesisConfig=function(){return{showHighlights:true,appType:'bookmarklet'};};var d=document,s=d.createElement('script');s.setAttribute('src','https://hypothes.is/embed.js');d.body.appendChild(s)})();")
#+end_src
See Amazon price history using CamelCamelCamel
#+begin_src python
config.bind(',A', ":open -t https://fr.camelcamelcamel.com/search?sq={url}")
#+end_src
* Userscripts
:PROPERTIES:
:header-args:bash: :shebang "#!/usr/bin/env bash"
:header-args:bash+: :comments both :mkdirp yes
:END:
** Create a new password
#+begin_src bash :tangle ~/.config/qutebrowser/userscripts/add-passowrd.sh
url=$(echo "$QUTE_URL" | awk -F[/:] '{print $4}' | rofi -p "URL" -dmenu -lines 1)
username=$(echo -e "dehaeze.thomas@gmail.com\nthomas.dehaeze@esrf.fr\ntdehaeze" | rofi -p "Username" -dmenu -lines 5)
password=$(rofi -p "Password" -dmenu -password -lines 1)
if [ -z "$url" ] || [ -z "$username" ] || [ -z "$password" ]; then
dunstify --urgency=critical "Pass" "Failed to Add Password"
else
echo -e "$password\nlogin: $username\nurl: $QUTE_URL" > /tmp/add-password.txt
pass insert --multiline "$url/$username" < /tmp/add-password.txt;
rm /tmp/add-password.txt
dunstify "Pass " "Password Added"
fi
#+end_src
** Download Youtube Video
#+begin_src bash :tangle ~/.config/qutebrowser/userscripts/yt-download.sh
cd ~/Documents/to-watch/;
if [[ "$1" == *"list"* ]]; then
choice=$(echo -e "Video\nPlaylist" | rofi -dmenu -only-match -i)
fi
if [ -n "$choice" ] && [ "$choice" = "Playlist" ]; then
dunstify --replace=19243 "Youtube " "Downloading Playlist...";
youtube-dl -i -f 'bestvideo[height<=720]+bestaudio/best[height<=720]' "$1" -o "%(playlist_title)s/%(playlist_index)s-%(title)s.%(ext)s" && \
dunstify --replace=19243 "Youtube " "Downloaded" || \
dunstify --replace=19243 --urgency=critical "Youtube " "Failed to download"
else
dunstify --replace=19243 "Youtube " "Downloading Video...";
youtube-dl --no-playlist -f 'bestvideo[height<=720]+bestaudio/best[height<=720]' "$1" && \
dunstify --replace=19243 "Youtube " "Downloaded" || \
dunstify --replace=19243 --urgency=critical "Youtube " "Failed to download"
fi
#+end_src
** Password_fill_rc
#+begin_src bash :tangle ~/.config/qutebrowser/password_fill_rc
# Show all password fields in the menu
query_entries() {
# safe queried url for choose_entry
# the subdomains are removed
export queried_url=$(expr match ".$1" '.*\.\(.*\..*\)')
mapfile -t files < <(find -L "$PREFIX" -iname '*.gpg' -printf '%P\n' |sed 's,\.gpg$,,')
}
# Even if there is only one entry, always show a menu
# for user confirmation.
choose_entry() {
MENU_COMMAND=(
rofi -dmenu
-p "qutebrowser> "
-filter "$queried_url"
-mesg $'Pick a password entry for <b>'"${QUTE_URL//&/&amp;}"'</b>'
)
file=$( printf "%s\n" "${files[@]}" | "${MENU_COMMAND[@]}" )
}
#+end_src
** Org-Capture
#+begin_src bash :tangle ~/.config/qutebrowser/userscripts/org-capture.sh
readonly CAPTURE_SCRIPT=~/.config/qutebrowser/userscripts/org-protocol-capture-html.sh
if [[ "$QUTE_MODE" = "hints" ]]; then
# if we start with hints, we juste want to capture the URL
$CAPTURE_SCRIPT --template "pu" --url "${QUTE_URL}"
elif [[ -n "$QUTE_SELECTED_TEXT" ]]; then
# if text is selected, we want to capture the text
$CAPTURE_SCRIPT --template "pt" --heading "${QUTE_TITLE}" --url "${QUTE_URL}" "${QUTE_SELECTED_TEXT}"
else
# if no text is selected, we want to capture the url
$CAPTURE_SCRIPT --template "pu" --heading "${QUTE_TITLE}" --url "${QUTE_URL}"
fi
#+end_src
** Org-Protocol-Capture-HTML
:PROPERTIES:
:header-args: :tangle ~/.config/qutebrowser/userscripts/org-protocol-capture-html.sh
:END:
Defaults
#+begin_src bash
heading="link"
template="pu"
url="https://google.com/"
#+end_src
Functions
#+begin_src bash
function debug {
if [[ -n $debug ]]
then
function debug {
echo "DEBUG: $@" >&2
}
debug "$@"
else
function debug {
true
}
fi
}
function die {
echo "$@" >&2
exit 1
}
function urlencode {
python -c "
from __future__ import print_function
try:
from urllib import quote # Python 2
except ImportError:
from urllib.parse import quote # Python 3
import sys
print(quote(sys.stdin.read()[:-1], safe=''))"
}
#+end_src
Documentation
#+begin_src bash
function usage {
cat <<EOF
$0 [OPTIONS] [HTML]
html | $0 [OPTIONS]
Send HTML to Emacs through org-protocol, passing it through Pandoc to
convert HTML to Org-mode. HTML may be passed as an argument or
through STDIN. If only URL is given, it will be downloaded and its
contents used.
Options:
-h, --heading HEADING Heading
-t, --template TEMPLATE org-capture template key (default: pu)
-u, --url URL URL
--debug Print debug info
--help I need somebody!
EOF
}
#+end_src
Arguments
#+begin_src bash
args=$(getopt -n "$0" -o dh:rt:u: -l debug,help,heading:,template:,url: -- "$@") \
|| die "Unable to parse args. Is getopt installed?"
eval set -- "$args"
while true
do
case "$1" in
-d|--debug)
debug=true
debug "Debugging on"
;;
--help)
usage
exit
;;
-h|--heading)
shift
heading="$1"
;;
-t|--template)
shift
template="$1"
;;
-u|--url)
shift
url="$1"
;;
--)
# Remaining args
shift
rest=("$@")
break
;;
esac
shift
done
debug "ARGS: $args"
debug "Remaining args: ${rest[@]}"
#+end_src
Get HTML
#+begin_src bash
if [[ -n $@ ]]
then
debug "Text from args"
body="$@"
fi
#+end_src
URL-encode
#+begin_src bash
heading=$(urlencode <<<"$heading") || die "Unable to urlencode heading."
url=$(urlencode <<<"$url") || die "Unable to urlencode URL."
body=$(urlencode <<<"$body") || die "Unable to urlencode text."
#+end_src
Send to Emacs
#+begin_src bash
emacsclient "org-protocol://capture?template=$template&url=$url&title=$heading&body=$body"
#+end_src
** Add Url to Buku using Rofi
#+begin_src bash :tangle ~/.config/qutebrowser/userscripts/buku-rofi.sh
title=$(echo "$QUTE_TITLE" | rofi -p "Title" -dmenu -lines 1)
tags=$(buku -t --nc --np | sed -e 's/\s*[[:digit:]]*\.\s*\(.*\)\s*([[:digit:]]*)\s*/\1/' -e '/^\s*$/d' | sort | uniq | rofi -multi-select -p "Tags" -dmenu | tr "\n" "," | sed 's/\s*,\s*$//')
buku --add "$QUTE_URL" --tag "$tags" --title "$title" && \
dunstify "Buku" "Bookmark Added" || \
dunstify --urgency=critical "Buku" "Bookmark Added" || \
#+end_src
** Add Url to Buku without asking for information
#+begin_src bash :tangle ~/.config/qutebrowser/userscripts/buku-add.sh
buku --add $QUTE_URL --title "$QUTE_TITLE" && dunstify "Buku" "📑 Bookmark Added"
#+end_src