Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
titaniumnetwork-dev
GitHub Repository: titaniumnetwork-dev/Ultraviolet
Path: blob/main/src/client/navigator.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 NavigatorApi extends EventEmitter {
9
/**
10
*
11
* @param {UVClient} ctx
12
*/
13
constructor(ctx) {
14
super();
15
this.ctx = ctx;
16
this.window = ctx.window;
17
this.navigator = this.window.navigator;
18
this.Navigator = this.window.Navigator || {};
19
this.navProto = this.Navigator.prototype || {};
20
this.sendBeacon = this.navProto.sendBeacon;
21
}
22
overrideSendBeacon() {
23
this.ctx.override(this.navProto, "sendBeacon", (target, that, args) => {
24
if (!args.length) return target.apply(that, args);
25
let [url, data = ""] = args;
26
27
const event = new HookEvent({ url, data }, target, that);
28
this.emit("sendBeacon", event);
29
30
if (event.intercepted) return event.returnValue;
31
return event.target.call(event.that, event.data.url, event.data.data);
32
});
33
}
34
}
35
36
export default NavigatorApi;
37
38