Skip to content

Instantly share code, notes, and snippets.

@yutsuku
Created September 21, 2021 23:39
Show Gist options
  • Save yutsuku/d19ed11e01e2d22458333c4b3f3959a6 to your computer and use it in GitHub Desktop.
Save yutsuku/d19ed11e01e2d22458333c4b3f3959a6 to your computer and use it in GitHub Desktop.
/**
* @param {String} HTML representing a single element
* @return {Element}
*/
function htmlToElement(html) {
var template = document.createElement('template');
html = html.trim();
template.innerHTML = html;
return template.content.firstChild;
}
/**
* @param {String} full url to the image
* @param {String} proxy
* @param {Number} timeout Network timeout in ms
* @return {String} | false
*/
async function tryURL(url, proxy, timeout) {
try {
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), timeout);
const response = await fetch(proxy + url, {
signal: controller.signal
});
if (!response.ok) {
return false;
}
if (response.status >= 400) {
return false;
}
return url;
} catch (error) {
return false;
}
}
/**
* DOM elements must be ready before calling this function
* @param {BigInt} a
* @param {BigInt} b
* @param {Number} timeout Network timeout in ms
*/
async function processPair(a, b, proxy, timeout) {
const images = document.querySelector('#images');
const counter = document.querySelector('#counter-ok');
const counterFail = document.querySelector('#counter-fail');
const lastA = document.querySelector('#last-a');
const lastB = document.querySelector('#last-b');
const url = `https://cdn.discordapp.com/attachments/${a}/${b}/unknown.png`;
let response = await tryURL(url, proxy, timeout);
if (response) {
let value = BigInt(counter.dataset.value) + BigInt('1');
counter.innerHTML = value;
counter.setAttribute('data-value', value);
images.appendChild(htmlToElement(`<img src="${response}" />`));
} else {
let value = BigInt(counterFail.dataset.value) + BigInt('1');
counterFail.innerHTML = value;
counterFail.setAttribute('data-value', value);
}
lastA.innerHTML = a;
lastB.innerHTML = b;
}
function prepareDomNodes() {
document.body.innerHTML = '';
document.body.className = '';
document.body.appendChild(htmlToElement(`
<style>
html, body { color: hsl(1, 0%, 15%); background: hsl(1, 0%, 90%); }
#counter { font-weight: bold; }
#counter-ok { color: hsl(115, 65%, 45%); }
#counter-fail { color: hsl(1, 65%, 45%); }
img { max-width: 100vmax; max-height: 100vmax; }
</style>
`));
document.body.appendChild(htmlToElement(`
<div id="box">
<section id="counter">
<div id="last-values">
<p>a: <span id="last-a"></span></p>
<p>b: <span id="last-b"></span></p>
</div>
<div id="counter-ok" data-value="0">...</div>
<div id="counter-fail" data-value="0">...</div>
</section>
<div id="images"></div>
</div>
`));
}
/**
* @param {BigInt} a
* @param {BigInt} aMax
* @param {BigInt} b
* @param {BigInt} bMax
* @param {String} proxy CORS proxy url
* @param {Number} timeout Network timeout in ms
*/
async function processAll(a, aMax, b, bMax, proxy, timeout) {
for (let newA = a; newA <= aMax; newA = newA + 1n) {
for (let newB = b; newB <= bMax; newB = newB + 1n) {
await processPair(newA, newB, proxy, timeout);
}
}
}
/**
* And freeze your browser!
* last a/b will be out of order too
*
* @param {BigInt} a
* @param {BigInt} aMax
* @param {BigInt} b
* @param {BigInt} bMax
* @param {String} proxy CORS proxy url
* @param {Number} timeout Network timeout in ms
*/
function processAllAtIncredibleHihgSpeed(a, aMax, b, bMax, proxy, timeout) {
for (let newA = a; newA <= aMax; newA = newA + 1n) {
for (let newB = b; newB <= bMax; newB = newB + 1n) {
processPair(newA, newB, proxy, timeout);
}
}
}
/** main entry **/
const proxy = "https://cors.bridged.cc/";
const timeout = 3000; // how long to wait for each request (in ms)
// "a" and "b" are part of the URL
// https://cdn.discordapp.com/attachments/494986892228689931/889331310705909760/unknown.png
const a = BigInt("494986892228689931");
const b = BigInt("889331310705909760");
// how far go from a and b
const aMax = a + 100n;
const bMax = b + 100n;
const gottaGoFast = false; // set to true if you hate your computer
/** main entry end **/
prepareDomNodes();
if (gottaGoFast) {
processAllAtIncredibleHihgSpeed(a, aMax, b, bMax, proxy, timeout);
} else {
processAll(a, aMax, b, bMax, proxy, timeout);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment