AtCoder Editorial Collector

View all editorials in one page.

目前為 2020-11-22 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         AtCoder Editorial Collector
// @description  View all editorials in one page.
// @version      1.2
// @match        https://atcoder.jp/contests/*/editorial
// @match        https://atcoder.jp/contests/*/editorial?*
// @grant        GM_addStyle
// @namespace    https://github.com/w0mbat-kyopro/user-scripts
// @author       w0mbat
// ==/UserScript==

(async function () {
  'use strict';
  console.log(`🐻 "AtCoder Editorial Collector" start execution. 🐻`)

  async function addScript(src, onload = () => { }) {
    return new Promise((resolve) => {
      const script = document.createElement("script");
      script.type = "text/javascript";
      script.src = src;
      script.onload = () => { onload(); resolve(); };
      document.getElementsByTagName("head")[0].appendChild(script);
    });
  }

  async function loadMathjax() {
    await addScript("https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js?config=TeX-MML-AM_CHTML", () => {
      MathJax.Hub.Config({ messageStyle: "none", tex2jax: { skipTags: ["script", "noscript", "style", "textarea", "code"], inlineMath: [['$', '$'], ['\\(', '\\)']] } });
    });
  }

  async function loadPrettifier() {
    await addScript("https://cdn.rawgit.com/google/code-prettify/master/loader/run_prettify.js");
  }

  async function getEditorial(link) {
    return new Promise((resolve) => {
      const xhr = new XMLHttpRequest()
      xhr.responseType = "document";
      xhr.onload = (response) => {
        const dom = response.target.responseXML;
        const editorialDom = dom.querySelector("#main-container > div.row > div:nth-child(2) > div:nth-child(3)");
        editorialDom && link.parentNode.appendChild(editorialDom);
        resolve();
      }
      xhr.open("GET", link.href);
      xhr.send();
    });
  }

  async function getAllEditorials() {
    return Promise.all(Array.prototype.filter.call(document.getElementsByTagName('a'), e => e.href.match(/\/editorial\//))
      .map(e => getEditorial(e)));
  }

  GM_addStyle("pre code { tab-size: 4; }");
  await getAllEditorials();
  await loadMathjax();
  await loadPrettifier();

  console.log(`🐻 "AtCoder Editorial Collector" end execution. 🐻`)
})();