Skip to content

Instantly share code, notes, and snippets.

@DavidWells
Last active February 8, 2022 17:02
Show Gist options
  • Save DavidWells/e42c1ac366a8aa3a623cb8032cb62121 to your computer and use it in GitHub Desktop.
Save DavidWells/e42c1ac366a8aa3a623cb8032cb62121 to your computer and use it in GitHub Desktop.
Fetch pages via JS proxy with await www
// Via https://twitter.com/RReverser/status/1490873967577640961
function wwwProxy() {
return new Proxy(new URL('https://www/'), {
get: function get(target, prop) {
let orig = Reflect.get(target, prop);
if (typeof orig === 'function') return orig.bind(target);
if (typeof prop !== 'string') return orig;
if (prop === 'then') return Promise.prototype.then.bind(fetch(target));
target = new URL(target);
target.hostname += `.${prop}`;
return new Proxy(target, { get });
}
})
}
const www = wwwProxy()
async function fetchSite() {
// This next line is the magic
const resp = await www.google.com
console.log('resp', resp)
}
fetchSite()
let round = 0
function wwwProxy() {
return new Proxy(new URL('https://www/'), {
get: function get(target, prop) {
round++
let orig = Reflect.get(target, prop);
console.log('round', round)
console.log('orig', orig)
console.log('target', target)
console.log('prop', prop)
console.log('typeof prop', typeof prop)
if (typeof orig === 'function') return orig.bind(target);
if (typeof prop !== 'string') return orig;
if (prop === 'then') return Promise.prototype.then.bind(fetch(target));
target = new URL(target);
target.hostname += `.${prop}`;
return new Proxy(target, { get });
}
})
}
const www = wwwProxy()
async function fetchSite() {
// This next line is the magic
const resp = await www.google.com
console.log('resp', resp)
}
fetchSite()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment