Skip to content

Commit f7c3cd2

Browse files
authored
Add Base64 decoding from URL fragment
1 parent f3d8502 commit f7c3cd2

File tree

1 file changed

+31
-4
lines changed

1 file changed

+31
-4
lines changed

index.html

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,36 @@
44

55
</body>
66
<script>
7-
const currentUrl = new URL(window.location.href);
8-
const newDiv = document.createElement('div');
9-
newDiv.textContent = currentUrl.searchParams;
10-
document.body.appendChild(newDiv);
7+
// 1. Get the URL fragment (hash)
8+
const fragment = window.location.hash;
9+
10+
// 2. Check if a fragment exists
11+
if (fragment) {
12+
// The fragment includes the '#' prefix, e.g., "#SGVsbG8gV29ybGQh"
13+
// We need to remove the '#' before decoding
14+
const base64EncodedString = fragment.substring(1);
15+
16+
try {
17+
// 3. Base64 decode the string using atob()
18+
const decodedString = atob(base64EncodedString);
19+
20+
// 4. Create a new element to append the content (e.g., a div or p tag)
21+
const newElement = document.createElement('div');
22+
newElement.textContent = decodedString;
23+
newElement.style.padding = '20px';
24+
newElement.style.border = '1px solid black';
25+
26+
// 5. Append the new element to the document body
27+
document.body.appendChild(newElement);
28+
29+
console.log("Decoded and appended:", decodedString);
30+
31+
} catch (error) {
32+
// Handle cases where the string might not be valid Base64
33+
console.error("Failed to decode Base64 string:", error);
34+
}
35+
} else {
36+
console.log("No URL fragment found.");
37+
}
1138
</script>
1239
</html>

0 commit comments

Comments
 (0)