View All Editorials

View all editorials of the AtCoder contest in one page.

目前为 2022-10-16 提交的版本。查看 最新版本

// ==UserScript==
// @name            View All Editorials
// @name:ja         解説ぜんぶ見る
// @description     View all editorials of the AtCoder contest in one page.
// @description:ja  AtCoderコンテストの解説ページに、すべての問題の解説をまとめて表示します。
// @version         1.4.0
// @icon            https://www.google.com/s2/favicons?domain=atcoder.jp
// @match           https://atcoder.jp/contests/*/editorial
// @match           https://atcoder.jp/contests/*/editorial?*
// @grant           GM_addStyle
// @require         https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.js
// @require         https://cdn.jsdelivr.net/npm/[email protected]/dist/contrib/auto-render.min.js
// @require         https://cdnjs.cloudflare.com/ajax/libs/jquery-timeago/1.6.7/jquery.timeago.min.js
// @require         https://cdnjs.cloudflare.com/ajax/libs/jquery-timeago/1.6.7/locales/jquery.timeago.ja.min.js
// @namespace       https://gitlab.com/w0mbat/user-scripts
// @author          w0mbat
// ==/UserScript==

(async function () {
  'use strict';
  console.log(`🐻 "View All Editorials" start execution. 🐻`)

  const appendHeadChild = (tagName, options) =>
    Object.assign(document.head.appendChild(document.createElement(tagName)), options);
  const addScript = (src) => new Promise((resolve) => {
    appendHeadChild('script', { src, type: 'text/javascript', onload: resolve });
  });
  const addStyleSheet = (src) => new Promise((resolve) => {
    appendHeadChild('link', { rel: 'stylesheet', href: src, onload: resolve });
  });

  const loadKaTeX = async () =>
    await addStyleSheet("https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.css");
  const kaTexOptions = {
    delimiters: [
      { left: "$$", right: "$$", display: true },
      { left: "$", right: "$", display: false },
      { left: "\\(", right: "\\)", display: false },
      { left: "\\[", right: "\\]", display: true }
    ],
    ignoredTags: ["script", "noscript", "style", "textarea", "code", "option"],
    ignoredClasses: ["prettyprint", "source-code-for-copy"],
    throwOnError: false
  };
  const renderKaTeX = (rootDom) => {
    /* global renderMathInElement */
    renderMathInElement && renderMathInElement(rootDom, kaTexOptions);
  };

  const loadPrettifier = async () =>
    await addScript("https://cdn.rawgit.com/google/code-prettify/master/loader/run_prettify.js?autorun=false");
  /* global PR */
  const runPrettifier = () => PR.prettyPrint();

  const renderTimeAgo = () => {
    /* global $ */
    $("time.timeago").timeago();
    $('.tooltip-unix').each(function () {
      var unix = parseInt($(this).attr('title'), 10);
      if (1400000000 <= unix && unix <= 5000000000) {
        var date = new Date(unix * 1000);
        $(this).attr('title', date.toLocaleString());
      }
    });
    $('[data-toggle="tooltip"]').tooltip();
  };

  const editorialBodyQuery = "#main-container > div.row > div:nth-child(2)";
  const scrape = (doc) => [
    doc.querySelector(`${editorialBodyQuery} > div:nth-of-type(1)`),
    doc.querySelector(`${editorialBodyQuery} > div:nth-of-type(2)`),
  ];
  const loadEditorial = async (link) => {
    const response = await fetch(link.href);
    if (!response.ok) throw "Fetch failed";
    const [content, history] = scrape(new DOMParser().parseFromString(await response.text(), 'text/html'));
    if (!content || !history) throw "Scraping failed";
    const div = link.parentNode.appendChild(document.createElement('div'));
    div.classList.add('🐻-editorial-content');
    div.appendChild(content);
    div.appendChild(history);
    renderKaTeX(div);
    renderTimeAgo();
    runPrettifier();
  };

  const intersectionCallback = (entries, observer) => {
    entries.forEach(async entry => {
      if (entry.isIntersecting) {
        await loadEditorial(entry.target)
          .catch(ex => console.warn(`🐻 Something wrong: "${entry.target.href}", ${ex}`));
        observer.unobserve(entry.target);
      }
    });
  };
  const observeEditorialLinks = (links) => {
    const observer = new IntersectionObserver(intersectionCallback);
    links.forEach(e => observer.observe(e));
  };

  GM_addStyle(`
    pre code { tab-size: 2; }
    ${editorialBodyQuery} > ul > li { font-size: larger; }
    .🐻-editorial-content { margin-top: 0.3em; font-size: smaller; }
  `);
  await loadKaTeX();
  await loadPrettifier();

  const filter4InternalEditorialLink = (link) => link.href.match(/\/contests\/.+\/editorial\//);
  const links = Array.prototype.filter.call(document.getElementsByTagName('a'), filter4InternalEditorialLink);
  if (links.length > 0) await loadEditorial(links[0]);
  if (links.length > 1) observeEditorialLinks(links.slice(1));

  console.log(`🐻 "View All Editorials" end execution. 🐻`)
})();