완장들 글 수정 버튼 안보
다른서브는 모르겠는데, 내가 완장인 서브에서는 내 글 수정, 삭제 버튼이 안보이더라고요
기능 자체가 없는 건 아니고,
그냥 버튼만 안 보이는 문제라 템퍼몽키 스크립트로 우회해서 띄울 수는 있습니다
수정이 되었으면 하네요.
코드
// UserScript
// @name KONE 수정 버튼 복구
// @namespace konevi
// @version 0.1.1
// @description KONE에서 본인 글의 수정 버튼이 사라지는 경우 수정 버튼을 복구합니다.
// @match https://kone.gg/s/*/*
// @run-at document-idle
// @grant none
// /UserScript
(() => {
'use strict';
const BUTTON_ATTR = 'data-konevi-edit-restore';
const ownershipCache = new Map();
let scheduled = false;
let checkingPath = null;
// ============================================================
// 현재 주소가 글 페이지인지 확인
// ============================================================
function getArticleRoute() {
const match = location.pathname.match(
/^\/s\/([^/]+)\/([^/]+)\/?$/
);
if (!match) return null;
const [, sub, articleId] = match;
return {
sub,
articleId,
path: location.pathname,
editPath: /s/${sub}/write/${articleId}
};
}
// ============================================================
// 기존 KONE 수정 버튼 확인
// ============================================================
function hasNativeEditButton(editPath) {
return [
...document.querySelectorAll('a')
].some(anchor => {
const href =
anchor.getAttribute('href');
return (
href === editPath ||
href === ${location.origin}${editPath}
);
});
}
// ============================================================
// 현재 글이 로그인한 본인의 글인지 확인
// ============================================================
async function isOwnArticle(path) {
if (ownershipCache.has(path)) {
return ownershipCache.get(path);
}
try {
const response = await fetch(
${path}/__data.json?x-sveltekit-invalidated=1111,
{
credentials: 'include'
}
);
if (!response.ok) {
return false;
}
const json =
await response.json();
const sessionData =
json?.nodes?.[0]?.data;
const articleData =
json?.nodes?.[2]?.data;
if (
!Array.isArray(sessionData) ||
!Array.isArray(articleData)
) {
return false;
}
// --------------------------------------------------------
// 로그인한 사용자 handle
// --------------------------------------------------------
let myHandle = null;
try {
const root =
sessionData[0];
const session =
sessionData[root.session];
const profile =
sessionData[session.profile];
myHandle =
sessionData[profile.handle];
} catch (_) {
return false;
}
if (
!myHandle ||
typeof myHandle !== 'string'
) {
return false;
}
// --------------------------------------------------------
// 현재 글 작성자 handle
// --------------------------------------------------------
let writerHandle = null;
for (
let i = 0;
i < articleData.length;
i++
) {
const article =
articleData[i];
if (
!article ||
typeof article !== 'object' ||
Array.isArray(article) ||
!('writer' in article)
) {
continue;
}
try {
const writer =
articleData[article.writer];
if (
!writer ||
typeof writer !== 'object' ||
Array.isArray(writer) ||
!('handle' in writer)
) {
continue;
}
const handle =
articleData[writer.handle];
if (
typeof handle !== 'string'
) {
continue;
}
// 현재 글 객체로 보이는 항목 우선
if (
'title' in article ||
'content' in article ||
'myVote' in article
) {
writerHandle =
handle;
break;
}
} catch (_) {}
}
const result =
!!writerHandle &&
writerHandle === myHandle;
ownershipCache.set(
path,
result
);
return result;
} catch (error) {
console.warn(
'[KONE 수정 버튼 복구] 작성자 확인 실패:',
error
);
return false;
}
}
// ============================================================
// 실제 글 제목 H1 찾기
//
// 제목 글자 수 제한 없음.
// "ㅎ", "ㅎㅇ", 긴 제목 모두 동일하게 처리.
// ============================================================
function findArticleTitle() {
return [
...document.querySelectorAll('h1')
].find(element => {
const rect =
element.getBoundingClientRect();
const style =
getComputedStyle(element);
return (
rect.width > 0 &&
rect.height > 0 &&
style.display !== 'none' &&
style.visibility !== 'hidden'
);
}) || null;
}
// ============================================================
// 제목이 들어있는 가로 행 찾기
// ============================================================
function findTitleRow() {
const title =
findArticleTitle();
if (!title) {
return null;
}
let row =
title.parentElement;
const titleRect =
title.getBoundingClientRect();
for (
let i = 0;
i < 6 && row;
i++
) {
const rect =
row.getBoundingClientRect();
if (
rect.width >
titleRect.width + 150
) {
return row;
}
row =
row.parentElement;
}
return title.parentElement;
}
// ============================================================
// 수정 버튼 생성
// ============================================================
function createEditButton(editPath) {
const wrapper =
document.createElement('div');
wrapper.setAttribute(
BUTTON_ATTR,
'1'
);
Object.assign(
{
display: 'flex',
alignItems: 'center',
marginLeft: 'auto',
flexShrink: '0'
}
);
const button =
document.createElement('button');
button.type = 'button';
button.innerHTML = `
<span style="
display:flex;
align-items:center;
justify-content:center;
width:16px;
height:16px;
">
<svg
viewBox="0 0 24 24"
width="16"
height="16"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
>
<path d="M12 20h9"></path>
<path d="M16.5 3.5a2.1 2.1 0 0 1 3 3L7 19l-4 1 1-4Z"></path>
</svg>
</span>
<span>수정</span>
`;
Object.assign(
{
height: '36px',
padding: '0 14px',
display: 'inline-flex',
alignItems: 'center',
justifyContent: 'center',
gap: '6px',
border:
'1px solid rgba(255,255,255,.18)',
borderRadius: '10px',
background: 'transparent',
color: 'inherit',
fontFamily: 'inherit',
fontSize: '14px',
fontWeight: '600',
lineHeight: '1',
cursor: 'pointer',
transition:
'background-color .15s, border-color .15s'
}
);
button.addEventListener(
'mouseenter',
() => {
button.style.background =
'rgba(255,255,255,.06)';
}
);
button.addEventListener(
'mouseleave',
() => {
button.style.background =
'transparent';
}
);
button.addEventListener(
'click',
() => {
location.href =
editPath;
}
);
wrapper.appendChild(
button
);
return wrapper;
}
// ============================================================
// 잘못 남아있는 복구 버튼 제거
// ============================================================
function removeRestoredButtons() {
document
.querySelectorAll(
[${BUTTON_ATTR}]
)
.forEach(element =>
element.remove()
);
}
// ============================================================
// 수정 버튼 복구
// ============================================================
async function ensureEditButton() {
const route =
getArticleRoute();
if (!route) {
removeRestoredButtons();
return;
}
// KONE 원래 수정 버튼이 있으면 건드리지 않음
if (
hasNativeEditButton(
route.editPath
)
) {
removeRestoredButtons();
return;
}
// 이미 복구된 버튼 존재
if (
document.querySelector(
[${BUTTON_ATTR}]
)
) {
return;
}
if (
checkingPath ===
route.path
) {
return;
}
checkingPath =
route.path;
const checkedPath =
route.path;
const own =
await isOwnArticle(
route.path
);
checkingPath = null;
// 검사 도중 다른 페이지로 이동
if (
location.pathname !==
checkedPath
) {
return;
}
// 남의 글
if (!own) {
return;
}
// 검사하는 사이 KONE 원래 버튼이 생김
if (
hasNativeEditButton(
route.editPath
)
) {
return;
}
const row =
findTitleRow();
if (!row) {
return;
}
const rowStyle =
getComputedStyle(row);
if (
rowStyle.display !== 'flex'
) {
row.style.display =
'flex';
row.style.alignItems =
'center';
'12px';
}
const button =
createEditButton(
route.editPath
);
row.appendChild(
button
);
}
// ============================================================
// 실행 예약
// ============================================================
function schedule() {
if (scheduled) {
return;
}
scheduled = true;
requestAnimationFrame(() => {
scheduled = false;
ensureEditButton();
});
}
// ============================================================
// KONE SPA DOM 변경 감지
// ============================================================
const observer =
new MutationObserver(
schedule
);
observer.observe(
document.documentElement,
{
childList: true,
subtree: true
}
);
// ============================================================
// SPA 주소 이동 대응
// ============================================================
for (
const method of [
'pushState',
'replaceState'
]
) {
const original =
history[method];
history[method] =
function (...args) {
const previousPath =
location.pathname;
const result =
original.apply(
this,
args
);
if (
previousPath !==
location.pathname
) {
removeRestoredButtons();
}
schedule();
return result;
};
}
window.addEventListener(
'popstate',
() => {
removeRestoredButtons();
schedule();
}
);
// 최초 실행
schedule();
})();
s/feedback
• 279 subscribers- 1. 주제 이외 게시물