-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
42 lines (37 loc) · 1.08 KB
/
main.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
// Define the Route class
class Route {
constructor({ url, onRoute }) {
this.url = url;
this.onRoute = onRoute;
}
}
// Define the scriptRouter class
class ScriptRouter {
constructor(routes) {
this.routes = routes;
this.currentRoute = null;
}
init() {
this.handlePathChange(); // Initial call to handle the current path
this.intervalId = setInterval(this.handlePathChange.bind(this), 1000); // Check every second
}
handlePathChange() {
const path = window.location.href.split('/').at('-1');
const newRoute = this.findMatchingRoute(path);
if (newRoute && newRoute !== this.currentRoute) {
newRoute.onRoute();
this.currentRoute = newRoute;
} else {}
}
findMatchingRoute(path) {
for (const key in this.routes) {
if (this.routes.hasOwnProperty(key)) {
const route = this.routes[key];
if (route.url === path) {
return route;
}
}
}
return null;
}
}