-
Notifications
You must be signed in to change notification settings - Fork 2
/
popup.js
81 lines (66 loc) · 2.57 KB
/
popup.js
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
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
'use strict';
const MIN_PASSPHRASE_LEN = 8;
let logger = chrome.extension.getBackgroundPage().console;
let labelDomain = document.getElementById('domain-name');
let inputTextMessage = document.getElementById('input-text-message');
let inputErrorMessage = document.getElementById('input-error-message');
let buttonGeneratePassword = document.getElementById('button-generate-password');
let buttonUpdatePassphrase = document.getElementById('button-update-passphrase');
let inputPassphrase = document.getElementById('input-passphrase');
let hostNname = "";
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
let activeTab = tabs[0];
let url = new URL(activeTab.url);
hostNname = url.hostname
labelDomain.innerText = hostNname;
logger.log(hostNname)
});
function setMessageBox(msg) {
inputErrorMessage.style.display = "none";
inputTextMessage.style.display = "block";
inputTextMessage.style.padding = "5px";
inputTextMessage.style.paddingLeft = "10px";
inputTextMessage.style.borderRadius = "5px";
inputTextMessage.innerText = msg;
}
function setErrorBox(msg) {
inputTextMessage.style.display = "none";
inputErrorMessage.style.display = "block";
inputErrorMessage.style.padding = "5px";
inputErrorMessage.style.paddingLeft = "10px";
inputErrorMessage.style.borderRadius = "5px";
inputErrorMessage.innerText = msg;
}
buttonGeneratePassword.onclick = function () {
chrome.storage.sync.get('passphrase', function (data) {
let passphrase = data.passphrase;
if (passphrase === '' || passphrase === null || passphrase === undefined) {
setErrorBox('passphrase is not set');
return
}
if (passphrase.length < MIN_PASSPHRASE_LEN) {
setErrorBox(`passphrase is too short, it must be ${MIN_PASSPHRASE_LEN} characters at least`);
return
}
let sha256 = new Hashes.SHA256();
let password = sha256.b64_hmac(passphrase, hostNname)
logger.log(`generated password: ${password}`);
navigator.clipboard.writeText(password)
.then(() => {
setMessageBox('password copied to clipboard');
})
.catch(err => {
// This can happen if the user denies clipboard permissions:
setErrorBox('could not copy password: ', err);
});
});
}
buttonUpdatePassphrase.onclick = function () {
let passphrase = inputPassphrase.value;
chrome.storage.sync.set({passphrase: passphrase}, function() {
console.log(`passphrase '${passphrase}' is stored`);
});
}