Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
titaniumnetwork-dev
GitHub Repository: titaniumnetwork-dev/Ludicrous
Path: blob/main/corrosion/lib/browser/index.js
1223 views
1
const createDocumentRewriter = require('./document');
2
const createHistoryRewriter = require('./history');
3
const createHttpRewriter = require('./http');
4
const createLocation = require('./location');
5
const createWorkerRewriter = require('./worker');
6
const defaultConfig = {
7
prefix: '/service/',
8
codec: 'plain',
9
ws: true,
10
cookie: true,
11
title: 'Service',
12
serviceWorker: false,
13
url: null,
14
window: false,
15
};
16
17
class Corrosion extends require('../rewrite') {
18
constructor(config = defaultConfig) {
19
super(Object.assign(defaultConfig, config));
20
const corrosion = this;
21
if (!this.config.window) throw 'Corrosion Error: No window was given.';
22
this.serviceWorker = this.config.serviceWorker || false;
23
this.window = this.config.window;
24
this.document = this.serviceWorker ? this.window.document : {};
25
this._url = new URL((this.config.url || this.url.unwrap(this.window.location.href, { origin: this.window.location.origin, })));
26
this.originalXhr = this.window.XMLHttpRequest;
27
this.meta = {
28
origin: this.window.location.origin,
29
get base() {
30
if (corrosion.serviceWorker) return corrosion._url;
31
return corrosion.window.document.baseURI != corrosion.location.href ? new URL(corrosion.window.document.baseURI) : corrosion._url;
32
},
33
url: this._url,
34
};
35
this.location = createLocation(this, this._url);
36
this.rewriteHttp = createHttpRewriter(this);
37
this.rewriteDocument = createDocumentRewriter(this);
38
this.rewriteHistory = createHistoryRewriter(this);
39
this.rewriteWorker = createWorkerRewriter(this);
40
if (!this.serviceWorker && this.window.document.currentScript) this.window.document.currentScript.remove();
41
};
42
get parent() {
43
if (this.serviceWorker) return false;
44
try {
45
return this.window.parent.$corrosion ? this.window.parent : this.window;
46
} catch(e) {
47
return this.window;
48
};
49
};
50
get top() {
51
if (this.serviceWorker) return false;
52
try {
53
return this.window.top.$corrosion ? this.window.top : this.parent;
54
} catch(e) {
55
return this.parent;
56
};
57
};
58
init() {
59
this.rewriteHttp();
60
this.rewriteDocument();
61
this.rewriteHistory();
62
this.rewriteWorker();
63
this.window.Location = createLocation.Location;
64
this.window.$corrosionGet$ = this.get$.bind(this);
65
this.window.$corrosionSet$ = this.set$.bind(this);
66
this.window.$corrosionGet$m = this.get$m.bind(this);
67
this.window.$corrosionSet$m = this.set$m.bind(this);
68
this.window.$corrosionCall$m = this.call$m.bind(this);
69
};
70
get$m(obj, key) {
71
if (!this.serviceWorker && this.window != this.window.parent && obj == this.window.parent) {
72
return this.parent.$corrosion.get$m(this.parent, key);
73
};
74
if (!this.serviceWorker && this.window != this.window.top && obj == this.window.top) {
75
return this.top.$corrosion.get$m(this.top, key);
76
};
77
if (obj == this.window && key == 'location' || !this.serviceWorker && obj == this.window.document && key == 'location') return this.location;
78
if (!this.serviceWorker && obj == this.window && key == 'parent' && this.window != this.window.parent) return this.parent;
79
if (!this.serviceWorker && obj == this.window && key == 'top' && this.window != this.window.top) return this.top;
80
return obj[key];
81
};
82
set$m(obj, key, val, operator) {
83
if (!this.serviceWorker && this.window != this.window.parent && obj == this.window.parent) {
84
return this.parent.$corrosion.set$m(this.parent, key, val, operator);
85
};
86
if (!this.serviceWorker && this.window != this.window.top && obj == this.window.top) {
87
return this.top.$corrosion.set$m(this.top, key, val, operator);
88
};
89
if (obj == this.window && key == 'location' || !this.serviceWorker && obj == this.window.document && key == 'location') obj = this;
90
switch(operator) {
91
case '+=':
92
return obj[key] += val;
93
case '-=':
94
return obj[key] -= val;
95
case '*=':
96
return obj[key] *= val;
97
case '/=':
98
return obj[key] /= val;
99
case '%=':
100
return obj[key] %= val;
101
case '**=':
102
return obj[key] **= val;
103
case '<<=':
104
return obj[key] <<= val;
105
case '>>=':
106
return obj[key] >>= val;
107
case '>>>=':
108
return obj[key] >>>= val;
109
case '&=':
110
return obj[key] &= val;
111
case '^=':
112
return obj[key] ^= val;
113
case '|=':
114
return obj[key] |= val;
115
case '&&=':
116
return obj[key] &&= val;
117
case '||=':
118
return obj[key] ||= val;
119
case '??=':
120
return obj[key] ??= val;
121
case '++':
122
return obj[key]++;
123
case '--':
124
return obj[key]--;
125
case '=':
126
default:
127
return obj[key] = val;
128
};
129
};
130
call$m(obj, key, args) {
131
if (!this.serviceWorker && this.window != this.window.parent && obj == this.window.parent) {
132
return this.parent.$corrosion.call$m(this.parent, key, args);
133
};
134
if (!this.serviceWorker && this.window != this.window.top && obj == this.window.top) {
135
return this.top.$corrosion.call$m(this.top, key, args);
136
};
137
return obj[key](...args);
138
};
139
get$(obj) {
140
if (obj == this.window.location) return this.location;
141
if (!this.serviceWorker && obj == this.window.parent) return this.parent;
142
if (!this.serviceWorker && obj == this.window.top) return this.top;
143
return obj;
144
};
145
set$(obj, val, operator) {
146
if (obj == this.window.location) return this.set$(this.location, val, operator);
147
if (!this.serviceWorker && this.window != this.window.parent && obj == this.window.parent) return this.parent.set$(this.parent, val, operator);
148
if (!this.serviceWorker && this.window != this.window.top && obj == this.window.top) return this.top.set$(this.top, val, operator);
149
switch(operator) {
150
case '+=':
151
return obj += val;
152
case '-=':
153
return obj -= val;
154
case '*=':
155
return obj *= val;
156
case '/=':
157
return obj /= val;
158
case '%=':
159
return obj %= val;
160
case '**=':
161
return obj **= val;
162
case '<<=':
163
return obj <<= val;
164
case '>>=':
165
return obj >>= val;
166
case '>>>=':
167
return obj >>>= val;
168
case '&=':
169
return obj &= val;
170
case '^=':
171
return obj ^= val;
172
case '|=':
173
return obj |= val;
174
case '&&=':
175
return obj &&= val;
176
case '||=':
177
return obj ||= val;
178
case '??=':
179
return obj ??= val;
180
case '++':
181
return obj++;
182
case '--':
183
return obj--;
184
case '=':
185
default:
186
return obj = val;
187
};
188
};
189
updateLocation() {
190
this._url = new URL(this.url.unwrap(this.window.location.href, this.meta));
191
this.location = createLocation(this, this._url);
192
};
193
};
194
195
globalThis.Corrosion = Corrosion;
196