Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
titaniumnetwork-dev
GitHub Repository: titaniumnetwork-dev/Ultraviolet
Path: blob/main/src/client/history.js
304 views
1
import EventEmitter from "events";
2
import HookEvent from "./hook.js";
3
4
/**
5
* @typedef {import('./index').default} UVClient
6
*/
7
8
class History extends EventEmitter {
9
/**
10
*
11
* @param {UVClient} ctx
12
*/
13
constructor(ctx) {
14
super();
15
this.ctx = ctx;
16
this.window = this.ctx.window;
17
this.History = this.window.History;
18
this.history = this.window.history;
19
this.historyProto = this.History ? this.History.prototype : {};
20
this.pushState = this.historyProto.pushState;
21
this.replaceState = this.historyProto.replaceState;
22
this.go = this.historyProto.go;
23
this.back = this.historyProto.back;
24
this.forward = this.historyProto.forward;
25
}
26
override() {
27
this.overridePushState();
28
this.overrideReplaceState();
29
this.overrideGo();
30
this.overrideForward();
31
this.overrideBack();
32
}
33
overridePushState() {
34
this.ctx.override(this.historyProto, "pushState", (target, that, args) => {
35
if (2 > args.length) return target.apply(that, args);
36
let [state, title, url = ""] = args;
37
38
const event = new HookEvent({ state, title, url }, target, that);
39
this.emit("pushState", event);
40
41
if (event.intercepted) return event.returnValue;
42
return event.target.call(
43
event.that,
44
event.data.state,
45
event.data.title,
46
event.data.url
47
);
48
});
49
}
50
overrideReplaceState() {
51
this.ctx.override(
52
this.historyProto,
53
"replaceState",
54
(target, that, args) => {
55
if (2 > args.length) return target.apply(that, args);
56
let [state, title, url = ""] = args;
57
58
const event = new HookEvent({ state, title, url }, target, that);
59
this.emit("replaceState", event);
60
61
if (event.intercepted) return event.returnValue;
62
return event.target.call(
63
event.that,
64
event.data.state,
65
event.data.title,
66
event.data.url
67
);
68
}
69
);
70
}
71
overrideGo() {
72
this.ctx.override(this.historyProto, "go", (target, that, [delta]) => {
73
const event = new HookEvent({ delta }, target, that);
74
this.emit("go", event);
75
76
if (event.intercepted) return event.returnValue;
77
return event.target.call(event.that, event.data.delta);
78
});
79
}
80
overrideForward() {
81
this.ctx.override(this.historyProto, "forward", (target, that) => {
82
const event = new HookEvent(null, target, that);
83
this.emit("forward", event);
84
85
if (event.intercepted) return event.returnValue;
86
return event.target.call(event.that);
87
});
88
}
89
overrideBack() {
90
this.ctx.override(this.historyProto, "back", (target, that) => {
91
const event = new HookEvent(null, target, that);
92
this.emit("back", event);
93
94
if (event.intercepted) return event.returnValue;
95
return event.target.call(event.that);
96
});
97
}
98
}
99
100
export default History;
101
102