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