forked from ericwoodruff/passwordhasherplus
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathoptions.js
160 lines (143 loc) · 6.71 KB
/
options.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
153
154
155
156
157
158
159
160
// TODO:
// notify user if they have local and sync settings that differ + offer to
// merge
var storage;
function setNewGuid () {
var seedElement = document.getElementById ("seed");
seedElement.value = generateGuid ();
}
function refreshOptionsPage(tag) {
if (debug) console.log("[options.js] refreshOptionsPage called: " + tag);
// make sure checkbox does not remain checked
document.getElementById("sync-options").value = 0;
restoreOptions();
refreshStorage();
}
function saveSync() {
var sync = document.getElementById("sync").value == 1;
var syncopt = document.getElementById("sync-options").value;
if (debug) console.log("[options.js:saveSync] sync=" + sync);
if (debug) console.log("[options.js:saveSync] syncopt=" + syncopt);
storage.migrateArea(sync, syncopt, () => { refreshOptionsPage('saveSync') });
}
// saveOptions does not update sync status, see saveSync().
function saveOptions () {
var options = new Object ();
options.defaultLength = document.getElementById ("length").value;
options.defaultStrength = document.getElementById ("strength").value;
console.log("defaultStrength = " + options.defaultStrength);
if (options.defaultStrength == -1) {
options.custom = new Object();
options.custom.d = document.getElementById('d').checked;
options.custom.p = document.getElementById('p').checked;
options.custom.m = document.getElementById('m').checked;
options.custom.r = document.getElementById('r').checked;
} else {
delete options.custom;
}
options.showMaskButton = document.getElementById ("maskbutton").checked;
options.compatibilityMode = document.getElementById ("compatibility").checked;
options.privateSeed = document.getElementById ("seed").value;
options.backedUp = document.getElementById ("backedup").checked;
options.hashKey = document.getElementById ("hashkey").value;
options.maskKey = document.getElementById ("maskkey").value;
if (debug) console.log('[options.js] Saving options='+JSON.stringify(options,null,2));
storage.saveOptions(options, () => { refreshOptionsPage('saveOptions') });
}
function restoreOptions () {
storage.loadOptions((options) => {
document.getElementById ("length").value = options.defaultLength;
document.getElementById ("strength").value = options.defaultStrength;
document.getElementById ("compatibility").checked = options.compatibilityMode;
document.getElementById ("maskbutton").checked = options.showMaskButton;
document.getElementById ("seed").value = options.privateSeed;
document.getElementById ("backedup").checked = options.backedUp;
document.getElementById ("hashkey").value = options.hashKey;
document.getElementById ("maskkey").value = options.maskKey;
if (debug) console.log('setting sync value to '+ options.sync);
document.getElementById ("sync").value = options.sync ? 1 : 0;
});
}
function refreshStorage() {
dumpDatabase().then(db => {
document.getElementById ("everything").value = JSON.stringify(db, null, 2);
});
}
function clearStorage () {
if (confirm ("You are about to erase all of the Password Hasher Plus database. " +
"This is typically done before loading a snapshot of a previous database state. " +
"Are you certain you want to erase the database?")) {
select_storage_area().then(area => {
area.clear ();
alert ("Your database is now empty. " +
"You probably want to paste a previous snapshot of the database to the text area to the right, " +
"and hit \"Load\" to re-populate the database. " +
"Good luck.");
});
}
storage.migrate ();
}
function loadStorage () {
try {
everything = JSON.parse(document.getElementById ("everything").value);
} catch(e) {
alert("Sorry, the data in the text area to the right is not valid JSON.");
return;
}
// clear and rewrite storage area
storage.init(everything).then(() => { refreshOptionsPage('load from db') });
}
function setShortcut(action, e) {
if (e.which == 16 || e.which == 17)
return;
if (action == "hash")
hk = document.getElementById('hashkey');
if (action == "mask")
hk = document.getElementById('maskkey');
if (e.which != 0)
hk.value = (e.ctrlKey ? "Ctrl+" : "") + (e.shiftKey ? "Shift+" : "") + e.which;
else
hk.value = (action == "hash" ? "Ctrl+Shift+51" : "Ctrl+Shift+56");
}
function showHideCustomStrength() {
var strength = document.getElementById('strength').value;
if (strength == -1) {
document.getElementById('customstrengthrow').classList.remove("hidden");
} else {
document.getElementById('customstrengthrow').classList.add("hidden");
}
}
// Add event listeners once the DOM has fully loaded by listening for the
// `DOMContentLoaded` event on the document, and adding your listeners to
// specific elements when it triggers.
document.addEventListener('DOMContentLoaded', function () {
restoreOptions ();
refreshStorage ();
// make sure that when storage is changed, and options page is open, we
// reflect the changed settings.
browser.storage.onChanged.addListener(function(changes, area) {
restoreOptions();
refreshStorage();
});
document.getElementById('generate').addEventListener('click', setNewGuid);
document.getElementById('backupSave').addEventListener('click', saveOptions);
document.getElementById('backupRevert').addEventListener('click', restoreOptions);
document.getElementById('removeUnUsedTags').addEventListener('click',
function() {storage.collectGarbage (); refreshStorage ();});
document.getElementById('dbClear').addEventListener('click',
function() {clearStorage (); refreshStorage ();});
document.getElementById('dbSave').addEventListener('click', loadStorage);
document.getElementById('dbRevert').addEventListener('click', refreshStorage);
document.getElementById('syncSave').addEventListener('click', saveSync);
document.getElementById('strength').addEventListener('change', showHideCustomStrength);
document.getElementById('portablePage').addEventListener('click',
function() { chrome.tabs.create({url:'/passhashplus.html'}) });
document.getElementById('hashkey').addEventListener('keydown',
function(e) {setShortcut("hash", e)});
document.getElementById('maskkey').addEventListener('keydown',
function(e) {setShortcut("mask", e)});
document.getElementById('haskeydefault').addEventListener('click',
function() {var e = new Object(); e.which=0; setShortcut("hash", e)});
document.getElementById('maskkeydefault').addEventListener('click',
function(e) {var e = new Object(); e.which=0; setShortcut("mask", e)});
});