make the whole xdg/dev directories /configs etc more sense making. now theres just...
[uzbl-mobile] / examples / data / uzbl / scripts / formfiller.sh
1 #!/bin/bash
2
3 # simple html form (eg for logins) filler (and manager) for uzbl.
4 # uses settings files like: $keydir/<domain>
5 # files contain lines like: <fieldname>: <value>
6
7
8 # user arg 1:
9 # edit: force editing the file (falls back to new if not found)
10 # new:  start with a new file.
11 # load: try to load from file into form
12
13 # something else (or empty): if file not available: new, otherwise load.
14
15 keydir=$XDG_DATA_HOME/uzbl/forms
16 [ -z "$keydir" ] && exit 1
17
18 #editor=gvim
19 editor='urxvt -e vim'
20
21 config=$1; shift
22 pid=$1;    shift
23 xid=$1;    shift
24 fifo=$1;   shift
25 socket=$1; shift
26 url=$1;    shift
27 title=$1;  shift
28 action=$1
29
30 [ -d $keydir ] || mkdir $keydir || exit 1
31
32 if [ "$action" != 'edit' -a  "$action" != 'new' -a "$action" != 'load' ]
33 then
34         action=new
35         [[ -e $keydir/$domain ]] && action=load
36 elif [ "$action" == 'edit' ] && [[ ! -e $keydir/$domain ]]
37 then
38         action=new
39 fi
40 domain=$(echo $url | sed -re 's|(http\|https)+://([A-Za-z0-9\.]+)/.*|\2|')
41
42
43 #regex='s|.*<input.*?name="([[:graph:]]+)".*?/>.*|\1: |p' # sscj's first version, does not work on http://wiki.archlinux.org/index.php?title=Special:UserLogin&returnto=Main_Page
44  regex='s|.*<input.*?name="([^"]*)".*|\1: |p' #works on arch wiki, but not on http://lists.uzbl.org/listinfo.cgi/uzbl-dev-uzbl.org TODO: improve
45
46
47 if [ "$action" = 'load' ]
48 then
49         [[ -e $keydir/$domain ]] || exit 2
50         gawk -F': ' '{ print "js document.getElementsByName(\"" $1 "\")[0].value = \"" $2 "\";"}' $keydir/$domain >> $fifo
51 else
52         if [ "$action" == 'new' ]
53         then
54                 curl "$url" | grep '<input' | sed -nre "$regex" > $keydir/$domain
55         fi
56         [[ -e $keydir/$domain ]] || exit 3 #this should never happen, but you never know.
57         $editor $keydir/$domain #TODO: if user aborts save in editor, the file is already overwritten
58 fi