Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
titaniumnetwork-dev
GitHub Repository: titaniumnetwork-dev/Ludicrous
Path: blob/main/corrosion/lib/browser/location.js
1223 views
1
class Location {
2
get [Symbol.toPrimitive]() {
3
return () => this.href;
4
};
5
};
6
7
function createLocation(ctx, url) {
8
const _location = new Location();
9
const _url = new URL(url);
10
[
11
'hash',
12
'host',
13
'hostname',
14
'href',
15
'pathname',
16
'port',
17
'protocol',
18
'search',
19
'origin',
20
].forEach(property => {
21
Object.defineProperty(_location, property, {
22
get() {
23
return _url[property];
24
},
25
set(val) {
26
if (ctx.serviceWorker || property == 'origin') return;
27
if (property == 'href') {
28
return ctx.window.location.href = ctx.url.wrap(new URL(val, _url).href);
29
};
30
_url[property] = val;
31
return ctx.window.location.href = ctx.url.wrap(_url);
32
},
33
});
34
});
35
if (!ctx.serviceWorker) [
36
'assign',
37
'replace',
38
'reload',
39
].forEach(method => {
40
_location[method] = new Proxy(ctx.window.location[method], {
41
apply(target, that, args) {
42
if (args[0]) args[0] = ctx.url.wrap(args[0], ctx.meta);
43
return Reflect.apply(target.bind(ctx.window.location), that, args);
44
},
45
});
46
});
47
_location.toString = new Proxy(_url.toString, {
48
apply(target, that, args) {
49
return Reflect.apply(target.bind(_url), that, args);
50
},
51
});
52
return _location;
53
};
54
55
createLocation.Location = Location;
56
module.exports = createLocation;
57