zod.kr 댓글 새로고침 버튼

zod.kr에서 댓글 새로고침 버튼 추가

当前为 2024-11-17 提交的版本,查看 最新版本

// ==UserScript==
// @name         zod.kr 댓글 새로고침 버튼
// @namespace    http://tampermonkey.net/
// @version      1.1
// @description  zod.kr에서 댓글 새로고침 버튼 추가
// @author       znjxl
// @match        *://zod.kr/*/*
// @require      https://code.jquery.com/jquery-3.6.0.min.js
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    // jQuery가 로드된 후 실행
    $(document).ready(function() {
        // 버튼 생성
        const refreshButton = $('<button>댓글 새로고침</button>');
        refreshButton.css({
            position: 'fixed',
            bottom: '20px',
            right: '20px',
            zIndex: '1000',
            padding: '10px',
            backgroundColor: '#007BFF',
            color: 'white',
            border: 'none',
            borderRadius: '5px',
            cursor: 'pointer'
        });

        $('body').append(refreshButton);

        // 버튼 클릭 시 댓글 새로고침
        refreshButton.on('click', () => {
            const params = { cpage: 1 }; // 현재 페이지로 새로 고침
            refreshComments(params).then(() => {
                appToast('댓글이 새로 고침되었습니다.');
            });
        });

        // 댓글 새로고침 함수
        function refreshComments(params) {
            let refresh_url = window.location.href; // 현재 URL 사용

            if (typeof params === 'object') {
                if (params.hasOwnProperty('cpage')) {
                    refresh_url = new URL(refresh_url);
                    refresh_url.searchParams.set('cpage', params.cpage);
                    refresh_url = refresh_url.toString();
                }
            }

            return fetch(refresh_url, {
                credentials: "include"
            }).then(response => response.text())
              .then(response => {
                  const selector = 'ul#app-board-comment-list';
                  const commentList = $(response).find(selector);
                  $(selector).html(commentList.html());

                  // 페이지 업데이트
                  if (params.hasOwnProperty('cpage')) {
                      const currentURL = new URL(window.location.href);
                      currentURL.searchParams.set('cpage', params.cpage);
                      window.history.pushState({}, null, currentURL);
                  }
              });
        }

        // 알림 함수
        function appToast(message) {
            const toast = $('<div></div>').text(message).css({
                position: 'fixed',
                bottom: '20px',
                right: '20px',
                backgroundColor: 'rgba(0, 123, 255, 0.9)',
                color: 'white',
                padding: '10px',
                borderRadius: '5px',
                zIndex: '1000'
            });
            $('body').append(toast);
            setTimeout(() => {
                toast.remove();
            }, 3000);
        }
    });
})();