-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
53 lines (42 loc) · 1.42 KB
/
index.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
const HomeComponent = {
render: () => {
let url = window.location.hash.split('?')[1];
if (url !== undefined) {
let decrypted = CryptoJS.AES.decrypt(url, prompt("Passphrase"));
setTimeout(function() {
window.location.href = decrypted.toString(CryptoJS.enc.Utf8);
}, 3000);
return `
<h1>Redirecting to ${decrypted.toString(CryptoJS.enc.Utf8)}</h1>
`
}
let encrypted = CryptoJS.AES.encrypt(prompt("URL"), prompt("Passphrase"));
return `
<code>${window.location}#/?${encrypted}</code>
`;
}
}
const ErrorComponent = {
render: () => {
return `
<section>
<h1>Page Not Found</h1>
</section>
`;
}
}
const routes = [
{ path: '/', component: HomeComponent, },
];
const parseLocation = () => location.hash.slice(1).toLowerCase().split('?')[0] || '/';
const findComponentByPath = (path, routes) => routes.find(r => r.path.match(new RegExp(`^\\${path}$`, 'gm'))) || undefined;
const router = () => {
// Find the component based on the current path
const path = parseLocation();
// If there's no matching route, get the "Error" component
const { component = ErrorComponent } = findComponentByPath(path, routes) || {};
// Render the component in the "app" placeholder
document.getElementById('app').innerHTML = component.render();
};
window.addEventListener('hashchange', router);
window.addEventListener('load', router);