sites

public wiki contents of suckless.org
git clone git://git.suckless.org/sites
Log | Files | Refs

bw-dmenu-fill (2440B)


      1 #!/bin/sh
      2 # bitwarden dmenu script - based off of the autofill userscript from qutebrowser
      3 # requires the fifo patch
      4 # $1: winid
      5 #
      6 # add something like this to your config.h:
      7 # #define BITWARDEN_DMENU { .v = (char *[]){ "/bin/sh", "-c", "bw-dmenu-fill" } }
      8 #
      9 # and this to your keybindings:
     10 # { MODKEY, GDK_KEY_z, spawn, BITWARDEN_DMENU },
     11 
     12 fifo=~/.surf/fifo/"$1"
     13 url=$(xprop -id "$1" _SURF_URI | awk '{ print $3 }' | sed 's/"//g')
     14 
     15 username=""
     16 password=""
     17 
     18 output=$(bitwarden-dmenu \
     19 	--bw-list-args="--url=$url" \
     20     --dmenu-args="-l 5 -w $1" \
     21     --dmenu-pswd-args="-w $1" \
     22     --stdout)
     23 
     24 if [ ! -z "$output" ]; then
     25     username=$(echo "$output" | awk 'FNR == 1 {print}')
     26     password=$(echo "$output" | awk 'FNR == 2 {print}')
     27 else
     28     exit
     29 fi
     30 
     31 javascript_escape() {
     32     sed "s,[\\\\'\"],\\\\&,g" <<< "$1"
     33 }
     34 
     35 js() {
     36 cat <<EOF
     37     function isVisible(elem) {
     38         var style = elem.ownerDocument.defaultView.getComputedStyle(elem, null);
     39         if (style.getPropertyValue("visibility") !== "visible" ||
     40             style.getPropertyValue("display") === "none" ||
     41             style.getPropertyValue("opacity") === "0") {
     42             return false;
     43         }
     44         return elem.offsetWidth > 0 && elem.offsetHeight > 0;
     45     };
     46     function hasPasswordField(form) {
     47         var inputs = form.getElementsByTagName("input");
     48         for (var j = 0; j < inputs.length; j++) {
     49             var input = inputs[j];
     50             if (input.type == "password") {
     51                 return true;
     52             }
     53         }
     54         return false;
     55     };
     56     function loadData2Form (form) {
     57         var inputs = form.getElementsByTagName("input");
     58         for (var j = 0; j < inputs.length; j++) {
     59             var input = inputs[j];
     60             if (isVisible(input) && (input.type == "text" || input.type == "email")) {
     61                 input.focus();
     62                 input.value = "$(javascript_escape "${username}")";
     63                 input.blur();
     64             }
     65             if (input.type == "password") {
     66                 input.focus();
     67                 input.value = "$(javascript_escape "${password}")";
     68                 input.blur();
     69             }
     70         }
     71     };
     72     var forms = document.getElementsByTagName("form");
     73     for (i = 0; i < forms.length; i++) {
     74         if (hasPasswordField(forms[i])) {
     75             loadData2Form(forms[i]);
     76         }
     77     }
     78 EOF
     79 }
     80 
     81 printjs() {
     82     js | sed 's,//.*$,,' | tr '\n' ' '
     83 }
     84 
     85 echo "inject $(printjs)" >> "$fifo"