Last active
February 8, 2022 17:02
-
-
Save DavidWells/e42c1ac366a8aa3a623cb8032cb62121 to your computer and use it in GitHub Desktop.
Fetch pages via JS proxy with await www
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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