forked from ericwoodruff/passwordhasherplus
-
Notifications
You must be signed in to change notification settings - Fork 4
/
popup.js
152 lines (142 loc) · 6.66 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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
var storage;
var url;
var config;
var debug = true;
function rehash(e) {
var masterpw = document.getElementById('masterpw').value;
var hash = generateHash (config, masterpw);
// same hint algo as passhash-ng
var master_hint = PassHashCommon.generateHashWord(
masterpw, masterpw, 2, true, false, true, true, false);
// generate hashword hint based on config, but length 2
if (debug) console.log("config.policy.length: "+config.policy.length);
var hash_hint = generateHashL(config, masterpw, 2);
document.getElementById('hashword').value = hash;
document.getElementById('master-hint').textContent = master_hint;
document.getElementById('hash-hint').textContent = hash_hint;
}
function writeModel () {
config.tag = document.getElementById('tag').value;
if (config.tag.startsWith ("compatible:")) {
config.tag = config.tag.substringAfter ("compatible:");
delete config.policy.seed;
} else {
config.policy.seed = config.options.privateSeed;
}
config.policy.length = document.getElementById('length').value;
config.policy.strength = document.getElementById('strength').value;
if (config.policy.strength == -1) {
config.policy.custom = new Object();
config.policy.custom.d = document.getElementById('d').checked;
config.policy.custom.p = document.getElementById('p').checked;
config.policy.custom.m = document.getElementById('m').checked;
config.policy.custom.r = document.getElementById('r').checked;
} else {
delete config.policy.custom;
}
if(null == config.policy.seed || config.policy.seed == config.options.privateSeed) {
document.getElementById('syncneeded').classList.add("hidden");
}
if (debug) console.log("[popup.js] saving config");
storage.saveConfig(url, config, refreshPopup);
}
function readModel () {
if (debug) console.log("readModel()");
document.getElementById('tag').value = config.tag;
document.getElementById('length').value = config.policy.length;
document.getElementById('strength').val = config.policy.strength;
if (true == config.options.compatibilityMode) {
document.getElementById('compatmodeheader').innerHTML =
"<b>Compatibility:</b> on";
} else if (null == config.policy.seed) {
document.getElementById('tag').value = "compatible:" + config.tag;
}
if (false == config.options.backedUp && false == config.options.compatibilityMode) {
document.getElementById('compatmodeheader').innerHTML =
"<b>Warning:</b> You have not yet indicated that you have backed up your private key. Please do so on the Options page.";
}
if(null != config.policy.seed && config.policy.seed != config.options.privateSeed) {
document.getElementById('syncneeded').classList.remove('hidden');
}
if (config.policy.strength == -1) {
if (debug) console.log("custom strength: show checkboxes");
document.getElementById('strength-requirements').classList.remove('hidden');
if (config.policy.custom === undefined) {
config.policy.custom = config.options.custom;
}
document.getElementById('d').checked = config.policy.custom.d;
document.getElementById('p').checked = config.policy.custom.p;
document.getElementById('m').checked = config.policy.custom.m;
document.getElementById('r').checked = config.policy.custom.r;
} else {
if (debug) console.log("not custom strength: hide checkboxes");
document.getElementById('strength-requirements').classList.add('hidden');
}
if (debug) console.log("[popup.js:readModel] rehashing...");
rehash();
}
function refreshPopup() {
chrome.tabs.query({active: true, currentWindow: true}, function (tabs) {
url = chrome.extension.getBackgroundPage ().grepUrl (tabs[0].url);
if (debug) console.log('loading/creating config for url='+url);
storage.loadConfig(url, (cfg) => {
if (debug) console.log('got config='+JSON.stringify(cfg, null, 2));
config = cfg;
config.fields = toSet (config.fields);
readModel ();
});
});
}
document.getElementById('bump').addEventListener('click', function () {
var tagField = document.getElementById('tag');
tagField.value = bump (tagField.value);
writeModel ();
});
document.getElementById('tag').addEventListener('input', writeModel);
document.getElementById('tag').addEventListener('change', writeModel);
document.getElementById('length').addEventListener('change', writeModel);
document.getElementById('strength').addEventListener('change', writeModel)
document.getElementById('d').addEventListener('change', writeModel);
document.getElementById('p').addEventListener('change', writeModel);
document.getElementById('m').addEventListener('change', writeModel);
document.getElementById('r').addEventListener('change', writeModel);
// for hashing
document.getElementById('masterpw').addEventListener('input', rehash);
document.getElementById('masterpw').addEventListener('change', rehash);
// form submit / cancel
document.getElementById('cancel').addEventListener('click', function() {
window.close();
});
document.getElementById('hasher').addEventListener('submit', function() {
var hash = document.getElementById('hashword').value;
if(debug) console.log("[popup.js] submitting hash '" + hash + "' to content script");
browser.extension.getBackgroundPage().forwardHash(config.tag, hash);
window.close();
});
/* handle masking / unmasking of password and hash fields */
function maskUnmask(elemid) {
var buttonid = 'unmask-'+elemid;
var masked = document.getElementById(elemid).type == 'password';
if (masked) {
document.getElementById(elemid).type = 'text';
document.getElementById(buttonid).textContent = '*';
} else {
document.getElementById(elemid).type = 'password';
document.getElementById(buttonid).textContent = 'a';
}
}
document.getElementById('unmask-masterpw').addEventListener('click',
()=>{maskUnmask('masterpw')});
document.getElementById('unmask-hashword').addEventListener('click',
()=>{maskUnmask('hashword')});
// populate popup fields
refreshPopup();
document.addEventListener('DOMContentLoaded', function() {
document.getElementById('link-options').addEventListener('click', function() {
chrome.runtime.openOptionsPage();
});
document.getElementById('portablePage').addEventListener('click', function() {
// For compatibility with Firefox just do /page.html for URL
chrome.tabs.create({url:'/passhashplus.html?tag=' + document.getElementById('tag').value})
});
});