AtCoder Sample Downloader

AtCoderのサンプルをダウンロードするボタンを追加

ของเมื่อวันที่ 05-05-2021 ดู เวอร์ชันล่าสุด

คุณจะต้องติดตั้งส่วนขยาย เช่น Tampermonkey, Greasemonkey หรือ Violentmonkey เพื่อติดตั้งสคริปต์นี้

คุณจะต้องติดตั้งส่วนขยาย เช่น Tampermonkey หรือ Violentmonkey เพื่อติดตั้งสคริปต์นี้

คุณจะต้องติดตั้งส่วนขยาย เช่น Tampermonkey หรือ Violentmonkey เพื่อติดตั้งสคริปต์นี้

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         AtCoder Sample Downloader
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  AtCoderのサンプルをダウンロードするボタンを追加
// @author       y-i
// @match        https://atcoder.jp/contests/*/tasks/*
// @grant        none
// @require https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/2.0.0/FileSaver.min.js
// @require https://cdnjs.cloudflare.com/ajax/libs/jszip/3.6.0/jszip.min.js
// ==/UserScript==

(function() {
    'use strict';

    const problemName = location.href.split('/').pop();

    const titleElem = document.querySelector('.row > div > .h2');
    if (!titleElem) return;

    const dlBtn = document.createElement('a');
    dlBtn.classList.add('btn', 'btn-default', 'btn-sm');
    dlBtn.textContent = 'Sample DL';
    titleElem.appendChild(dlBtn);

    dlBtn.addEventListener('click', async e => {
        e.preventDefault();

        const samples = Array.from(document.querySelectorAll('[id^="pre-sample"]')).map(el => el.innerText);
        const n = samples.length / 2;

        const zip = new JSZip();

        for (let i = 0; i < n; ++i) {
            const sampleNum = (i - i % 2) / 2;
            const inOut = i % 2 ? 'out' : 'in';
            const filename = `sample-${sampleNum}.${inOut}`;

            zip.file(filename, samples[i]);
        }

        const content = await zip.generateAsync({type:"blob"});
        saveAs(content,`${problemName}.zip`);
    });
})();