-
-
Notifications
You must be signed in to change notification settings - Fork 865
Expand file tree
/
Copy pathpasswordCapture.js
More file actions
124 lines (102 loc) · 4.87 KB
/
Copy pathpasswordCapture.js
File metadata and controls
124 lines (102 loc) · 4.87 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
const webviews = require('webviews.js')
const settings = require('util/settings/settings.js')
const PasswordManagers = require('passwordManager/passwordManager.js')
const passwordCapture = {
bar: document.getElementById('password-capture-bar'),
description: document.getElementById('password-capture-description'),
usernameInput: document.getElementById('password-capture-username'),
passwordInput: document.getElementById('password-capture-password'),
revealButton: document.getElementById('password-capture-reveal-password'),
saveButton: document.getElementById('password-capture-save'),
neverSaveButton: document.getElementById('password-capture-never-save'),
closeButton: document.getElementById('password-capture-ignore'),
currentDomain: null,
barHeight: 0,
showCaptureBar (username, password) {
passwordCapture.description.textContent = l('passwordCaptureSavePassword').replace('%s', passwordCapture.currentDomain)
passwordCapture.bar.hidden = false
passwordCapture.passwordInput.type = 'password'
passwordCapture.revealButton.classList.add('carbon:view')
passwordCapture.revealButton.classList.remove('carbon:view-off')
passwordCapture.usernameInput.value = username || ''
passwordCapture.passwordInput.value = password || ''
passwordCapture.barHeight = passwordCapture.bar.getBoundingClientRect().height
webviews.adjustMargin([passwordCapture.barHeight, 0, 0, 0])
},
hideCaptureBar () {
webviews.adjustMargin([passwordCapture.barHeight * -1, 0, 0, 0])
passwordCapture.bar.hidden = true
passwordCapture.usernameInput.value = ''
passwordCapture.passwordInput.value = ''
passwordCapture.currentDomain = null
},
togglePasswordVisibility () {
if (passwordCapture.passwordInput.type === 'password') {
passwordCapture.passwordInput.type = 'text'
passwordCapture.revealButton.classList.remove('carbon:view')
passwordCapture.revealButton.classList.add('carbon:view-off')
} else {
passwordCapture.passwordInput.type = 'password'
passwordCapture.revealButton.classList.add('carbon:view')
passwordCapture.revealButton.classList.remove('carbon:view-off')
}
},
async handleReceivedCredentials (tab, args, frameId) {
let domain = args[0][0]
if (domain.startsWith('www.')) {
domain = domain.slice(4)
}
if (settings.get('passwordsNeverSaveDomains') && settings.get('passwordsNeverSaveDomains').includes(domain)) {
return
}
try {
const username = args[0][1] || ''
const password = args[0][2] || ''
const manager = await PasswordManagers.getConfiguredPasswordManager()
if (!manager || !manager.saveCredential) {
// the password can't be saved
return
}
// check if this username/password combo is already saved
const credentials = await manager.getSuggestions(domain)
const alreadyExists = credentials.some(cred => cred.username === username && cred.password === password)
if (!alreadyExists) {
if (!passwordCapture.bar.hidden) {
passwordCapture.hideCaptureBar()
}
passwordCapture.currentDomain = domain
passwordCapture.showCaptureBar(username, password)
}
} catch (e) {
console.error(`Failed to get password suggestions: ${e.message}`)
}
},
initialize () {
passwordCapture.usernameInput.placeholder = l('username')
passwordCapture.passwordInput.placeholder = l('password')
webviews.bindIPC('password-form-filled', passwordCapture.handleReceivedCredentials)
passwordCapture.saveButton.addEventListener('click', function () {
if (passwordCapture.usernameInput.checkValidity() && passwordCapture.passwordInput.checkValidity()) {
PasswordManagers.getConfiguredPasswordManager().then(function (manager) {
manager.saveCredential(passwordCapture.currentDomain, passwordCapture.usernameInput.value, passwordCapture.passwordInput.value)
passwordCapture.hideCaptureBar()
})
}
})
passwordCapture.neverSaveButton.addEventListener('click', function () {
settings.set('passwordsNeverSaveDomains', (settings.get('passwordsNeverSaveDomains') || []).concat([passwordCapture.currentDomain]))
passwordCapture.hideCaptureBar()
})
passwordCapture.closeButton.addEventListener('click', passwordCapture.hideCaptureBar)
passwordCapture.revealButton.addEventListener('click', passwordCapture.togglePasswordVisibility)
// the bar can change height when the window is resized, so the webview needs to be resized in response
window.addEventListener('resize', function () {
if (!passwordCapture.bar.hidden) {
const oldHeight = passwordCapture.barHeight
passwordCapture.barHeight = passwordCapture.bar.getBoundingClientRect().height
webviews.adjustMargin([passwordCapture.barHeight - oldHeight, 0, 0, 0])
}
})
}
}
module.exports = passwordCapture