This repository has been archived by the owner on Dec 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathlocation.js
135 lines (119 loc) · 5.18 KB
/
location.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import EventEmitter from "./events.js";
class LocationApi extends EventEmitter {
constructor(ctx) {
super();
this.ctx = ctx;
this.window = ctx.window;
this.location = this.window.location;
this.WorkerLocation = this.ctx.worker ? this.window.WorkerLocation : null;
this.workerLocProto = this.WorkerLocation ? this.WorkerLocation.prototype : {};
this.keys = ['href', 'protocol', 'host', 'hostname', 'port', 'pathname', 'search', 'hash', 'origin'];
this.HashChangeEvent = this.window.HashChangeEvent || null;
this.href = this.WorkerLocation ? ctx.nativeMethods.getOwnPropertyDescriptor(this.workerLocProto, 'href') :
ctx.nativeMethods.getOwnPropertyDescriptor(this.location, 'href');
};
overrideWorkerLocation(parse) {
if (!this.WorkerLocation) return false;
const uv = this;
for (const key of this.keys) {
this.ctx.overrideDescriptor(this.workerLocProto, key, {
get: (target, that) => {
return parse(
uv.href.get.call(this.location)
)[key]
},
});
};
return true;
};
emulate(parse, wrap) {
const emulation = {};
const that = this;
for (const key of that.keys) {
this.ctx.nativeMethods.defineProperty(emulation, key, {
get() {
return parse(
that.href.get.call(that.location)
)[key];
},
set: key !== 'origin' ? function (val) {
switch(key) {
case 'href':
that.location.href = wrap(val);
break;
case 'hash':
that.emit('hashchange', emulation.href, (val.trim().startsWith('#') ? new URL(val.trim(), emulation.href).href : new URL('#' + val.trim(), emulation.href).href), that);
break;
default:
const url = new URL(emulation.href);
url[key] = val;
that.location.href = wrap(url.href);
};
} : undefined,
configurable: false,
enumerable: true,
});
};
if ('reload' in this.location) {
this.ctx.nativeMethods.defineProperty(emulation, 'reload', {
value: this.ctx.wrap(this.location, 'reload', (target, that) => {
return target.call(that === emulation ? this.location : that);
}),
writable: false,
enumerable: true,
});
};
if ('replace' in this.location) {
this.ctx.nativeMethods.defineProperty(emulation, 'replace', {
value: this.ctx.wrap(this.location, 'assign', (target, that, args) => {
if (!args.length || that !== emulation) target.call(that);
that = this.location;
let [ input ] = args;
const url = new URL(input, emulation.href);
return target.call(that === emulation ? this.location : that, wrap(url.href));
}),
writable: false,
enumerable: true,
});
};
if ('assign' in this.location) {
this.ctx.nativeMethods.defineProperty(emulation, 'assign', {
value: this.ctx.wrap(this.location, 'assign', (target, that, args) => {
if (!args.length || that !== emulation) target.call(that);
that = this.location;
let [ input ] = args;
const url = new URL(input, emulation.href);
return target.call(that === emulation ? this.location : that, wrap(url.href));
}),
writable: false,
enumerable: true,
});
};
if ('ancestorOrigins' in this.location) {
this.ctx.nativeMethods.defineProperty(emulation, 'ancestorOrigins', {
get() {
const arr = [];
if (that.window.DOMStringList) that.ctx.nativeMethods.setPrototypeOf(arr, that.window.DOMStringList.prototype);
return arr;
},
set: undefined,
enumerable: true,
});
};
this.ctx.nativeMethods.defineProperty(emulation, 'toString', {
value: this.ctx.wrap(this.location, 'toString', () => {
return emulation.href;
}),
enumerable: true,
writable: false,
});
this.ctx.nativeMethods.defineProperty(emulation, Symbol.toPrimitive, {
value: () => emulation.href,
writable: false,
enumerable: false,
});
if (this.ctx.window.Location) this.ctx.nativeMethods.setPrototypeOf(emulation, this.ctx.window.Location.prototype);
return emulation;
};
};
export default LocationApi;