Last active
August 30, 2025 21:18
-
-
Save kiwiyou/bc89e3dbff247619f92a70f654d6dd1f to your computer and use it in GitHub Desktop.
Polygon Tests Bulk Downloader
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
// ==UserScript== | |
// @name Polygon Tests Bulk Downloader | |
// @match https://polygon.codeforces.com/tests?* | |
// @downloadURL https://gist.githubusercontent.com/kiwiyou/bc89e3dbff247619f92a70f654d6dd1f/raw/script.js | |
// @grant none | |
// @version 1.1 | |
// @author kiwiyou <[email protected]> | |
// ==/UserScript== | |
if (new URLSearchParams(location.search).get('action') === 'preview') setupUi(); | |
function setupUi() { | |
import("https://www.unpkg.com/@zip.js/zip.js@2/index.min.js").then(async ({ | |
ZipWriter | |
}) => { | |
const inputs = [...document.querySelectorAll('pre[id^="input"]')].map( | |
(pre) => [pre, `${pre.id.slice('input'.length)}.in`] | |
); | |
const answers = [...document.querySelectorAll('pre[id^="answer"]')].map( | |
(pre) => [pre, `${pre.id.slice('answer'.length)}.out`] | |
); | |
const all = inputs.concat(answers); | |
const fetchInputs = makeFetcher(inputs); | |
const fetchAnswers = makeFetcher(answers); | |
const fetchAll = makeFetcher(all); | |
const commandBar = document.getElementById('right'); | |
commandBar.append(makeDownloadButton('#download-inputs', 'inputs.zip', 'Download Inputs', fetchInputs)); | |
commandBar.append(makeDownloadButton('#download-answers', 'answers.zip', 'Download Answers', fetchAnswers)); | |
commandBar.append(makeDownloadButton('#download-all', 'all.zip', 'Download All', fetchAll)); | |
function makeFetcher(records) { | |
let url = null; | |
return async () => { | |
if (url === null) { | |
const zipFileStream = new TransformStream(); | |
const zipFileBlobPromise = new Response(zipFileStream.readable).blob(); | |
const zipWriter = new ZipWriter(zipFileStream.writable); | |
const fetchAll = []; | |
for (const [pre, filename] of records) { | |
const a = pre.nextElementSibling.firstElementChild; | |
fetchAll.push( | |
fetch(a.href).then((response) => zipWriter.add(filename, response.body)) | |
); | |
} | |
await Promise.all(fetchAll); | |
await zipWriter.close(); | |
url = URL.createObjectURL(await zipFileBlobPromise); | |
} | |
return url; | |
}; | |
} | |
function makeDownloadButton(href, filename, text, fetcher) { | |
const button = document.createElement('a'); | |
button.href = href; | |
button.download = filename; | |
button.append(text); | |
const firstClick = async () => { | |
button.removeEventListener('click', firstClick); | |
button.href = await fetcher(); | |
button.click(); | |
}; | |
button.addEventListener('click', firstClick); | |
return button; | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment