summaryrefslogtreecommitdiffstats
path: root/plugins/www/resources/js/www-js.js
blob: 06980c9812f97926cfd6540a3ecbad1fdf037948 (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/* jshint esversion: 6 */

/** A quick function to find  and fill login fields */
function setLoginFields () {
  const evt = new Event('change')

  const frames = window.frames

  let doc
  let pswdField
  let usrField
  let formNode
  let userFound = false

  if (frames.length !== 0) {
    for (let i = 0; i < frames.length; i++) {
      doc = frames[i].document
      pswdField = doc.querySelectorAll('input[type=\'password\']')
      if ((pswdField !== undefined) && (pswdField !== null)) {
        break
      }
    }
    if ((pswdField === undefined) || (pswdField === null)) {
      /* What if we don't have login forms in the iFrame? -> window */
      doc = window.document
      pswdField = doc.querySelectorAll('input[type=\'password\']')
    }
  } else {
    doc = window.document
    pswdField = doc.querySelectorAll('input[type=\'password\']')
  }

  if (pswdField !== undefined) {
    pswdField.forEach(function (pswdElement) {
      if (pswdElement.getAttribute('autocomplete') !== 'new-password') {
        pswdElement.value = 'PWDPLACEHOLDER'
      }

      formNode = pswdElement.form
      if (formNode !== null) {
        console.debug('Form elements found')
        usrField = formNode.querySelectorAll('input[type=\'text\']')

        usrField.forEach(function (usrElement) {
          usrElement.value = 'USRPLACEHOLDER'
          if (usrElement !== null) {
            usrElement.dispatchEvent(evt)
            userFound = true
          }
        })
        pswdElement.dispatchEvent(evt)
      }
      if (formNode === null || !userFound) {
        console.debug('Form elements found')
        console.debug('Inputs elements may be in other containers')
        const inputs = doc.getElementsByTagName('input')
        for (let i = 0; i < inputs.length; i += 1) {
          console.debug('input type: ' + inputs[i].type)
          switch (inputs[i].type) {
            case 'new-password':
              continue
            case 'password':
              continue
            case 'hidden':
              continue
            case 'email':
              inputs[i].value = 'USRPLACEHOLDER'
              userFound = true
              break
            case 'text':
              if (!userFound) {
                inputs[i].value = 'USRPLACEHOLDER'
                userFound = true
              }
              break
            default:
              console.debug('Tentativily add username if no userFound')
              if (!userFound) {
                inputs[i].value = 'USRPLACEHOLDER'
                userFound = true
              }
                            // code block
          }
          if (userFound) {
            inputs[i].dispatchEvent(evt)
            console.debug('Username field found and set(?)')
            break
          }
        }
      }
    })
  } else {
      console.debug('We already have a password')

  }
}

setLoginFields()