diff options
Diffstat (limited to '.config/qutebrowser/misc/userscripts')
17 files changed, 1971 insertions, 0 deletions
diff --git a/.config/qutebrowser/misc/userscripts/cast b/.config/qutebrowser/misc/userscripts/cast new file mode 100755 index 0000000..f7b64df --- /dev/null +++ b/.config/qutebrowser/misc/userscripts/cast @@ -0,0 +1,156 @@ +#!/usr/bin/env bash +# +# Behaviour +# Userscript for qutebrowser which casts the url passed in $1 to the default +# ChromeCast device in the network using the program `castnow` +# +# Usage +# You can launch the script from qutebrowser as follows: +# spawn --userscript ${PATH_TO_FILE} {url} +# +# Then, you can control the chromecast by launching the simple command +# `castnow` in a shell which will connect to the running castnow instance. +# +# For stopping the script, issue the command `pkill -f castnow` which would +# then let the rest of the userscript execute for cleaning temporary file. +# +# Thanks +# This userscript borrows Thorsten Wißmann's javascript code from his `mpv` +# userscript. +# +# Dependencies +# - castnow, https://github.com/xat/castnow +# +# Author +# Simon Désaulniers <sim.desaulniers@gmail.com> + +if [ -z "$QUTE_FIFO" ] ; then + cat 1>&2 <<EOF +Error: $0 can not be run as a standalone script. + +It is a qutebrowser userscript. In order to use it, call it using +'spawn --userscript' as described in qute://help/userscripts.html +EOF + exit 1 +fi + +msg() { + local cmd="$1" + shift + local msg="$*" + if [ -z "$QUTE_FIFO" ] ; then + echo "$cmd: $msg" >&2 + else + echo "message-$cmd '${msg//\'/\\\'}'" >> "$QUTE_FIFO" + fi +} + +js() { +cat <<EOF + + function descendantOfTagName(child, ancestorTagName) { + // tells whether child has some (proper) ancestor + // with the tag name ancestorTagName + while (child.parentNode != null) { + child = child.parentNode; + if (typeof child.tagName === 'undefined') break; + if (child.tagName.toUpperCase() == ancestorTagName.toUpperCase()) { + return true; + } + } + return false; + } + + var App = {}; + + var all_videos = []; + all_videos.push.apply(all_videos, document.getElementsByTagName("video")); + all_videos.push.apply(all_videos, document.getElementsByTagName("object")); + all_videos.push.apply(all_videos, document.getElementsByTagName("embed")); + App.backup_videos = Array(); + App.all_replacements = Array(); + for (i = 0; i < all_videos.length; i++) { + var video = all_videos[i]; + if (descendantOfTagName(video, "object")) { + // skip tags that are contained in an object, because we hide + // the object anyway. + continue; + } + var replacement = document.createElement("div"); + replacement.innerHTML = " + <p style=\\"margin-bottom: 0.5em\\"> + The video is being cast on your ChromeCast device. + </p> + <p> + In order to restore this particular video + <a style=\\"font-weight: bold; + color: white; + background: transparent; + \\" + onClick=\\"restore_video(this, " + i + ");\\" + href=\\"javascript: restore_video(this, " + i + ")\\" + >click here</a>. + </p> + "; + replacement.style.position = "relative"; + replacement.style.zIndex = "100003000000"; + replacement.style.fontSize = "1rem"; + replacement.style.textAlign = "center"; + replacement.style.verticalAlign = "middle"; + replacement.style.height = "100%"; + replacement.style.background = "#101010"; + replacement.style.color = "white"; + replacement.style.border = "4px dashed #545454"; + replacement.style.padding = "2em"; + replacement.style.margin = "auto"; + App.all_replacements[i] = replacement; + App.backup_videos[i] = video; + video.parentNode.replaceChild(replacement, video); + } + + function restore_video(obj, index) { + obj = App.all_replacements[index]; + video = App.backup_videos[index]; + console.log(video); + obj.parentNode.replaceChild(video, obj); + } + + /** force repainting the video, thanks to: + * http://martinwolf.org/2014/06/10/force-repaint-of-an-element-with-javascript/ + */ + var siteHeader = document.getElementById('header'); + siteHeader.style.display='none'; + siteHeader.offsetHeight; // no need to store this anywhere, the reference is enough + siteHeader.style.display='block'; + +EOF +} + +printjs() { + js | sed 's,//.*$,,' | tr '\n' ' ' +} +echo "jseval -q $(printjs)" >> "$QUTE_FIFO" + +tmpdir=$(mktemp -d) +file_to_cast=${tmpdir}/qutecast +program_=$(command -v castnow) + +if [[ "${program_}" == "" ]]; then + msg error "castnow can't be found..." + exit 1 +fi + +# kill any running instance of castnow +pkill -f "${program_}" + +# start youtube download in stream mode (-o -) into temporary file +youtube-dl -qo - "$1" > "${file_to_cast}" & +ytdl_pid=$! + +msg info "Casting $1" >> "$QUTE_FIFO" +# start castnow in stream mode to cast on ChromeCast +tail -F "${file_to_cast}" | ${program_} - + +# cleanup remaining background process and file on disk +kill ${ytdl_pid} +rm -rf "${tmpdir}" diff --git a/.config/qutebrowser/misc/userscripts/dmenu_qutebrowser b/.config/qutebrowser/misc/userscripts/dmenu_qutebrowser new file mode 100755 index 0000000..82e6d2f --- /dev/null +++ b/.config/qutebrowser/misc/userscripts/dmenu_qutebrowser @@ -0,0 +1,48 @@ +#!/usr/bin/env bash + +# Copyright 2015 Zach-Button <zachrey.button@gmail.com> +# Copyright 2015-2017 Florian Bruhin (The Compiler) <mail@qutebrowser.org> +# +# This file is part of qutebrowser. +# +# qutebrowser is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# qutebrowser is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with qutebrowser. If not, see <http://www.gnu.org/licenses/>. + +# Pipes history, quickmarks, and URL into dmenu. +# +# If run from qutebrowser as a userscript, it runs :open on the URL +# If not, it opens a new qutebrowser window at the URL +# +# Ideal for use with tabs_are_windows. Set a hotkey to launch this script, then: +# :bind o spawn --userscript dmenu_qutebrowser +# +# Use the hotkey to open in new tab/window, press 'o' to open URL in current tab/window +# You can simulate "go" by pressing "o<tab>", as the current URL is always first in the list +# +# I personally use "<Mod4>o" to launch this script. For me, my workflow is: +# Default keys Keys with this script +# O <Mod4>o +# o o +# go o<Tab> +# gO gC, then o<Tab> +# (This is unnecessarily long. I use this rarely, feel free to make this script accept parameters.) +# + +[ -z "$QUTE_URL" ] && QUTE_URL='http://google.com' + +url=$(echo "$QUTE_URL" | cat - "$QUTE_CONFIG_DIR/quickmarks" "$QUTE_DATA_DIR/history" | dmenu -l 15 -p qutebrowser) +url=$(echo "$url" | sed -E 's/[^ ]+ +//g' | grep -E "https?:" || echo "$url") + +[ -z "${url// }" ] && exit + +echo "open $url" >> "$QUTE_FIFO" || qutebrowser "$url" diff --git a/.config/qutebrowser/misc/userscripts/format_json b/.config/qutebrowser/misc/userscripts/format_json new file mode 100755 index 0000000..0d476b3 --- /dev/null +++ b/.config/qutebrowser/misc/userscripts/format_json @@ -0,0 +1,42 @@ +#!/bin/sh +set -euo pipefail +# +# Behavior: +# Userscript for qutebrowser which will take the raw JSON text of the current +# page, format it using `jq`, will add syntax highlighting using `pygments`, +# and open the syntax highlighted pretty printed html in a new tab. If the file +# is larger than 10MB then this script will only indent the json and will forego +# syntax highlighting using pygments. +# +# In order to use this script, just start it using `spawn --userscript` from +# qutebrowser. I recommend using an alias, e.g. put this in the +# [alias]-section of qutebrowser.conf: +# +# json = spawn --userscript /path/to/json_format +# +# Note that the color style defaults to monokai, but a different pygments style +# can be passed as the first parameter to the script. A full list of the pygments +# styles can be found at: https://help.farbox.com/pygments.html +# +# Bryan Gilbert, 2017 + +# do not run pygmentize on files larger than this amount of bytes +MAX_SIZE_PRETTIFY=10485760 # 10 MB +# default style to monokai if none is provided +STYLE=${1:-monokai} + +TEMP_FILE="$(mktemp)" +jq . "$QUTE_TEXT" >"$TEMP_FILE" + +# try GNU stat first and then OSX stat if the former fails +FILE_SIZE=$( + stat --printf="%s" "$TEMP_FILE" 2>/dev/null || + stat -f%z "$TEMP_FILE" 2>/dev/null +) +if [ "$FILE_SIZE" -lt "$MAX_SIZE_PRETTIFY" ]; then + pygmentize -l json -f html -O full,style="$STYLE" <"$TEMP_FILE" >"${TEMP_FILE}_" + mv -f "${TEMP_FILE}_" "$TEMP_FILE" +fi + +# send the command to qutebrowser to open the new file containing the formatted json +echo "open -t file://$TEMP_FILE" >> "$QUTE_FIFO" diff --git a/.config/qutebrowser/misc/userscripts/getbib b/.config/qutebrowser/misc/userscripts/getbib new file mode 100755 index 0000000..22af7a8 --- /dev/null +++ b/.config/qutebrowser/misc/userscripts/getbib @@ -0,0 +1,69 @@ +#!/usr/bin/env python3 +"""Qutebrowser userscript scraping the current web page for DOIs and downloading +corresponding bibtex information. + +Set the environment variable 'QUTE_BIB_FILEPATH' to indicate the path to +download to. Otherwise, bibtex information is downloaded to '/tmp' and hence +deleted at reboot. + +Installation: see qute://help/userscripts.html + +Inspired by +https://ocefpaf.github.io/python4oceanographers/blog/2014/05/19/doi2bibtex/ +""" + +import os +import sys +import shutil +import re +from collections import Counter +from urllib import parse as url_parse +from urllib import request as url_request + + +FIFO_PATH = os.getenv("QUTE_FIFO") + +def message_fifo(message, level="warning"): + """Send message to qutebrowser FIFO. The level must be one of 'info', + 'warning' (default) or 'error'.""" + with open(FIFO_PATH, "w") as fifo: + fifo.write("message-{} '{}'".format(level, message)) + + +source = os.getenv("QUTE_TEXT") +with open(source) as f: + text = f.read() + +# find DOIs on page using regex +dval = re.compile(r'(10\.(\d)+/([^(\s\>\"\<)])+)') +# https://stackoverflow.com/a/10324802/3865876, too strict +# dval = re.compile(r'\b(10[.][0-9]{4,}(?:[.][0-9]+)*/(?:(?!["&\'<>])\S)+)\b') +dois = dval.findall(text) +dois = Counter(e[0] for e in dois) +try: + doi = dois.most_common(1)[0][0] +except IndexError: + message_fifo("No DOIs found on page") + sys.exit() +message_fifo("Found {} DOIs on page, selecting {}".format(len(dois), doi), + level="info") + +# get bibtex data corresponding to DOI +url = "http://dx.doi.org/" + url_parse.quote(doi) +headers = dict(Accept='text/bibliography; style=bibtex') +request = url_request.Request(url, headers=headers) +response = url_request.urlopen(request) +status_code = response.getcode() +if status_code >= 400: + message_fifo("Request returned {}".format(status_code)) + sys.exit() + +# obtain content and format it +bibtex = response.read().decode("utf-8").strip() +bibtex = bibtex.replace(" ", "\n ", 1).\ + replace("}, ", "},\n ").replace("}}", "}\n}") + +# append to file +bib_filepath = os.getenv("QUTE_BIB_FILEPATH", "/tmp/qute.bib") +with open(bib_filepath, "a") as f: + f.write(bibtex + "\n\n") diff --git a/.config/qutebrowser/misc/userscripts/open_download b/.config/qutebrowser/misc/userscripts/open_download new file mode 100755 index 0000000..8dbb113 --- /dev/null +++ b/.config/qutebrowser/misc/userscripts/open_download @@ -0,0 +1,115 @@ +#!/usr/bin/env bash +# Both standalone script and qutebrowser userscript that opens a rofi menu with +# all files from the download director and opens the selected file. It works +# both as a userscript and a standalone script that is called from outside of +# qutebrowser. +# +# Suggested keybinding (for "show downloads"): +# spawn --userscript ~/.config/qutebrowser/open_download +# sd +# +# Requirements: +# - rofi (in a recent version) +# - xdg-open and xdg-mime +# - You should configure qutebrowser to download files to a single directory +# - It comes in handy if you enable downloads.remove_finished. If you want to +# see the recent downloads, just press "sd". +# +# Thorsten Wißmann, 2015 (thorsten` on freenode) +# Any feedback is welcome! + +set -e + +# open a file from the download directory using rofi +DOWNLOAD_DIR=${DOWNLOAD_DIR:-$QUTE_DOWNLOAD_DIR} +DOWNLOAD_DIR=${DOWNLOAD_DIR:-$HOME/Downloads} +# the name of the rofi command +ROFI_CMD=${ROFI_CMD:-rofi} +ROFI_ARGS=${ROFI_ARGS:-} + +msg() { + local cmd="$1" + shift + local msg="$*" + if [ -z "$QUTE_FIFO" ] ; then + echo "$cmd: $msg" >&2 + else + echo "message-$cmd '${msg//\'/\\\'}'" >> "$QUTE_FIFO" + fi +} +die() { + msg error "$*" + if [ -n "$QUTE_FIFO" ] ; then + # when run as a userscript, the above error message already informs the + # user about the failure, and no additional "userscript exited with status + # 1" is needed. + exit 0; + else + exit 1; + fi +} + +if ! [ -d "$DOWNLOAD_DIR" ] ; then + die "Download directory »$DOWNLOAD_DIR« not found!" +fi +if ! command -v "${ROFI_CMD}" > /dev/null ; then + die "Rofi command »${ROFI_CMD}« not found in PATH!" +fi + +rofi_default_args=( + -monitor -2 # place above window + -location 6 # aligned at the bottom + -width 100 # use full window width + -i + -no-custom + -format i # make rofi return the index + -l 10 + -p 'Open download:' -dmenu + ) + +crop-first-column() { + local maxlength=${1:-40} + local expression='s|^\([^\t]\{0,'"$maxlength"'\}\)[^\t]*\t|\1\t|' + sed "$expression" +} + +ls-files() { + # add the slash at the end of the download dir enforces to follow the + # symlink, if the DOWNLOAD_DIR itself is a symlink + # shellcheck disable=SC2010 + ls -Q --quoting-style escape -h -o -1 -A -t "${DOWNLOAD_DIR}/" \ + | grep '^[-]' \ + | cut -d' ' -f3- \ + | sed 's,^\(.*[^\]\) \(.*\)$,\2\t\1,' \ + | sed 's,\\\(.\),\1,g' +} + +mapfile -t entries < <(ls-files) + +# we need to manually check that there are items, because rofi doesn't show up +# if there are no items and -no-custom is passed to rofi. +if [ "${#entries[@]}" -eq 0 ] ; then + die "Download directory »${DOWNLOAD_DIR}« empty" +fi + +line=$(printf '%s\n' "${entries[@]}" \ + | crop-first-column 55 \ + | column -s $'\t' -t \ + | $ROFI_CMD "${rofi_default_args[@]}" "$ROFI_ARGS") || true +if [ -z "$line" ]; then + exit 0 +fi + +file="${entries[$line]}" +file="${file%%$'\t'*}" +path="$DOWNLOAD_DIR/$file" +filetype=$(xdg-mime query filetype "$path") +application=$(xdg-mime query default "$filetype") + +if [ -z "$application" ] ; then + die "Do not know how to open »$file« of type $filetype" +fi + +msg info "Opening »$file« (of type $filetype) with ${application%.desktop}" + +xdg-open "$path" & diff --git a/.config/qutebrowser/misc/userscripts/openfeeds b/.config/qutebrowser/misc/userscripts/openfeeds new file mode 100755 index 0000000..4a1a942 --- /dev/null +++ b/.config/qutebrowser/misc/userscripts/openfeeds @@ -0,0 +1,40 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +# Copyright 2015 jnphilipp <me@jnphilipp.org> +# Copyright 2016-2017 Florian Bruhin (The Compiler) <mail@qutebrowser.org> +# +# This file is part of qutebrowser. +# +# qutebrowser is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# qutebrowser is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with qutebrowser. If not, see <http://www.gnu.org/licenses/>. + +# Opens all links to feeds defined in the head of a site +# +# Ideal for use with tabs_are_windows. Set a hotkey to launch this script, then: +# :bind gF spawn --userscript openfeeds +# +# Use the hotkey to open the feeds in new tab/window, press 'gF' to open +# + +import os +import re + +from bs4 import BeautifulSoup +from urllib.parse import urljoin + +with open(os.environ['QUTE_HTML'], 'r') as f: + soup = BeautifulSoup(f) +with open(os.environ['QUTE_FIFO'], 'w') as f: + for link in soup.find_all('link', rel='alternate', type=re.compile(r'application/((rss|rdf|atom)\+)?xml|text/xml')): + f.write('open -t %s\n' % urljoin(os.environ['QUTE_URL'], link.get('href'))) diff --git a/.config/qutebrowser/misc/userscripts/password_fill b/.config/qutebrowser/misc/userscripts/password_fill new file mode 100755 index 0000000..a61a42c --- /dev/null +++ b/.config/qutebrowser/misc/userscripts/password_fill @@ -0,0 +1,381 @@ +#!/usr/bin/env bash +help() { + blink=$'\e[1;31m' reset=$'\e[0m' +cat <<EOF +This script can only be used as a userscript for qutebrowser +2015, Thorsten Wißmann <edu _at_ thorsten-wissmann _dot_ de> +In case of questions or suggestions, do not hesitate to send me an E-Mail or to +directly ask me via IRC (nickname thorsten\`) in #qutebrowser on freenode. + + $blink!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!$reset + WARNING: the passwords are stored in qutebrowser's + debug log reachable via the url qute://log + $blink!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!$reset + +Usage: run as a userscript form qutebrowser, e.g.: + spawn --userscript ~/.config/qutebrowser/password_fill + +Pass backend: (see also passwordstore.org) + This script expects pass to store the credentials of each page in an extra + file, where the filename (or filepath) contains the domain of the respective + page. The first line of the file must contain the password, the login name + must be contained in a later line beginning with "user:", "login:", or + "username:" (configurable by the user_pattern variable). + +Behavior: + It will try to find a username/password entry in the configured backend + (currently only pass) for the current website and will load that pair of + username and password to any form on the current page that has some password + entry field. If multiple entries are found, a zenity menu is offered. + + If no entry is found, then it crops subdomains from the url if at least one + entry is found in the backend. (In that case, it always shows a menu) + +Configuration: + This script loads the bash script ~/.config/qutebrowser/password_fill_rc (if + it exists), so you can change any configuration variable and overwrite any + function you like. + +EOF +} + +set -o errexit +set -o pipefail +shopt -s nocasematch # make regexp matching in bash case insensitive + +if [ -z "$QUTE_FIFO" ] ; then + help + exit +fi + +error() { + local msg="$*" + echo "message-error '${msg//\'/\\\'}'" >> "$QUTE_FIFO" +} +msg() { + local msg="$*" + echo "message-info '${msg//\'/\\\'}'" >> "$QUTE_FIFO" +} +die() { + error "$*" + exit 0 +} + +javascript_escape() { + # print the first argument in an escaped way, such that it can safely + # be used within javascripts double quotes + sed "s,[\\\\'\"],\\\\&,g" <<< "$1" +} + +# ======================================================= # +# CONFIGURATION +# ======================================================= # +# The configuration file is per default located in +# ~/.config/qutebrowser/password_fill_rc and is a bash script that is loaded +# later in the present script. So basically you can replace all of the +# following definitions and make them fit your needs. + +# The following simplifies a URL to the domain (e.g. "wiki.qutebrowser.org") +# which is later used to search the correct entries in the password backend. If +# you e.g. don't want the "www." to be removed or if you want to distinguish +# between different paths on the same domain. + +simplify_url() { + simple_url="${1##*://}" # remove protocol specification + simple_url="${simple_url%%\?*}" # remove GET parameters + simple_url="${simple_url%%/*}" # remove directory path + simple_url="${simple_url%:*}" # remove port + simple_url="${simple_url##www.}" # remove www. subdomain +} + +# no_entries_found() is called if the first query_entries() call did not find +# any matching entries. Multiple implementations are possible: +# The easiest behavior is to quit: +#no_entries_found() { +# if [ 0 -eq "${#files[@]}" ] ; then +# die "No entry found for »$simple_url«" +# fi +#} +# But you could also fill the files array with all entries from your pass db +# if the first db query did not find anything +# no_entries_found() { +# if [ 0 -eq "${#files[@]}" ] ; then +# query_entries "" +# if [ 0 -eq "${#files[@]}" ] ; then +# die "No entry found for »$simple_url«" +# fi +# fi +# } + +# Another behavior is to drop another level of subdomains until search hits +# are found: +no_entries_found() { + while [ 0 -eq "${#files[@]}" ] && [ -n "$simple_url" ]; do + shorter_simple_url=$(sed 's,^[^.]*\.,,' <<< "$simple_url") + if [ "$shorter_simple_url" = "$simple_url" ] ; then + # if no dot, then even remove the top level domain + simple_url="" + query_entries "$simple_url" + break + fi + simple_url="$shorter_simple_url" + query_entries "$simple_url" + #die "No entry found for »$simple_url«" + # enforce menu if we do "fuzzy" matching + menu_if_one_entry=1 + done + if [ 0 -eq "${#files[@]}" ] ; then + die "No entry found for »$simple_url«" + fi +} + +# Backend implementations tell, how the actual password store is accessed. +# Right now, there is only one fully functional password backend, namely for +# the program "pass". +# A password backend consists of three actions: +# - init() initializes backend-specific things and does sanity checks. +# - query_entries() is called with a simplified url and is expected to fill +# the bash array $files with the names of matching password entries. There +# are no requirements how these names should look like. +# - open_entry() is called with some specific entry of the $files array and is +# expected to write the username of that entry to the $username variable and +# the corresponding password to $password + +reset_backend() { + init() { true ; } + query_entries() { true ; } + open_entry() { true ; } +} + +# choose_entry() is expected to choose one entry from the array $files and +# write it to the variable $file. +choose_entry() { + choose_entry_zenity +} + +# The default implementation chooses a random entry from the array. So if there +# are multiple matching entries, multiple calls to this userscript will +# eventually pick the "correct" entry. I.e. if this userscript is bound to +# "zl", the user has to press "zl" until the correct username shows up in the +# login form. +choose_entry_random() { + local nr=${#files[@]} + file="${files[$((RANDOM % nr))]}" + # Warn user, that there might be other matching password entries + if [ "$nr" -gt 1 ] ; then + msg "Picked $file out of $nr entries: ${files[*]}" + fi +} + +# another implementation would be to ask the user via some menu (like rofi or +# dmenu or zenity or even qutebrowser completion in future?) which entry to +# pick +MENU_COMMAND=( head -n 1 ) +# whether to show the menu if there is only one entry in it +menu_if_one_entry=0 +choose_entry_menu() { + local nr=${#files[@]} + if [ "$nr" -eq 1 ] && ! ((menu_if_one_entry)) ; then + file="${files[0]}" + else + file=$( printf '%s\n' "${files[@]}" | "${MENU_COMMAND[@]}" ) + fi +} + +choose_entry_rofi() { + MENU_COMMAND=( rofi -p "qutebrowser> " -dmenu + -mesg $'Pick a password entry for <b>'"${QUTE_URL//&/&}"'</b>' ) + choose_entry_menu || true +} + +choose_entry_zenity() { + MENU_COMMAND=( zenity --list --title "qutebrowser password fill" + --text "Pick the password entry:" + --column "Name" ) + choose_entry_menu || true +} + +choose_entry_zenity_radio() { + zenity_helper() { + awk '{ print $0 ; print $0 }' \ + | zenity --list --radiolist \ + --title "qutebrowser password fill" \ + --text "Pick the password entry:" \ + --column " " --column "Name" + } + MENU_COMMAND=( zenity_helper ) + choose_entry_menu || true +} + +# ======================================================= +# backend: PASS + +# configuration options: +match_filename=1 # whether allowing entry match by filepath +match_line=0 # whether allowing entry match by URL-Pattern in file + # Note: match_line=1 gets very slow, even for small password stores! +match_line_pattern='^url: .*' # applied using grep -iE +user_pattern='^(user|username|login): ' + +GPG_OPTS=( "--quiet" "--yes" "--compress-algo=none" "--no-encrypt-to" ) +GPG="gpg" +export GPG_TTY="${GPG_TTY:-$(tty 2>/dev/null)}" +command -v gpg2 &>/dev/null && GPG="gpg2" +[[ -n $GPG_AGENT_INFO || $GPG == "gpg2" ]] && GPG_OPTS+=( "--batch" "--use-agent" ) + +pass_backend() { + init() { + PREFIX="${PASSWORD_STORE_DIR:-$HOME/.password-store}" + if ! [ -d "$PREFIX" ] ; then + die "Can not open password store dir »$PREFIX«" + fi + } + query_entries() { + local url="$1" + + if ((match_line)) ; then + # add entries with matching URL-tag + while read -r -d "" passfile ; do + if $GPG "${GPG_OPTS[@]}" -d "$passfile" \ + | grep --max-count=1 -iE "${match_line_pattern}${url}" > /dev/null + then + passfile="${passfile#$PREFIX}" + passfile="${passfile#/}" + files+=( "${passfile%.gpg}" ) + fi + done < <(find -L "$PREFIX" -iname '*.gpg' -print0) + fi + if ((match_filename)) ; then + # add entries with matching filepath + while read -r passfile ; do + passfile="${passfile#$PREFIX}" + passfile="${passfile#/}" + files+=( "${passfile%.gpg}" ) + done < <(find -L "$PREFIX" -iname '*.gpg' | grep "$url") + fi + } + open_entry() { + local path="$PREFIX/${1}.gpg" + password="" + local firstline=1 + while read -r line ; do + if ((firstline)) ; then + password="$line" + firstline=0 + else + if [[ $line =~ $user_pattern ]] ; then + # remove the matching prefix "user: " from the beginning of the line + username=${line#${BASH_REMATCH[0]}} + break + fi + fi + done < <($GPG "${GPG_OPTS[@]}" -d "$path" ) + } +} +# ======================================================= + +# ======================================================= +# backend: secret +secret_backend() { + init() { + return + } + query_entries() { + local domain="$1" + while read -r line ; do + if [[ "$line" == "attribute.username = "* ]] ; then + files+=("$domain ${line:21}") + fi + done < <( secret-tool search --unlock --all domain "$domain" 2>&1 ) + } + open_entry() { + local domain="${1%% *}" + username="${1#* }" + password=$(secret-tool lookup domain "$domain" username "$username") + } +} +# ======================================================= + +# load some sane default backend +reset_backend +pass_backend +# load configuration +QUTE_CONFIG_DIR=${QUTE_CONFIG_DIR:-${XDG_CONFIG_HOME:-$HOME/.config}/qutebrowser/} +PWFILL_CONFIG=${PWFILL_CONFIG:-${QUTE_CONFIG_DIR}/password_fill_rc} +if [ -f "$PWFILL_CONFIG" ] ; then + # shellcheck source=/dev/null + source "$PWFILL_CONFIG" +fi +init + +simplify_url "$QUTE_URL" +query_entries "${simple_url}" +no_entries_found +# remove duplicates +mapfile -t files < <(printf '%s\n' "${files[@]}" | sort | uniq ) +choose_entry +if [ -z "$file" ] ; then + # choose_entry didn't want any of these entries + exit 0 +fi +open_entry "$file" +#username="$(date)" +#password="XYZ" +#msg "$username, ${#password}" + +[ -n "$username" ] || die "Username not set in entry $file" +[ -n "$password" ] || die "Password not set in entry $file" + +js() { +cat <<EOF + function isVisible(elem) { + var style = elem.ownerDocument.defaultView.getComputedStyle(elem, null); + + if (style.getPropertyValue("visibility") !== "visible" || + style.getPropertyValue("display") === "none" || + style.getPropertyValue("opacity") === "0") { + return false; + } + + return elem.offsetWidth > 0 && elem.offsetHeight > 0; + }; + function hasPasswordField(form) { + var inputs = form.getElementsByTagName("input"); + for (var j = 0; j < inputs.length; j++) { + var input = inputs[j]; + if (input.type == "password") { + return true; + } + } + return false; + }; + function loadData2Form (form) { + var inputs = form.getElementsByTagName("input"); + for (var j = 0; j < inputs.length; j++) { + var input = inputs[j]; + if (isVisible(input) && (input.type == "text" || input.type == "email")) { + input.focus(); + input.value = "$(javascript_escape "${username}")"; + input.blur(); + } + if (input.type == "password") { + input.focus(); + input.value = "$(javascript_escape "${password}")"; + input.blur(); + } + } + }; + + var forms = document.getElementsByTagName("form"); + for (i = 0; i < forms.length; i++) { + if (hasPasswordField(forms[i])) { + loadData2Form(forms[i]); + } + } +EOF +} + +printjs() { + js | sed 's,//.*$,,' | tr '\n' ' ' +} +echo "jseval -q $(printjs)" >> "$QUTE_FIFO" diff --git a/.config/qutebrowser/misc/userscripts/qute-keepass b/.config/qutebrowser/misc/userscripts/qute-keepass new file mode 100755 index 0000000..a21ebc9 --- /dev/null +++ b/.config/qutebrowser/misc/userscripts/qute-keepass @@ -0,0 +1,261 @@ +#!/usr/bin/env python3 + +# Copyright 2018 Jay Kamat <jaygkamat@gmail.com> +# +# This file is part of qutebrowser. +# +# qutebrowser is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# qutebrowser is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with qutebrowser. If not, see <http://www.gnu.org/licenses/>. + +"""This userscript allows for insertion of usernames and passwords from keepass +databases using pykeepass. Since it is a userscript, it must be run from +qutebrowser. + +A sample invocation of this script is: + +:spawn --userscript qute-keepass -p ~/KeePassFiles/MainDatabase.kdbx + +And a sample binding + +:bind --mode=insert <ctrl-i> spawn --userscript qute-keepass -p ~/KeePassFiles/MainDatabase.kdbx + +-p or --path is a required argument. + +--keyfile-path allows you to specify a keepass keyfile. If you only use a +keyfile, also add --no-password as well. Specifying --no-password without +--keyfile-path will lead to an error. + +login information is inserted using :insert-text and :fake-key <Tab>, which +means you must have a cursor in position before initiating this userscript. If +you do not do this, you will get 'element not editable' errors. + +If keepass takes a while to open the DB, you might want to consider reducing +the number of transform rounds in your database settings. + +Dependencies: pykeepass (in python3), PyQt5. Without pykeepass, you will get an +exit code of 100. + +********************!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!****************** + +WARNING: The login details are viewable as plaintext in qutebrowser's debug log +(qute://log) and could be compromised if you decide to submit a crash report! + +********************!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!****************** + +""" + +# pylint: disable=bad-builtin + +import argparse +import enum +import functools +import os +import shlex +import subprocess +import sys + +from PyQt5.QtCore import QUrl +from PyQt5.QtWidgets import QApplication, QInputDialog, QLineEdit + +try: + import pykeepass +except ImportError as e: + print("pykeepass not found: {}".format(str(e)), file=sys.stderr) + + # Since this is a common error, try to print it to the FIFO if we can. + if 'QUTE_FIFO' in os.environ: + with open(os.environ['QUTE_FIFO'], 'w') as fifo: + fifo.write('message-error "pykeepass failed to be imported."\n') + fifo.flush() + sys.exit(100) + +argument_parser = argparse.ArgumentParser( + description="Fill passwords using keepass.", + formatter_class=argparse.RawDescriptionHelpFormatter, + epilog=__doc__) +argument_parser.add_argument('url', nargs='?', default=os.getenv('QUTE_URL')) +argument_parser.add_argument('--path', '-p', required=True, + help='Path to the keepass db.') +argument_parser.add_argument('--keyfile-path', '-k', default=None, + help='Path to a keepass keyfile') +argument_parser.add_argument( + '--no-password', action='store_true', + help='Supply if no password is required to unlock this database. ' + 'Only allowed with --keyfile-path') +argument_parser.add_argument( + '--dmenu-invocation', '-d', default='dmenu', + help='Invocation used to execute a dmenu-provider') +argument_parser.add_argument( + '--dmenu-format', '-f', default='{title}: {username}', + help='Format string for keys to display in dmenu.' + ' Must generate a unique string.') +argument_parser.add_argument( + '--no-insert-mode', '-n', dest='insert_mode', action='store_false', + help="Don't automatically enter insert mode") +argument_parser.add_argument( + '--io-encoding', '-i', default='UTF-8', + help='Encoding used to communicate with subprocesses') +group = argument_parser.add_mutually_exclusive_group() +group.add_argument('--username-fill-only', '-e', + action='store_true', help='Only insert username') +group.add_argument('--password-fill-only', '-w', + action='store_true', help='Only insert password') + +CMD_DELAY = 50 + + +class ExitCodes(enum.IntEnum): + """Stores various exit codes groups to use.""" + SUCCESS = 0 + FAILURE = 1 + # 1 is automatically used if Python throws an exception + NO_CANDIDATES = 2 + USER_QUIT = 3 + DB_OPEN_FAIL = 4 + + INTERNAL_ERROR = 10 + + +def qute_command(command): + with open(os.environ['QUTE_FIFO'], 'w') as fifo: + fifo.write(command + '\n') + fifo.flush() + + +def stderr(to_print): + """Extra functionality to echo out errors to qb ui.""" + print(to_print, file=sys.stderr) + qute_command('message-error "{}"'.format(to_print)) + + +def dmenu(items, invocation, encoding): + """Runs dmenu with given arguments.""" + command = shlex.split(invocation) + process = subprocess.run(command, input='\n'.join(items).encode(encoding), + stdout=subprocess.PIPE) + return process.stdout.decode(encoding).strip() + + +def get_password(): + """Get a keepass db password from user.""" + _app = QApplication(sys.argv) + text, ok = QInputDialog.getText( + None, "KeePass DB Password", + "Please enter your KeePass Master Password", + QLineEdit.Password) + if not ok: + stderr('Password Prompt Rejected.') + sys.exit(ExitCodes.USER_QUIT) + return text + + +def find_candidates(args, host): + """Finds candidates that match host""" + file_path = os.path.expanduser(args.path) + + # TODO find a way to keep the db open, so we don't open (and query + # password) it every time + + pw = None + if not args.no_password: + pw = get_password() + + kf = args.keyfile_path + if kf: + kf = os.path.expanduser(kf) + + try: + kp = pykeepass.PyKeePass(file_path, password=pw, keyfile=kf) + except Exception as e: + stderr("There was an error opening the DB: {}".format(str(e))) + + return kp.find_entries(url="{}{}{}".format(".*", host, ".*"), regex=True) + + +def candidate_to_str(args, candidate): + """Turns candidate into a human readable string for dmenu""" + return args.dmenu_format.format(title=candidate.title, + url=candidate.url, + username=candidate.username, + path=candidate.path, + uuid=candidate.uuid) + + +def candidate_to_secret(candidate): + """Turns candidate into a generic (user, password) tuple""" + return (candidate.username, candidate.password) + + +def run(args): + """Runs qute-keepass""" + if not args.url: + argument_parser.print_help() + return ExitCodes.FAILURE + + url_host = QUrl(args.url).host() + + if not url_host: + stderr('{} was not parsed as a valid URL!'.format(args.url)) + return ExitCodes.INTERNAL_ERROR + + # Find candidates matching the host of the given URL + candidates = find_candidates(args, url_host) + if not candidates: + stderr('No candidates for URL {!r} found!'.format(args.url)) + return ExitCodes.NO_CANDIDATES + + # Create a map so we can get turn the resulting string from dmenu back into + # a candidate + candidates_strs = list(map(functools.partial(candidate_to_str, args), + candidates)) + candidates_map = dict(zip(candidates_strs, candidates)) + + if len(candidates) == 1: + selection = candidates.pop() + else: + selection = dmenu(candidates_strs, + args.dmenu_invocation, + args.io_encoding) + + if selection not in candidates_map: + stderr("'{}' was not a valid entry!").format(selection) + return ExitCodes.USER_QUIT + + selection = candidates_map[selection] + + username, password = candidate_to_secret(selection) + + insert_mode = ';; enter-mode insert' if args.insert_mode else '' + if args.username_fill_only: + qute_command('insert-text {}{}'.format(username, insert_mode)) + elif args.password_fill_only: + qute_command('insert-text {}{}'.format(password, insert_mode)) + else: + # Enter username and password using insert-key and fake-key <Tab> + # (which supports more passwords than fake-key only), then switch back + # into insert-mode, so the form can be directly submitted by hitting + # enter afterwards. It dosen't matter when we go into insert mode, but + # the other commands need to be be executed sequentially, so we add + # delays with later. + qute_command('insert-text {} ;;' + 'later {} fake-key <Tab> ;;' + 'later {} insert-text {}{}' + .format(username, CMD_DELAY, + CMD_DELAY * 2, password, insert_mode)) + + return ExitCodes.SUCCESS + + +if __name__ == '__main__': + arguments = argument_parser.parse_args() + sys.exit(run(arguments)) diff --git a/.config/qutebrowser/misc/userscripts/qute-lastpass b/.config/qutebrowser/misc/userscripts/qute-lastpass new file mode 100755 index 0000000..ea88cf8 --- /dev/null +++ b/.config/qutebrowser/misc/userscripts/qute-lastpass @@ -0,0 +1,172 @@ +#!/usr/bin/env python3 + +# Copyright 2017 Chris Braun (cryzed) <cryzed@googlemail.com> +# Adapted for LastPass by Wayne Cheng (welps) <waynethecheng@gmail.com> +# +# This file is part of qutebrowser. +# +# qutebrowser is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published bjy +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# qutebrowser is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with qutebrowser. If not, see <http://www.gnu.org/licenses/>. + +""" +Insert login information using lastpass CLI and a dmenu-compatible application (e.g. dmenu, rofi -dmenu, ...). +A short demonstration can be seen here: https://i.imgur.com/zA61NrF.gifv. +""" + +USAGE = """The domain of the site has to be in the name of the LastPass entry, for example: "github.com/cryzed" or +"websites/github.com". The login information is inserted by emulating key events using qutebrowser's fake-key command in this manner: +[USERNAME]<Tab>[PASSWORD], which is compatible with almost all login forms. + +You must log into LastPass CLI using `lpass login <email>` prior to use of this script. The LastPass CLI agent only holds your master password for an hour by default. If you wish to change this, please see `man lpass`. + +To use in qutebrowser, run: `spawn --userscript qute-lastpass` +""" + +EPILOG = """Dependencies: tldextract (Python 3 module), LastPass CLI (1.3 or newer) + +WARNING: The login details are viewable as plaintext in qutebrowser's debug log (qute://log) and might be shared if +you decide to submit a crash report!""" + +import argparse +import enum +import fnmatch +import functools +import os +import re +import shlex +import subprocess +import sys +import json +import tldextract + +argument_parser = argparse.ArgumentParser( + description=__doc__, usage=USAGE, epilog=EPILOG) +argument_parser.add_argument('url', nargs='?', default=os.getenv('QUTE_URL')) +argument_parser.add_argument('--dmenu-invocation', '-d', default='rofi -dmenu', + help='Invocation used to execute a dmenu-provider') +argument_parser.add_argument('--no-insert-mode', '-n', dest='insert_mode', action='store_false', + help="Don't automatically enter insert mode") +argument_parser.add_argument('--io-encoding', '-i', default='UTF-8', + help='Encoding used to communicate with subprocesses') +argument_parser.add_argument('--merge-candidates', '-m', action='store_true', + help='Merge pass candidates for fully-qualified and registered domain name') +group = argument_parser.add_mutually_exclusive_group() +group.add_argument('--username-only', '-e', + action='store_true', help='Only insert username') +group.add_argument('--password-only', '-w', + action='store_true', help='Only insert password') + +stderr = functools.partial(print, file=sys.stderr) + +class ExitCodes(enum.IntEnum): + SUCCESS = 0 + FAILURE = 1 + # 1 is automatically used if Python throws an exception + NO_PASS_CANDIDATES = 2 + COULD_NOT_MATCH_USERNAME = 3 + COULD_NOT_MATCH_PASSWORD = 4 + +def qute_command(command): + with open(os.environ['QUTE_FIFO'], 'w') as fifo: + fifo.write(command + '\n') + fifo.flush() + +def pass_(domain, encoding): + args = ['lpass', 'show', '-x', '-j', '-G', '.*{:s}.*'.format(domain)] + process = subprocess.run(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + + err = process.stderr.decode(encoding).strip() + if err: + msg = "LastPass CLI returned for {:s} - {:s}".format(domain, err) + stderr(msg) + return '[]' + + out = process.stdout.decode(encoding).strip() + + return out + +def dmenu(items, invocation, encoding): + command = shlex.split(invocation) + process = subprocess.run(command, input='\n'.join( + items).encode(encoding), stdout=subprocess.PIPE) + return process.stdout.decode(encoding).strip() + + +def fake_key_raw(text): + for character in text: + # Escape all characters by default, space requires special handling + sequence = '" "' if character == ' ' else '\{}'.format(character) + qute_command('fake-key {}'.format(sequence)) + + +def main(arguments): + if not arguments.url: + argument_parser.print_help() + return ExitCodes.FAILURE + + extract_result = tldextract.extract(arguments.url) + + # Try to find candidates using targets in the following order: fully-qualified domain name (includes subdomains), + # the registered domain name and finally: the IPv4 address if that's what + # the URL represents + candidates = [] + for target in filter(None, [extract_result.fqdn, extract_result.registered_domain, extract_result.subdomain + extract_result.domain, extract_result.domain, extract_result.ipv4]): + target_candidates = json.loads(pass_(target, arguments.io_encoding)) + if not target_candidates: + continue + + candidates = candidates + target_candidates + if not arguments.merge_candidates: + break + else: + if not candidates: + stderr('No pass candidates for URL {!r} found!'.format( + arguments.url)) + return ExitCodes.NO_PASS_CANDIDATES + + if len(candidates) == 1: + selection = candidates.pop() + else: + choices = ["{:s} | {:s} | {:s} | {:s}".format(c["id"], c["name"], c["url"], c["username"]) for c in candidates] + choice = dmenu(choices, arguments.dmenu_invocation, arguments.io_encoding) + choiceId = choice.split("|")[0].strip() + selection = next((c for (i, c) in enumerate(candidates) if c["id"] == choiceId), None) + + # Nothing was selected, simply return + if not selection: + return ExitCodes.SUCCESS + + username = selection["username"] + password = selection["password"] + + if arguments.username_only: + fake_key_raw(username) + elif arguments.password_only: + fake_key_raw(password) + else: + # Enter username and password using fake-key and <Tab> (which seems to work almost universally), then switch + # back into insert-mode, so the form can be directly submitted by + # hitting enter afterwards + fake_key_raw(username) + qute_command('fake-key <Tab>') + fake_key_raw(password) + + if arguments.insert_mode: + qute_command('enter-mode insert') + + return ExitCodes.SUCCESS + + +if __name__ == '__main__': + arguments = argument_parser.parse_args() + sys.exit(main(arguments)) diff --git a/.config/qutebrowser/misc/userscripts/qute-pass b/.config/qutebrowser/misc/userscripts/qute-pass new file mode 100755 index 0000000..4f79e11 --- /dev/null +++ b/.config/qutebrowser/misc/userscripts/qute-pass @@ -0,0 +1,207 @@ +#!/usr/bin/env python3 + +# Copyright 2017 Chris Braun (cryzed) <cryzed@googlemail.com> +# +# This file is part of qutebrowser. +# +# qutebrowser is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# qutebrowser is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with qutebrowser. If not, see <http://www.gnu.org/licenses/>. + +""" +Insert login information using pass and a dmenu-compatible application (e.g. dmenu, rofi -dmenu, ...). A short +demonstration can be seen here: https://i.imgur.com/KN3XuZP.gif. +""" + +USAGE = """The domain of the site has to appear as a segment in the pass path, for example: "github.com/cryzed" or +"websites/github.com". How the username and password are determined is freely configurable using the CLI arguments. The +login information is inserted by emulating key events using qutebrowser's fake-key command in this manner: +[USERNAME]<Tab>[PASSWORD], which is compatible with almost all login forms. + +Suggested bindings similar to Uzbl's `formfiller` script: + + config.bind('<z><l>', 'spawn --userscript qute-pass') + config.bind('<z><u><l>', 'spawn --userscript qute-pass --username-only') + config.bind('<z><p><l>', 'spawn --userscript qute-pass --password-only') + config.bind('<z><o><l>', 'spawn --userscript qute-pass --otp-only') +""" + +EPILOG = """Dependencies: tldextract (Python 3 module), pass, pass-otp (optional). +For issues and feedback please use: https://github.com/cryzed/qutebrowser-userscripts. + +WARNING: The login details are viewable as plaintext in qutebrowser's debug log (qute://log) and might be shared if +you decide to submit a crash report!""" + +import argparse +import enum +import fnmatch +import functools +import os +import re +import shlex +import subprocess +import sys + +import tldextract + +argument_parser = argparse.ArgumentParser(description=__doc__, usage=USAGE, epilog=EPILOG) +argument_parser.add_argument('url', nargs='?', default=os.getenv('QUTE_URL')) +argument_parser.add_argument('--password-store', '-p', default=os.path.expanduser('~/.password-store'), + help='Path to your pass password-store') +argument_parser.add_argument('--username-pattern', '-u', default=r'.*/(.+)', + help='Regular expression that matches the username') +argument_parser.add_argument('--username-target', '-U', choices=['path', 'secret'], default='path', + help='The target for the username regular expression') +argument_parser.add_argument('--password-pattern', '-P', default=r'(.*)', + help='Regular expression that matches the password') +argument_parser.add_argument('--dmenu-invocation', '-d', default='rofi -dmenu', + help='Invocation used to execute a dmenu-provider') +argument_parser.add_argument('--no-insert-mode', '-n', dest='insert_mode', action='store_false', + help="Don't automatically enter insert mode") +argument_parser.add_argument('--io-encoding', '-i', default='UTF-8', + help='Encoding used to communicate with subprocesses') +argument_parser.add_argument('--merge-candidates', '-m', action='store_true', + help='Merge pass candidates for fully-qualified and registered domain name') +group = argument_parser.add_mutually_exclusive_group() +group.add_argument('--username-only', '-e', action='store_true', help='Only insert username') +group.add_argument('--password-only', '-w', action='store_true', help='Only insert password') +group.add_argument('--otp-only', '-o', action='store_true', help='Only insert OTP code') + +stderr = functools.partial(print, file=sys.stderr) + + +class ExitCodes(enum.IntEnum): + SUCCESS = 0 + FAILURE = 1 + # 1 is automatically used if Python throws an exception + NO_PASS_CANDIDATES = 2 + COULD_NOT_MATCH_USERNAME = 3 + COULD_NOT_MATCH_PASSWORD = 4 + + +def qute_command(command): + with open(os.environ['QUTE_FIFO'], 'w') as fifo: + fifo.write(command + '\n') + fifo.flush() + + +def find_pass_candidates(domain, password_store_path): + candidates = [] + for path, directories, file_names in os.walk(password_store_path, followlinks=True): + if directories or domain not in path.split(os.path.sep): + continue + + # Strip password store path prefix to get the relative pass path + pass_path = path[len(password_store_path) + 1:] + secrets = fnmatch.filter(file_names, '*.gpg') + candidates.extend(os.path.join(pass_path, os.path.splitext(secret)[0]) for secret in secrets) + return candidates + + +def _run_pass(command, encoding): + process = subprocess.run(command, stdout=subprocess.PIPE) + return process.stdout.decode(encoding).strip() + + +def pass_(path, encoding): + return _run_pass(['pass', path], encoding) + + +def pass_otp(path, encoding): + return _run_pass(['pass', 'otp', path], encoding) + + +def dmenu(items, invocation, encoding): + command = shlex.split(invocation) + process = subprocess.run(command, input='\n'.join(items).encode(encoding), stdout=subprocess.PIPE) + return process.stdout.decode(encoding).strip() + + +def fake_key_raw(text): + for character in text: + # Escape all characters by default, space requires special handling + sequence = '" "' if character == ' ' else '\{}'.format(character) + qute_command('fake-key {}'.format(sequence)) + + +def main(arguments): + if not arguments.url: + argument_parser.print_help() + return ExitCodes.FAILURE + + extract_result = tldextract.extract(arguments.url) + + # Expand potential ~ in paths, since this script won't be called from a shell that does it for us + password_store_path = os.path.expanduser(arguments.password_store) + + # Try to find candidates using targets in the following order: fully-qualified domain name (includes subdomains), + # the registered domain name and finally: the IPv4 address if that's what the URL represents + candidates = set() + for target in filter(None, [extract_result.fqdn, extract_result.registered_domain, extract_result.ipv4]): + target_candidates = find_pass_candidates(target, password_store_path) + if not target_candidates: + continue + + candidates.update(target_candidates) + if not arguments.merge_candidates: + break + else: + if not candidates: + stderr('No pass candidates for URL {!r} found!'.format(arguments.url)) + return ExitCodes.NO_PASS_CANDIDATES + + selection = candidates.pop() if len(candidates) == 1 else dmenu(sorted(candidates), arguments.dmenu_invocation, + arguments.io_encoding) + # Nothing was selected, simply return + if not selection: + return ExitCodes.SUCCESS + + secret = pass_(selection, arguments.io_encoding) + + # Match username + target = selection if arguments.username_target == 'path' else secret + match = re.match(arguments.username_pattern, target) + if not match: + stderr('Failed to match username pattern on {}!'.format(arguments.username_target)) + return ExitCodes.COULD_NOT_MATCH_USERNAME + username = match.group(1) + + # Match password + match = re.match(arguments.password_pattern, secret) + if not match: + stderr('Failed to match password pattern on secret!') + return ExitCodes.COULD_NOT_MATCH_PASSWORD + password = match.group(1) + + if arguments.username_only: + fake_key_raw(username) + elif arguments.password_only: + fake_key_raw(password) + elif arguments.otp_only: + otp = pass_otp(selection, arguments.io_encoding) + fake_key_raw(otp) + else: + # Enter username and password using fake-key and <Tab> (which seems to work almost universally), then switch + # back into insert-mode, so the form can be directly submitted by hitting enter afterwards + fake_key_raw(username) + qute_command('fake-key <Tab>') + fake_key_raw(password) + + if arguments.insert_mode: + qute_command('enter-mode insert') + + return ExitCodes.SUCCESS + + +if __name__ == '__main__': + arguments = argument_parser.parse_args() + sys.exit(main(arguments)) diff --git a/.config/qutebrowser/misc/userscripts/qutedmenu b/.config/qutebrowser/misc/userscripts/qutedmenu new file mode 100755 index 0000000..de1b8d6 --- /dev/null +++ b/.config/qutebrowser/misc/userscripts/qutedmenu @@ -0,0 +1,54 @@ +#!/usr/bin/env bash +# Handle open -s && open -t with bemenu + +#:bind o spawn --userscript /path/to/userscripts/qutedmenu open +#:bind O spawn --userscript /path/to/userscripts/qutedmenu tab + +# If you would like to set a custom colorscheme/font use these dirs. +# https://github.com/halfwit/dotfiles/blob/master/.config/dmenu/bemenucolors +readonly confdir=${XDG_CONFIG_HOME:-$HOME/.config} + +readonly optsfile=$confdir/dmenu/bemenucolors + +create_menu() { + # Check quickmarks + while read -r url; do + printf -- '%s\n' "$url" + done < "$QUTE_CONFIG_DIR"/quickmarks + + # Next bookmarks + while read -r url _; do + printf -- '%s\n' "$url" + done < "$QUTE_CONFIG_DIR"/bookmarks/urls + + # Finally history + while read -r _ url; do + printf -- '%s\n' "$url" + done < "$QUTE_DATA_DIR"/history + } + +get_selection() { + opts+=(-p qutebrowser) + #create_menu | dmenu -l 10 "${opts[@]}" + create_menu | bemenu -l 10 "${opts[@]}" +} + +# Main +# https://github.com/halfwit/dotfiles/blob/master/.config/dmenu/font +[[ -s $confdir/dmenu/font ]] && read -r font < "$confdir"/dmenu/font + +[[ $font ]] && opts+=(-fn "$font") + +# shellcheck source=/dev/null +[[ -s $optsfile ]] && source "$optsfile" + +url=$(get_selection) +url=${url/*http/http} + +# If no selection is made, exit (escape pressed, e.g.) +[[ ! $url ]] && exit 0 + +case $1 in + open) printf '%s' "open $url" >> "$QUTE_FIFO" || qutebrowser "$url" ;; + tab) printf '%s' "open -t $url" >> "$QUTE_FIFO" || qutebrowser "$url" ;; +esac diff --git a/.config/qutebrowser/misc/userscripts/readability b/.config/qutebrowser/misc/userscripts/readability new file mode 100755 index 0000000..d0ef437 --- /dev/null +++ b/.config/qutebrowser/misc/userscripts/readability @@ -0,0 +1,41 @@ +#!/usr/bin/env python +# +# Executes python-readability on current page and opens the summary as new tab. +# +# Depends on the python-readability package, or its fork: +# +# - https://github.com/buriy/python-readability +# - https://github.com/bookieio/breadability +# +# Usage: +# :spawn --userscript readability +# +from __future__ import absolute_import +import codecs, os + +tmpfile = os.path.join( + os.environ.get('QUTE_DATA_DIR', + os.path.expanduser('~/.local/share/qutebrowser')), + 'userscripts/readability.html') + +if not os.path.exists(os.path.dirname(tmpfile)): + os.makedirs(os.path.dirname(tmpfile)) + +with codecs.open(os.environ['QUTE_HTML'], 'r', 'utf-8') as source: + data = source.read() + + try: + from breadability.readable import Article as reader + doc = reader(data) + content = doc.readable + except ImportError: + from readability import Document + doc = Document(data) + content = doc.summary().replace('<html>', '<html><head><title>%s</title></head>' % doc.title()) + + with codecs.open(tmpfile, 'w', 'utf-8') as target: + target.write('<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />') + target.write(content) + + with open(os.environ['QUTE_FIFO'], 'w') as fifo: + fifo.write('open -t %s' % tmpfile) diff --git a/.config/qutebrowser/misc/userscripts/ripbang b/.config/qutebrowser/misc/userscripts/ripbang new file mode 100755 index 0000000..b35ff77 --- /dev/null +++ b/.config/qutebrowser/misc/userscripts/ripbang @@ -0,0 +1,34 @@ +#!/usr/bin/env python +# +# Adds DuckDuckGo bang as searchengine. +# +# Usage: +# :spawn --userscript ripbang [bang]... +# +# Example: +# :spawn --userscript ripbang amazon maps +# + +from __future__ import print_function +import os, re, requests, sys + +try: + from urllib.parse import unquote +except ImportError: + from urllib import unquote + +for argument in sys.argv[1:]: + bang = '!' + argument + r = requests.get('https://duckduckgo.com/', + params={'q': bang + ' SEARCHTEXT'}) + + searchengine = unquote(re.search("url=[^']+", r.text).group(0)) + searchengine = searchengine.replace('url=', '') + searchengine = searchengine.replace('/l/?kh=-1&uddg=', '') + searchengine = searchengine.replace('SEARCHTEXT', '{}') + + if os.getenv('QUTE_FIFO'): + with open(os.environ['QUTE_FIFO'], 'w') as fifo: + fifo.write('set searchengines %s %s' % (bang, searchengine)) + else: + print('%s %s' % (bang, searchengine)) diff --git a/.config/qutebrowser/misc/userscripts/rss b/.config/qutebrowser/misc/userscripts/rss new file mode 100755 index 0000000..f8feebe --- /dev/null +++ b/.config/qutebrowser/misc/userscripts/rss @@ -0,0 +1,122 @@ +#!/bin/sh + +# Copyright 2016 Jan Verbeek (blyxxyz) <ring@openmailbox.org> +# +# This file is part of qutebrowser. +# +# qutebrowser is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# qutebrowser is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with qutebrowser. If not, see <http://www.gnu.org/licenses/>. + +# This script keeps track of URLs in RSS feeds and opens new ones. +# New feeds can be added with ':spawn -u /path/to/userscripts/rss add' or +# ':spawn -u /path/to/userscripts/rss <url>'. +# New items can be opened with ':spawn -u /path/to/userscripts/rss'. +# The script doesn't really parse XML, and searches for things that look like +# item links. It might open things that aren't real links, and it might miss +# real links. + +config_dir="$HOME/.qute-rss" + +add_feed () { + touch "feeds" + if grep -Fq "$1" "feeds"; then + notice "$1 is saved already." + else + printf '%s\n' "$1" >> "feeds" + fi +} + +# Show an error message and exit +fail () { + echo "message-error '$*'" > "$QUTE_FIFO" + exit 1 +} + +# Get a sorted list of item URLs from a RSS feed +get_items () { + $curl "$@" | grep "$text_only" -zo -e '<guid[^<>]*>[^<>]*</guid>' \ + -e '<link[^<>]*>[^<>]*</link>' \ + -e '<link[^<>]*href="[^"]*"' | + grep "$text_only" -o 'http[^<>"]*' | sort | uniq +} + +# Show an info message +notice () { + echo "message-info '$*'" > "$QUTE_FIFO" +} + +# Update a database of a feed and open new URLs +read_items () { + cd read_urls || return 1 + feed_file="$(echo "$1" | tr -d /)" + feed_temp_file="$(mktemp "$feed_file.tmp.XXXXXXXXXX")" + feed_new_items="$(mktemp "$feed_file.new.XXXXXXXXXX")" + get_items "$1" > "$feed_temp_file" + if [ ! -s "$feed_temp_file" ]; then + notice "No items found for $1." + rm "$feed_temp_file" "$feed_new_items" + elif [ ! -f "$feed_file" ]; then + notice "$1 is a new feed. All items will be marked as read." + mv "$feed_temp_file" "$feed_file" + rm "$feed_new_items" + else + sort -o "$feed_file" "$feed_file" + comm -2 -3 "$feed_temp_file" "$feed_file" | tee "$feed_new_items" + cat "$feed_new_items" >> "$feed_file" + sort -o "$feed_file" "$feed_file" + rm "$feed_temp_file" "$feed_new_items" + fi | while read -r item; do + echo "open -t $item" > "$QUTE_FIFO" + done +} + +if [ ! -d "$config_dir/read_urls" ]; then + notice "Creating configuration directory." + mkdir -p "$config_dir/read_urls" +fi + +cd "$config_dir" || exit 1 + +if [ $# != 0 ]; then + for arg in "$@"; do + if [ "$arg" = "add" ]; then + add_feed "$QUTE_URL" + else + add_feed "$arg" + fi + done + exit +fi + +if [ ! -f "feeds" ]; then + fail "Add feeds by running ':spawn -u rss add' or ':spawn -u rss <url>'." +fi + +if curl --version >&-; then + curl="curl -sL" +elif wget --version >&-; then + curl="wget -qO -" +else + fail "Either curl or wget is needed to run this script." +fi + +# Detect GNU grep so we can force it to treat everything as text +if < /dev/null grep --help 2>&1 | grep -q -- -a; then + text_only="-a" +fi + +while read -r feed_url; do + read_items "$feed_url" & +done < "$config_dir/feeds" + +wait diff --git a/.config/qutebrowser/misc/userscripts/taskadd b/.config/qutebrowser/misc/userscripts/taskadd new file mode 100755 index 0000000..36e1c2c --- /dev/null +++ b/.config/qutebrowser/misc/userscripts/taskadd @@ -0,0 +1,34 @@ +#!/usr/bin/env bash +# +# Behavior: +# Userscript for qutebrowser which adds a task to taskwarrior. +# If run as a command (:spawn --userscript taskadd), it creates a new task +# with the description equal to the current page title and annotates it with +# the current page url. Additional arguments are passed along so you can add +# mods to the task (e.g. priority, due date, tags). +# +# Example: +# :spawn --userscript taskadd due:eod pri:H +# +# To enable passing along extra args, I suggest using a mapping like: +# :bind <somekey> set-cmd-text -s :spawn --userscript taskadd +# +# If run from hint mode, it uses the selected hint text as the description +# and the selected hint url as the annotation. +# +# Ryan Roden-Corrent (rcorre), 2016 +# Any feedback is welcome! +# +# For more info on Taskwarrior, see http://taskwarrior.org/ + +# use either the current page title or the hint text as the task description +[[ $QUTE_MODE == 'hints' ]] && title=$QUTE_SELECTED_TEXT || title=$QUTE_TITLE + +# try to add the task and grab the output +if msg="$(task add "$title" "$*" 2>&1)"; then + # annotate the new task with the url, send the output back to the browser + task +LATEST annotate "$QUTE_URL" + echo "message-info '$(echo "$msg" | head -n 1)'" >> "$QUTE_FIFO" +else + echo "message-error '$(echo "$msg" | head -n 1)'" >> "$QUTE_FIFO" +fi diff --git a/.config/qutebrowser/misc/userscripts/tor_identity b/.config/qutebrowser/misc/userscripts/tor_identity new file mode 100755 index 0000000..93b6d41 --- /dev/null +++ b/.config/qutebrowser/misc/userscripts/tor_identity @@ -0,0 +1,52 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +# Copyright 2018 jnphilipp <mail@jnphilipp.org> +# +# This file is part of qutebrowser. +# +# qutebrowser is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# qutebrowser is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with qutebrowser. If not, see <http://www.gnu.org/licenses/>. + +# Change your tor identity. +# +# Set a hotkey to launch this script, then: +# :bind ti spawn --userscript tor_identity PASSWORD +# +# Use the hotkey to change your tor identity, press 'ti' to change it. +# https://stem.torproject.org/faq.html#how-do-i-request-a-new-identity-from-tor +# + +import os +import sys + +try: + from stem import Signal + from stem.control import Controller +except ImportError: + if os.getenv('QUTE_FIFO'): + with open(os.environ['QUTE_FIFO'], 'w') as f: + f.write('message-error "Failed to import stem."') + else: + print('Failed to import stem.') + + +password = sys.argv[1] +with Controller.from_port(port=9051) as controller: + controller.authenticate(password) + controller.signal(Signal.NEWNYM) + if os.getenv('QUTE_FIFO'): + with open(os.environ['QUTE_FIFO'], 'w') as f: + f.write('message-info "Tor identity changed."') + else: + print('Tor identity changed.') diff --git a/.config/qutebrowser/misc/userscripts/view_in_mpv b/.config/qutebrowser/misc/userscripts/view_in_mpv new file mode 100755 index 0000000..16603bd --- /dev/null +++ b/.config/qutebrowser/misc/userscripts/view_in_mpv @@ -0,0 +1,143 @@ +#!/usr/bin/env bash +# +# Behavior: +# Userscript for qutebrowser which views the current web page in mpv using +# sensible mpv-flags. While viewing the page in MPV, all <video>, <embed>, +# and <object> tags in the original page are temporarily removed. Clicking on +# such a removed video restores the respective video. +# +# In order to use this script, just start it using `spawn --userscript` from +# qutebrowser. I recommend using an alias, e.g. put this in the +# [alias]-section of qutebrowser.conf: +# +# mpv = spawn --userscript /path/to/view_in_mpv +# +# Background: +# Most of my machines are too slow to play youtube videos using html5, but +# they work fine in mpv (and mpv has further advantages like video scaling, +# etc). Of course, I don't want the video to be played (or even to be +# downloaded) twice — in MPV and in qwebkit. So I often close the tab after +# opening it in mpv. However, I actually want to keep the rest of the page +# (comments and video suggestions), i.e. only the videos should disappear +# when mpv is started. And that's precisely what the present script does. +# +# Thorsten Wißmann, 2015 (thorsten` on freenode) +# Any feedback is welcome! + +set -e + +if [ -z "$QUTE_FIFO" ] ; then + cat 1>&2 <<EOF +Error: $0 can not be run as a standalone script. + +It is a qutebrowser userscript. In order to use it, call it using +'spawn --userscript' as described in qute://help/userscripts.html +EOF + exit 1 +fi + +msg() { + local cmd="$1" + shift + local msg="$*" + if [ -z "$QUTE_FIFO" ] ; then + echo "$cmd: $msg" >&2 + else + echo "message-$cmd '${msg//\'/\\\'}'" >> "$QUTE_FIFO" + fi +} + +MPV_COMMAND=${MPV_COMMAND:-mpv} +# Warning: spaces in single flags are not supported +MPV_FLAGS=${MPV_FLAGS:- --force-window --no-terminal --keep-open=yes --ytdl} +IFS=" " read -r -a video_command <<< "$MPV_COMMAND $MPV_FLAGS" + +js() { +cat <<EOF + + function descendantOfTagName(child, ancestorTagName) { + // tells whether child has some (proper) ancestor + // with the tag name ancestorTagName + while (child.parentNode != null) { + child = child.parentNode; + if (typeof child.tagName === 'undefined') break; + if (child.tagName.toUpperCase() == ancestorTagName.toUpperCase()) { + return true; + } + } + return false; + } + + var App = {}; + + var all_videos = []; + all_videos.push.apply(all_videos, document.getElementsByTagName("video")); + all_videos.push.apply(all_videos, document.getElementsByTagName("object")); + all_videos.push.apply(all_videos, document.getElementsByTagName("embed")); + App.backup_videos = Array(); + App.all_replacements = Array(); + for (i = 0; i < all_videos.length; i++) { + var video = all_videos[i]; + if (descendantOfTagName(video, "object")) { + // skip tags that are contained in an object, because we hide + // the object anyway. + continue; + } + var replacement = document.createElement("div"); + replacement.innerHTML = " + <p style=\\"margin-bottom: 0.5em\\"> + Opening page with: + <span style=\\"font-family: monospace;\\">${video_command[*]}</span> + </p> + <p> + In order to restore this particular video + <a style=\\"font-weight: bold; + color: white; + background: transparent; + \\" + onClick=\\"restore_video(this, " + i + ");\\" + href=\\"javascript: restore_video(this, " + i + ")\\" + >click here</a>. + </p> + "; + replacement.style.position = "relative"; + replacement.style.zIndex = "100003000000"; + replacement.style.fontSize = "1rem"; + replacement.style.textAlign = "center"; + replacement.style.verticalAlign = "middle"; + replacement.style.height = "100%"; + replacement.style.background = "#101010"; + replacement.style.color = "white"; + replacement.style.border = "4px dashed #545454"; + replacement.style.padding = "2em"; + replacement.style.margin = "auto"; + App.all_replacements[i] = replacement; + App.backup_videos[i] = video; + video.parentNode.replaceChild(replacement, video); + } + + function restore_video(obj, index) { + obj = App.all_replacements[index]; + video = App.backup_videos[index]; + console.log(video); + obj.parentNode.replaceChild(video, obj); + } + + /** force repainting the video, thanks to: + * http://martinwolf.org/2014/06/10/force-repaint-of-an-element-with-javascript/ + */ + var siteHeader = document.getElementById('header'); + siteHeader.style.display='none'; + siteHeader.offsetHeight; // no need to store this anywhere, the reference is enough + siteHeader.style.display='block'; + +EOF +} + +printjs() { + js | sed 's,//.*$,,' | tr '\n' ' ' +} +echo "jseval -q $(printjs)" >> "$QUTE_FIFO" + +msg info "Opening $QUTE_URL with mpv" +"${video_command[@]}" "$@" "$QUTE_URL" |
