Arcalive Template Loader

-

  1. // ==UserScript==
  2. // @name Arcalive Template Loader
  3. // @name:ko 아카라이브 글 양식 스크립트
  4. // @namespace Violentmonkey Scripts
  5. // @match https://arca.live/b/*/write
  6. // @match https://arca.live/b/*/*/edit
  7. // @grant none
  8. // @version 1.0.3
  9. // @author -
  10. // @description -
  11. // @description:ko -
  12. // ==/UserScript==
  13. // 심야식당 고정 탬플릿 (표만 불러옵니다)
  14. const TEMPLATE = {
  15. '요청': 'https://arca.live/b/simya/29695511',
  16. '미번': 'https://arca.live/b/simya/29696978',
  17. '번역': 'https://arca.live/b/simya/29697375'
  18. };
  19. // 탬플릿 불러오기
  20. const fetchTemplate = async (category) => {
  21. // 커스텀 양식 불러오기
  22. let currentTemplate = localStorage.getItem("ARCA_CUSTOM_FORM_FOR_PATH_" + location.pathname + "_AND_FOR_CATEGORY_" + category)
  23. if(currentTemplate) return currentTemplate
  24. // 심야식당 확인
  25. if(location.href.includes("simya")) {
  26. // 기본 양식 불러오기
  27. if(!TEMPLATE[category]) return '';
  28. return await fetch(TEMPLATE[category])
  29. .then(res => res.text())
  30. .then(html => new DOMParser().parseFromString(html, 'text/html'))
  31. .then(dom => dom.querySelector(".article-content").innerHTML); // 해당 게시글을 불러오기
  32. } else {
  33. return null
  34. }
  35. }
  36. // 탬플릿 불러오기
  37. const loadTemplate = async (category) => {
  38. const template = await fetchTemplate(category);
  39. const editorBox = document.querySelector('.write-body .fr-element');
  40. let formDiv = editorBox.querySelector("#smpeople-article-form")
  41. if (!template && formDiv) {
  42. editorBox.removeChild(formDiv);
  43. }
  44. if (template) {
  45. if (!formDiv) {
  46. let formElement = document.createElement("div");
  47. formElement.setAttribute("id", "smpeople-article-form")
  48. editorBox.appendChild(formElement)
  49. formDiv = editorBox.querySelector("#smpeople-article-form")
  50. }
  51. formDiv.innerHTML = template;
  52. }
  53. };
  54. // 탬플릿 붙여넣기
  55. const attachTemplate = () => {
  56. const tabs = document.querySelectorAll('.sub-row span');
  57. tabs.forEach(tab => {
  58. const button = tab.querySelector('input');
  59. const label = tab.querySelector('label');
  60. if(button && label) {
  61. button.addEventListener('click', e => {
  62. loadTemplate(label.innerText);
  63. });
  64. }
  65. });
  66. }
  67. // onload시 한번 실행
  68. let loaded = false
  69. window.addEventListener('load', e => {
  70. if(!loaded) {
  71. attachTemplate();
  72. customForm();
  73. loaded = true
  74. }
  75. });
  76. // 커스텀 양식 함수
  77. function customForm() {
  78. // 커스텀 양식 저장
  79. let makeFormBtn = document.createElement("div")
  80. makeFormBtn.setAttribute("class", "btn btn-arca")
  81. makeFormBtn.setAttribute("id", "makeFormBtn")
  82. makeFormBtn.setAttribute("style", "margin:0 5px")
  83. makeFormBtn.append("양식 저장")
  84. makeFormBtn.onclick = function() {
  85. let doCustom = confirm("본 게시글을 양식으로 저장하시겠습니까?\n현재 선택된 탭에 본 게시글이 양식으로 설정되며,\n기존 양식을 덮어쓰게 됩니다.")
  86. if (doCustom) {
  87. checkRadio("category").then((category) => {
  88. let customForm = document.querySelector('.write-body .fr-element').innerHTML
  89. localStorage.setItem("ARCA_CUSTOM_FORM_FOR_PATH_" + location.pathname + "_AND_FOR_CATEGORY_" + category, String(customForm))
  90. })
  91. }
  92. }
  93. // 커스텀 양식 삭제
  94. let deleteFormBtn = document.createElement("div")
  95. deleteFormBtn.setAttribute("class", "btn btn-arca")
  96. deleteFormBtn.setAttribute("id", "deleteFormBtn")
  97. deleteFormBtn.setAttribute("style", "margin:0 5px")
  98. deleteFormBtn.append("양식 삭제")
  99. deleteFormBtn.onclick = function() {
  100. let doCustom = confirm("현재 선택된 탭에 설정된 양식을 삭제하시겠습니까?\n단, 기본 양식은 삭제되지 않습니다.")
  101. if (doCustom) {
  102. checkRadio("category").then((category) => {
  103. localStorage.removeItem("ARCA_CUSTOM_FORM_FOR_PATH_" + location.pathname + "_AND_FOR_CATEGORY_" + category)
  104. let form = document.querySelector("#smpeople-article-form")
  105. if(form) {
  106. form.remove()
  107. }
  108. })
  109. }
  110. }
  111. // 글머리 div에 버튼 추가
  112. let articleRow = document.querySelector("#article_write_form > div:nth-child(5)")
  113. articleRow.appendChild(makeFormBtn)
  114. articleRow.appendChild(deleteFormBtn)
  115. }
  116. // 라디오버튼 체크 함수
  117. function checkRadio(name) {
  118. return new Promise((resolve, reject) => {
  119. var radios = document.getElementsByName(name);
  120. for (let i = 0, length = radios.length; i < length; i++) {
  121. if (radios[i].checked) {
  122. checkLabel(radios[i].id).then((val) => {
  123. resolve(val)
  124. })
  125. }
  126. }
  127. })
  128. }
  129. // 레이블 체크 함수
  130. function checkLabel(value) {
  131. return new Promise((resolve, reject) => {
  132. var labels = document.querySelectorAll('.sub-row label');
  133. for (let i = 0, length = labels.length; i < length; i++) {
  134. if (labels[i].getAttribute('for') === value) {
  135. resolve(labels[i].innerText)
  136. }
  137. }
  138. })
  139. }