Bilibili评论区添加复制按钮

向评论-操作列表(评论右下的三个点打开的菜单)添加复制按钮

目前为 2021-05-03 提交的版本。查看 最新版本

// ==UserScript==
// @name         Bilibili评论区添加复制按钮
// @namespace    myitian.bili.comm-add-copy-btn
// @version      1.0
// @description  向评论-操作列表(评论右下的三个点打开的菜单)添加复制按钮
// @author       Myitian
// @match        https://www.bilibili.com/*
// @match        https://t.bilibili.com/*
// @match        https://h.bilibili.com/*
// @match        https://space.bilibili.com/*
// @match        https://manga.bilibili.com/*
// @match        https://live.bilibili.com/*
// @grant        GM_setClipboard
// ==/UserScript==

var href = window.location.href;
window.onmousedown = function addCopyBtn() {
    var oprLst = document.getElementsByClassName('opera-list'); //获取目标元素
    if (oprLst.length != 0) {

        var reg_t = /^https:\/\/t\.bilibili\.com(((\/[^\d])|(\?|\#))\S*|\/)?$/;
        var reg_s = /^https:\/\/space\.bilibili\.com\S*$/;
        var reg_l = /^https:\/\/live\.bilibili\.com\S*$/;
        var noLinkCopy = (reg_t.test(href) || reg_s.test(href) || reg_l.test(href));

        for (var x = 0; x < oprLst.length; x++) {
            if (!oprLst[x].hasAttribute('data-changed')) { //判断是否已被更改,若否,则继续

                var p3 = oprLst[x].parentNode.parentNode.parentNode;

                if (!noLinkCopy) { //排除动态首页、个人空间、直播页

                    var clipBoardContent_Link;
                    if (p3.parentNode.className == 'reply-box') { //判断是否为子评论
                        clipBoardContent_Link = href + '#reply' + p3.getAttribute('data-id');
                    } else {
                        clipBoardContent_Link = href + '#reply' + p3.parentNode.getAttribute('data-id');
                    }

                    var copyLink = document.createElement('li');
                    copyLink.className = 'myitian-copybtn';
                    copyLink.setAttribute('data-copycontent', clipBoardContent_Link);
                    copyLink.onclick = function (event) { var obj = document.elementFromPoint(event.clientX, event.clientY); GM_setClipboard(obj.getAttribute('data-copycontent')); };
                    copyLink.innerText = '复制链接';
                    oprLst[x].firstChild.appendChild(copyLink);
                }

                var clipBoardContent_Text
                if (p3.parentNode.className == 'reply-box') { //判断是否为子评论
                    clipBoardContent_Text = p3.childNodes[1].firstChild.childNodes[2].innerText;
                } else {
                    clipBoardContent_Text = p3.childNodes[1].innerText;
                }
                var copyText = document.createElement('li');
                copyText.className = 'myitian-copybtn';
                copyText.setAttribute('data-copycontent', clipBoardContent_Text);
                copyText.onclick = function (event) { var obj = document.elementFromPoint(event.clientX, event.clientY); GM_setClipboard(obj.getAttribute('data-copycontent')); };
                copyText.innerText = '复制文字';
                oprLst[x].firstChild.appendChild(copyText);

                // [ WORK IN PROGRESS ]
                //var copyRaw = document.createElement('li');
                //copyRaw.className = 'myitian-copybtn';
                //copyRaw.onclick= function (){GM_setClipboard(clipBoardContent_Raw);};
                //copyRaw.innerText = '复制原始'
                //
                //oprLst[x].firstChild.appendChild(copyRaw);

                oprLst[x].setAttribute('data-changed', 'true');
            }
        }
    }
};