Style Fixes for AtCoder

Forced attempt to fix style issues on the AtCoder web pages.

Per 08-03-2024. Zie de nieuwste versie.

Voor het installeren van scripts heb je een extensie nodig, zoals Tampermonkey, Greasemonkey of Violentmonkey.

Voor het installeren van scripts heb je een extensie nodig, zoals Tampermonkey of Violentmonkey.

Voor het installeren van scripts heb je een extensie nodig, zoals Tampermonkey of Violentmonkey.

Voor het installeren van scripts heb je een extensie nodig, zoals Tampermonkey of Userscripts.

Voor het installeren van scripts heb je een extensie nodig, zoals {tampermonkey_link:Tampermonkey}.

Voor het installeren van scripts heb je een gebruikersscriptbeheerder nodig.

(Ik heb al een user script manager, laat me het downloaden!)

Voor het installeren van gebruikersstijlen heb je een extensie nodig, zoals {stylus_link:Stylus}.

Voor het installeren van gebruikersstijlen heb je een extensie nodig, zoals {stylus_link:Stylus}.

Voor het installeren van gebruikersstijlen heb je een extensie nodig, zoals {stylus_link:Stylus}.

Voor het installeren van gebruikersstijlen heb je een gebruikersstijlbeheerder nodig.

Voor het installeren van gebruikersstijlen heb je een gebruikersstijlbeheerder nodig.

Voor het installeren van gebruikersstijlen heb je een gebruikersstijlbeheerder nodig.

(Ik heb al een beheerder - laat me doorgaan met de installatie!)

// ==UserScript==
// @name             Style Fixes for AtCoder
// @name:ja          Style Fixes for AtCoder
// @namespace        https://github.com/roumcha/browser-extensions/tree/main/src/style-fixes-for-atcoder
// @version          2023.12.1
// @description      Forced attempt to fix style issues on the AtCoder web pages.
// @description:ja   AtCoderのウェブサイトの表示崩れを無理やり抑え込む
// @author           Roumcha
// @license          Creative Commons Zero v1.0 Universal
// @match            https://atcoder.jp/*
// @match            https://*.atcoder.jp/*
// @grant            GM.xmlHttpRequest
// @connect          img.atcoder.jp
// @run-at           document-start
// ==/UserScript==

"use strict";
(() => {
  // src/style-fixes-for-atcoder/infonav.ts
  function fix() {
    document.addEventListener("DOMContentLoaded", () => {
      if (navigator.userAgent.toLowerCase().includes("mobile"))
        return;
      const navElement = document.querySelector("#top-editarea header nav");
      const logoDiv = navElement.children[0];
      const hamburgerDiv = navElement.children[1];
      const menuDiv = navElement.children[2];
      hamburgerDiv.remove();
      menuDiv.className = "flex gap-x-12";
    });
  }

  // src/style-fixes-for-atcoder/mediaquery768.ts
  function fix2(fetchFunc) {
    mitigateLayoutShift();
    window.addEventListener("load", () => {
      overwriteStyleElements();
      overrideLinkElements(fetchFunc);
    });
  }
  function mitigateLayoutShift() {
    const styleElement = document.createElement("style");
    styleElement.textContent = `
    @media screen and (767.0px < width < 768.0px){
      .header-logo > img {
        max-width: 40px !important;
      }
      .keyvisual-logo > img {
        max-width: 67px !important;
      }
    }
  `;
    document.head.insertAdjacentElement("afterbegin", styleElement);
  }
  function overwriteStyleElements() {
    for (const styleElement of document.head.getElementsByTagName("style")) {
      if (styleElement.textContent) {
        styleElement.textContent = modifyCss(styleElement.textContent);
      }
    }
  }
  function overrideLinkElements(requestFunc) {
    for (const linkElement of document.head.getElementsByTagName("link")) {
      if (linkElement.rel !== "stylesheet")
        continue;
      if (!linkElement.href.includes("atcoder"))
        continue;
      if (linkElement.href.includes("bootstrap"))
        continue;
      requestFunc(linkElement.href).then((response) => {
        const styleElement = document.createElement("style");
        styleElement.textContent = modifyCss(response);
        linkElement.insertAdjacentElement("afterend", styleElement);
      });
    }
  }
  function modifyCss(css) {
    return css.replace(/(@media[^{]+[0-9]+[13579])()(px[^{]*{)/g, "$1.95$3");
  }

  // src/style-fixes-for-atcoder/lib.ts
  function fixStyle(options) {
    if (document.head.classList.contains("style-fixed"))
      return;
    document.head.classList.add("style-fixed");
    if (location.href.startsWith("https://atcoder.jp")) {
      fix2(options.fetchFunc);
    }
    if (location.href.startsWith("https://info.atcoder.jp")) {
      fix();
    }
  }

  // src/style-fixes-for-atcoder/user-script.ts
  fixStyle({
    fetchFunc: (url) => GM.xmlHttpRequest({ method: "GET", url }).then(
      (response) => response.responseText
    )
  });
})();