-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.html
324 lines (301 loc) · 11.6 KB
/
index.html
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Bitwarden Password Analyzer</title>
<link
rel="shortcut icon"
href="https://raw.githubusercontent.com/bitwarden/clients/main/apps/web/src/images/icons/favicon-32x32.png"
type="image/x-icon"
/>
<script src="https://cdn.tailwindcss.com"></script>
<style>
.file-input-button {
cursor: pointer;
}
.spinner {
border: 4px solid rgba(255, 255, 255, 0.3);
border-radius: 50%;
border-top: 4px solid white;
width: 24px;
height: 24px;
animation: spin 1s linear infinite;
display: inline-block;
vertical-align: middle;
margin-right: 8px;
}
@keyframes spin {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
</style>
</head>
<body class="bg-gray-100 min-h-screen">
<div class="max-w-4xl mx-auto">
<div class="sticky top-0 pb-6 bg-gray-100 p-8 shadow">
<h1 class="text-3xl font-bold mb-6 text-center text-blue-600">
Bitwarden Password Analyzer
</h1>
<div class="flex gap-4 justify-between items-center">
<label
for="fileInput"
class="file-input-button block w-full text-sm text-gray-500 py-2 px-4 rounded-full border text-sm font-semibold bg-blue-50 text-blue-700 hover:bg-blue-100 cursor-pointer"
>
Choose File
<input type="file" id="fileInput" accept=".json" class="hidden" />
</label>
<button
id="downloadBtn"
class="bg-green-500 hover:bg-green-600 text-white font-bold py-2 px-4 rounded flex items-center"
style="display: none"
>
<span
id="downloadSpinner"
class="spinner"
style="display: none"
></span>
<span id="downloadText">Download Updated Export</span>
</button>
</div>
</div>
<div id="results" class="space-y-6 p-8"></div>
</div>
<script>
let originalData = null
let deletedItems = new Set()
let hasUnsavedChanges = false
let originalFileName = ""
function isIPAddress(str) {
const ipv4Regex = /^(\d{1,3}\.){3}\d{1,3}$/
const ipv6Regex = /^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$/
return ipv4Regex.test(str) || ipv6Regex.test(str)
}
function extractDomain(url) {
try {
const { hostname, protocol, pathname } = new URL(url)
if (hostname.trim() === "") {
return protocol + pathname
}
if (isIPAddress(hostname)) {
return hostname
}
const parts = hostname.split(".")
if (parts.length > 2) {
//is this really the "useable domain", or a second-level domain like .co.uk?
const domainOrSld = parts.slice(-2).join(".")
if (domainOrSld.match(/(com|co|org|gov|edu)\.\w{2}/)) {
return parts.slice(-3).join(".")
}
return domainOrSld
}
return hostname
} catch (error) {
if (isIPAddress(url)) {
return url
}
return url
}
}
function groupPasswords(data) {
const groups = {}
data.items.forEach((item) => {
if (
item.type === 1 &&
item.login &&
item.login.uris &&
item.login.uris.length > 0
) {
const domains = new Set(
item.login.uris.map((uri) => extractDomain(uri.uri))
)
domains.forEach((domain) => {
if (!groups[domain]) {
groups[domain] = new Set()
}
groups[domain].add(item)
})
}
})
// Convert Sets to Arrays
for (let domain in groups) {
groups[domain] = Array.from(groups[domain])
}
return groups
}
function displayResults(groups) {
const resultsDiv = document.getElementById("results")
resultsDiv.innerHTML = ""
const filteredAndSorted = Object.entries(groups)
.filter(([_, items]) => items.length > 1)
.sort(([domainA, itemsA], [domainB, itemsB]) => {
if (domainA === "") return 1
if (domainB === "") return -1
const latestA = Math.max(
...itemsA.map((item) => new Date(item.revisionDate))
)
const latestB = Math.max(
...itemsB.map((item) => new Date(item.revisionDate))
)
return latestB - latestA
})
filteredAndSorted.forEach(([domain, items]) => {
const groupDiv = document.createElement("div")
groupDiv.className = "bg-white shadow-md rounded-lg p-6"
groupDiv.innerHTML = `
<h2 class="text-xl font-semibold mb-4 text-gray-800">${domain} (${
items.length
})</h2>
<ul class="space-y-4">
${items
.sort(
(a, b) =>
new Date(b.revisionDate) -
new Date(a.revisionDate)
)
.map(
(item) => `
<li class="flex items-center justify-between ${
deletedItems.has(item.id) ? "opacity-50" : ""
}">
<div class="flex flex-col gap-2">
<div class="text-gray-600">${
item.name
}</div>
<div class="text-sm text-gray-400">${
item.login.username
}</div>
<div class="text-sm text-gray-400">Password: ${
item.login.password
}</div>
<div class="text-xs text-gray-400 text-ellipsis overflow-hidden break-all">URIs: ${item.login.uris
.map((uri) => uri.uri)
.join(", ")}</div>
</div>
<button class="delete-btn bg-red-500 hover:bg-red-600 text-white font-bold py-1 px-3 rounded text-sm" data-id="${
item.id
}">
${
deletedItems.has(item.id)
? "Undo"
: "Delete"
}
</button>
</li>
`
)
.join("")}
</ul>
`
resultsDiv.appendChild(groupDiv)
})
if (filteredAndSorted.length === 0) {
resultsDiv.innerHTML =
'<p class="text-center text-gray-600">No domains with multiple login items found.</p>'
}
// Add event listeners to delete buttons
document.querySelectorAll(".delete-btn").forEach((btn) => {
btn.addEventListener("click", function () {
const id = this.getAttribute("data-id")
if (deletedItems.has(id)) {
deletedItems.delete(id)
hasUnsavedChanges = deletedItems.size > 0
} else {
deletedItems.add(id)
hasUnsavedChanges = true
}
// Find all buttons with the same data-id and update their text and opacity
document
.querySelectorAll(`.delete-btn[data-id="${id}"]`)
.forEach((sameIdBtn) => {
if (deletedItems.has(id)) {
sameIdBtn.textContent = "Undo"
} else {
sameIdBtn.textContent = "Delete"
}
sameIdBtn.closest("li").classList.toggle("opacity-50")
})
})
})
}
function handleFileSelect(event) {
const file = event.target.files[0]
if (file) {
originalFileName = file.name // Store the original file name
const reader = new FileReader()
reader.onload = (e) => {
try {
originalData = JSON.parse(e.target.result)
const groups = groupPasswords(originalData)
displayResults(groups)
document.getElementById("downloadBtn").style.display = "flex"
} catch (error) {
console.error("Error parsing JSON:", error)
alert(
"Error parsing the JSON file. Please make sure it's a valid Bitwarden export."
)
}
}
reader.readAsText(file)
}
}
async function downloadUpdatedJSON() {
if (!originalData) return
const downloadBtn = document.getElementById("downloadBtn")
const downloadSpinner = document.getElementById("downloadSpinner")
const downloadText = document.getElementById("downloadText")
downloadBtn.disabled = true
downloadSpinner.style.display = "inline-block"
downloadText.textContent = "Preparing Download..."
await new Promise((resolve) => setTimeout(resolve, 100)) // Allow UI to update
const updatedData = {
...originalData,
items: originalData.items.filter(
(item) => !deletedItems.has(item.id)
),
}
const dataStr =
"data:text/json;charset=utf-8," +
encodeURIComponent(JSON.stringify(updatedData, null, 2))
const downloadAnchorNode = document.createElement("a")
downloadAnchorNode.setAttribute("href", dataStr)
// Modify the file name
let baseName = originalFileName.replace(/\.[^/.]+$/, "") // Remove extension
let extension = originalFileName.split(".").pop() // Get extension
let suffix = "_updated"
let match = baseName.match(/_updated(_\d+)?$/) // Check if ends with _updated or _updated_n
if (match) {
let num = parseInt(match[1]?.slice(1) || "0", 10) + 1 // Increment the number
baseName = baseName.replace(/_updated(_\d+)?$/, `_updated_${num}`) // Replace with incremented number
} else {
baseName += suffix // Simply add _updated
}
const updatedFileName = `${baseName}.${extension}` // Reconstruct the file name with the updated part
downloadAnchorNode.setAttribute("download", updatedFileName)
document.body.appendChild(downloadAnchorNode)
downloadAnchorNode.click()
downloadAnchorNode.remove()
downloadBtn.disabled = false
downloadSpinner.style.display = "none"
downloadText.textContent = "Download Updated Export"
hasUnsavedChanges = false
}
document
.getElementById("fileInput")
.addEventListener("change", handleFileSelect)
document
.getElementById("downloadBtn")
.addEventListener("click", downloadUpdatedJSON)
window.addEventListener("beforeunload", function (e) {
if (hasUnsavedChanges) {
e.preventDefault()
e.returnValue = ""
}
})
</script>
</body>
</html>