summaryrefslogtreecommitdiff
path: root/examples/js/formfiller.js
blob: b89147f97837a076fab7fcaea9519f91689fca3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!javascript

/* 
 * Formfiller that automatically fills html forms. To get form names the
 * webinspector can be used (needs 'enable-developer-extras' to be enabled.)
 *
 * The script has 2 configurable variables: 
 *
 * shorcut:   the shortcut that fills the form, uses dwb's default shortcut
 *            syntax
 *
 * forms:     form data that will be filled into the form, the form data entry
 *            follows the following format: 
 *
 * "[url regexp]" : { submit : [whether to autosubmit form], form { [name attribute of input] : [value], .... } }, 
 *
 *
 * */ 

var shortcut = "gF";

var forms = { 
  "http://www.example.com/.*" : {  submit : true,   form : {  name : "name", pass : "password", savepass : "true" } },
  "http://www.foo.com.uk/.*" :  {  submit : false,  form : {  foo_name : "foo", foo_pass : "bar" } }
};

var injectFunction = function (data, submit) {
  var name, value;
  var e;
  for (name in data) {
    value = data[name];
    e = document.getElementsByName(name)[0];
    if(e.type=="checkbox" || e.type=="radio") 
      e.checked=(value.toLowerCase() !== 'false' && value !== '0');
    else
      e.value=value;
  }
  if (submit)
    e.form.submit();
};

function fillForm () {
  var key, r, name;
  var uri = tabs.current.uri;
  for (key in forms) {
    r = new RegExp(key);
    if (r.test(uri)) {
      var script = "(" + String(injectFunction) + ")(" + JSON.stringify(forms[key].form) + "," +  forms[key].submit + ");";
      var frames = tabs.current.allFrames;
      for (var i=0; i<frames.length; i++) {
        frames[i].inject(script);
      }
      return;
    }
  }
}

bind(shortcut, fillForm);

// vim: set ft=javascript: