SikDang Template Loader

2021. 7. 10. 오전 1:37:30

Stan na 09-07-2021. Zobacz najnowsza wersja.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

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

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

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.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==UserScript==
// @name         SikDang Template Loader
// @namespace    http://tampermonkey.net/
// @version      1.0
// @author       -
// @match        https://arca.live/b/smpeople/write
// @icon         https://www.google.com/s2/favicons?domain=arca.live
// @grant        none
// @description  2021. 7. 10. 오전 1:37:30
// ==/UserScript==

const TEMPLATE = {
  '일반': null,
  '요청': 'https://arca.live/b/smpeople/29695511',
  '미번': 'https://arca.live/b/smpeople/29696978',
  '번역': 'https://arca.live/b/smpeople/29697375'
};

const fetchTemplate = async (category) => {
  if(!TEMPLATE[category]) return '';
  return await fetch(TEMPLATE[category])
    .then(res => res.text())
    .then(html => new DOMParser().parseFromString(html, 'text/html'))
    .then(dom => dom.querySelector('.article-content').innerHTML);
}

const loadTemplate = async (category) => {
  const template = await fetchTemplate(category);

  const editorBox = document.querySelector('.write-body .fr-element');
  editorBox.innerHTML = template;

  const editorPlaceHolder = document.querySelector('.fr-placeholder');
  editorPlaceHolder.style.display = template ? 'none' : 'block';
};

const attachTemplate = () => {
  const tabs = document.querySelectorAll('.sub-row span');
  tabs.forEach(tab => {
    const button = tab.querySelector('input');
    const label = tab.querySelector('label');

    if(button && label) {
      button.addEventListener('click', e => {
        loadTemplate(label.innerText);
      });
    }
  });
}

window.addEventListener('load', e => {
  attachTemplate();
});