Bilibili评论区添加复制按钮

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

目前为 2021-07-21 提交的版本。查看 最新版本

// ==UserScript==
// @name         Bilibili评论区添加复制按钮
// @namespace    myitian.bili.comm-add-copy-btn
// @version      2.1
// @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==

window.onmousedown = function addCopyBtn() {
    var href = window.location.href;
    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 = '';
                var clipBoardContent_TextWithAuthor;
                var nodes;
                if (p3.parentNode.className == 'reply-box') { //判断是否为子评论
                    nodes = p3.children[1].children[0].children[2].childNodes;
                    clipBoardContent_TextWithAuthor = p3.children[1].children[0].children[0].innerText + ' [UID:' + p3.children[0].getAttribute('data-usercard-mid') + ']\n';
                } else {
                    nodes = p3.children[1].childNodes;
                    clipBoardContent_TextWithAuthor = p3.children[0].children[0].innerText + ' [UID:' + p3.children[0].children[0].getAttribute('data-usercard-mid') + ']\n';
                }
                for (var y = 0; y < nodes.length; y++) {
                    switch (nodes[y].nodeName) {
                        case '#text':
                            clipBoardContent_Text += nodes[y].textContent;
                            break;
                        case 'A':
                            var href_a = nodes[y].href;
                            var reg_v = /\/\/www\.bilibili\.com\/video\/\S+/;
                            var reg_r = /\/\/www\.bilibili\.com\/read\/\S+/;
                            if (reg_v.test(href_a) || reg_r.test(href_a)) {
                                clipBoardContent_Text += href_a.split('/')[4];
                            } else {
                                clipBoardContent_Text += nodes[y].textContent;
                            }
                            break;
                        case 'BR':
                            clipBoardContent_Text += '\n';
                            break;
                        case 'IMG':
                            var alt = nodes[y].getAttribute('alt');
                            if (alt != null) {
                                clipBoardContent_Text += alt;
                            }
                            break;
                        default:
                    }
                }
                clipBoardContent_TextWithAuthor += clipBoardContent_Text;

                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);

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

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