projang / 체스 갤러리 컨텐츠 헬퍼

// ==UserScript==
// @name         체스 갤러리 컨텐츠 헬퍼
// @version      1.1
// @description  디시인사이드 체스 갤러리 글의 Embed 컨텐츠들을 볼수있게 합니다.
// @match        https://gall.dcinside.com/mgallery/board/view/?id=chess*
// @match        https://m.dcinside.com/board/chess/*
// @author       projang
// @license MIT
// @copyright 2021, projang (https://openuserjs.org/users/projang)
// @updateURL https://openuserjs.org/meta/projang/체스_갤러리_컨텐츠_헬퍼.meta.js
// ==/UserScript==

function injectEmboardIframe(emboardId) {
  const iframe = `<iframe id="${emboardId}" scrolling="no" frameborder="0" style="width:100%;height:550px" src="//www.chess.com/emboard?id=${emboardId}"></iframe>`;
  $(`a:contains("${emboardId}")`).replaceWith(iframe);
  $(`p:contains("${emboardId}")`).replaceWith(iframe);
  $(`div:contains("${emboardId}"):not([class])`).replaceWith(iframe);
  window.addEventListener("message", (e) => {
    if (!(e["data"] && emboardId === e["data"]["id"])) return;
    document.getElementById(`${e["data"]["id"]}`) &&
      (document.getElementById(`${e["data"]["id"]}`).style.height = `${
        e["data"]["frameHeight"] + 30
      }px`);
  });
}

function main() {
  // 체스닷컴 emboard
  const exp = /emboard\?id=(\d+)/g;
  const body = document.body.innerHTML;
  const matches = [...body.matchAll(exp)];
  matches?.map((match) => injectEmboardIframe(match[1]));

  // https://www.chess.com/daily_puzzle
  const iframe =
    '<iframe src="https://www.chess.com/daily_puzzle" scrolling="no" style="width: 400px; height: 480px;"></iframe>';
  $('p:contains("https://www.chess.com/daily_puzzle")').replaceWith(iframe);

  // 기타
  const isMobile = navigator.userAgentData.mobile;
  const eles = [];
  eles.push(...$('span:contains("https://lichess.org")'));
  eles.push(...$('pre:contains("https://lichess.org")'));
  eles.map((ele) => {
    const text = ele.textContent;
    if (!text.includes("iframe")) return;
    ele.innerHTML = text;
    if (isMobile) {
      if (text.includes("lichess")) ele.firstChild.height = 525;
    }
  });
}

main();