Greasy Fork

Greasy Fork is available in English.

아프리카TV - 댓글 링크복사 스크립트

아프리카TV 게시글의 댓글 링크를 바로 복사할 수 있도록 도와주는 익스텐션입니다.

目前为 2024-06-28 提交的版本,查看 最新版本

// ==UserScript==
// @name         아프리카TV - 댓글 링크복사 스크립트
// @namespace    http://afreecatv.com/
// @version      2024-06-28
// @description  아프리카TV 게시글의 댓글 링크를 바로 복사할 수 있도록 도와주는 익스텐션입니다.
// @author       You
// @match        https://bj.afreecatv.com/*/post/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=afreecatv.com
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    function findComment(obj) {
        if (obj) {
            if (typeof obj === 'object' && obj.hasOwnProperty('comment')) {
                return obj.comment;
            }

            for (let key in obj) {
                if (key === 'children' || key === 'props' || !isNaN(key)) {
                    const result = findComment(obj[key]);
                    if (result){
                        return result;
                    }
                }
            }
        }
    }

    function callback(mutationsList, observer) {
        for (let mutation of mutationsList) {
            if (mutation.type === 'childList') {
                mutation.addedNodes.forEach(node => {
                    if (node.nodeType === Node.ELEMENT_NODE && node.matches('div > section > ul.cmmt-list > li')) {
                        for (let attr in node) {
                            if (attr.startsWith('__reactEventHandlers$')) {
                                let handlers = node[attr];
                                if (handlers) {
                                    const comment = findComment(handlers);
                                    const link_btn = document.createElement('button');
                                    link_btn.textContent = "링크복사";
                                    link_btn.setAttribute("type", "button");
                                    link_btn.classList.add("btn-basic");
                                    link_btn.addEventListener('click', (event) => {
                                        const url = location.origin + location.pathname + (comment ? "#comment_noti" + comment.p_comment_no : "");
                                        navigator.clipboard.writeText(url).then( () => alert(url + '\n클립보드에 링크가 복사되었습니다.'));
                                    });

                                    // attach button
                                    node.querySelector(".cmmt-btn")?.append(link_btn);
                                }
                                return;
                            }
                        }
                    }
                });
            }
        }
    }

    const observer = new MutationObserver(callback);
    observer.observe(document.body, { childList: true, subtree: true });

})();