Added example scripts to setup formfiller (#153).
authorDaniel Carl <danielcarl@gmx.de>
Sun, 14 Dec 2014 21:35:33 +0000 (22:35 +0100)
committerDaniel Carl <danielcarl@gmx.de>
Sun, 14 Dec 2014 21:35:33 +0000 (22:35 +0100)
examples/formfiller/formfiller [new file with mode: 0755]
examples/formfiller/scripts.js [new file with mode: 0644]

diff --git a/examples/formfiller/formfiller b/examples/formfiller/formfiller
new file mode 100755 (executable)
index 0000000..60fdf5e
--- /dev/null
@@ -0,0 +1,53 @@
+#!/usr/bin/env bash
+# Formfiller that reads gpg encrypted credentials from file and let vimb fill
+# execute a JavaScript method to fill in the form data.
+# Call this from vimb by ':sh! formfiller %'
+#
+# The form data are stored gpg encrypted in $VIMB_KEY_DIR/{domain}.gpg or as
+# fallback $XDG_CONFIG_HOME/vimb/keys/{domain}.gpg. The files must contain a
+# valid JavaScript arra that can be used for the _vbform.fill() method.
+#
+# A unencrypted sample file could look like this:
+# ["input[name='user']:daniel", "input[name='password']:p45w0rD"]
+
+export POSIXLY_CORRECT=1
+
+VIMB_KEY_DIR=${VIMB_KEY_DIR:-"$XDG_CONFIG_HOME/vimb/keys"}
+uri=$1
+
+die() {
+    echo "$1" >&2
+    exit 1
+}
+
+fillform() {
+    # make sure we are in normal mode and fill in the form data
+    # use :: to not save the secrets into vimb command history or into the
+    # last ex command register ":
+    echo "<Esc>::e! _vbform.fill($1);<CR>" >> $VIMB_FIFO
+}
+
+# check if uri is given
+if [ -z "$uri" ]; then
+    die 'No URI given'
+fi
+# check if the script is run from vimb with fifo support enabled
+if [ -z "$VIMB_FIFO" ] || [ ! -p "$VIMB_FIFO" ]; then
+    die 'This script must be run from vimb with fifo support'
+fi
+
+# extract the domain part without ports from given uri
+domain=$(echo "$uri" | sed -r 's@https?://([^:/]+).*@\1@')
+
+# check if there exists a keyfile for the domain
+if [ -e "$VIMB_KEY_DIR/${domain}.gpg" ]; then
+    # this requires the gpg-agent to contains already the key
+    data=$(gpg --batch -qd "$VIMB_KEY_DIR/${domain}.gpg")
+    # abort here if the file could not be decrypted
+    if [ $? -gt 0 ]; then
+        exit 1
+    fi
+    fillform "$data"
+elif [ -e "$VIMB_KEY_DIR/$domain" ]; then # fallback to unencrypted files
+    fillform "$(cat "$VIMB_KEY_DIR/$domain")"
+fi
diff --git a/examples/formfiller/scripts.js b/examples/formfiller/scripts.js
new file mode 100644 (file)
index 0000000..f9bbd8f
--- /dev/null
@@ -0,0 +1,50 @@
+/* Script to insert various data into form fields.
+ * Given data is an array of "Selector:Value" tupel.
+ * ["selector:value","...:..."]
+ * Example call from within vimb:
+ * ::e! _vbform.fill(["input[name='login']:daniel","input[name='password']:p45w0rD"]);
+ *
+ * Note the double ':' in front, this tells vimb not to write the command into
+ * history or the last excmd register ":. */
+"use strict"
+var _vbform = {
+    fill: function(data) {
+        data = data||[];
+        var e, i, k, s;
+
+        // iterate over data array and try to find the elements
+        for (i = 0; i < data.length; i++) {
+            // split into selector and value part
+            s = data[i].split(":");
+            e = document.querySelectorAll(s[0]);
+            for (k = 0; k < e.length; k++) {
+                // fill the form fields
+                this.set(e[k], s[1]);
+            }
+        }
+    },
+
+    // set the value for the form element
+    set: function(e, v) {
+        var i, t, n = e.nodeName.toLowerCase();
+
+        if (n == "input") {
+            t = e.type;
+            if (!t || t == "text" || t == "password") {
+                e.value = v;
+            } else if (t == "checkbox" || t == "radio") {
+                e.checked = ("1" == v) ? true : false;
+            } else if (t == "submit") {
+                e.click();
+            }
+        } else if (n == "textarea") {
+            e.value = v;
+        } else if (n == "select") {
+            for (i = 0; i < e.options.length; i++) {
+                if (e.options[i].value == v) {
+                    e.options[i].selected = "selected";
+                }
+            }
+        }
+    }
+};