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