-
Notifications
You must be signed in to change notification settings - Fork 156
/
index.ts
72 lines (58 loc) · 3.01 KB
/
index.ts
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
import LocationWrapper from './wrapper';
import SandboxBase from '../../base';
import { isLocation, isCrossDomainWindows } from '../../../utils/dom';
import INSTRUCTION from '../../../../processing/script/instruction';
import nativeMethods from '../../native-methods';
import { isIE } from '../../../utils/browser';
import MessageSandbox from '../../event/message';
const LOCATION_WRAPPER = 'hammerhead|location-wrapper';
export default class LocationAccessorsInstrumentation extends SandboxBase {
LOCATION_CHANGED_EVENT = 'hammerhead|event|location-changed';
_locationChangedEventCallback: any;
constructor (private readonly _messageSandbox: MessageSandbox) {
super();
this._locationChangedEventCallback = (e: string) => this.emit(this.LOCATION_CHANGED_EVENT, e);
}
static isLocationWrapper (obj: any) {
return obj instanceof LocationWrapper;
}
static getLocationWrapper (owner: any) {
// NOTE: IE11 case. We can get cross-domain location wrapper without any exceptions.
// We return owner.location in this case, as in other browsers.
if (isIE && isCrossDomainWindows(window, owner))
return owner.location;
// NOTE: When the owner is cross-domain, we cannot get its location wrapper, so we return the original
// location, which cannot be accessed but behaves like a real one. Cross-domain location retains the 'replace'
// and 'assign' methods, so we intercept calls to them through MethodCallInstrumentation.
try {
return owner[LOCATION_WRAPPER];
}
catch (e) {
return owner.location;
}
}
attach (window: Window & typeof globalThis) {
super.attach(window);
const document = window.document;
const locationWrapper = new LocationWrapper(window, this._messageSandbox, this._locationChangedEventCallback);
// NOTE: In Google Chrome, iframes whose src contains html code raise the 'load' event twice.
// So, we need to define code instrumentation functions as 'configurable' so that they can be redefined.
nativeMethods.objectDefineProperty(window, LOCATION_WRAPPER, { value: locationWrapper, configurable: true });
nativeMethods.objectDefineProperty(document, LOCATION_WRAPPER, { value: locationWrapper, configurable: true });
nativeMethods.objectDefineProperty(window, INSTRUCTION.getLocation, {
value: (location: any) => isLocation(location) ? locationWrapper : location,
configurable: true,
});
nativeMethods.objectDefineProperty(window, INSTRUCTION.setLocation, {
value: (location: any, value: any) => {
if (isLocation(location) && typeof value === 'string') {
// @ts-ignore
locationWrapper.href = value;// eslint-disable-line no-restricted-properties
return value;
}
return null;
},
configurable: true,
});
}
}