Bilibili评论区添加复制按钮

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

目前为 2021-08-18 提交的版本。查看 最新版本

// ==UserScript==
// @name         Bilibili评论区添加复制按钮
// @namespace    myitian.bili.comm-add-copy-btn
// @version      2.5
// @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_av = /\/\/www\.bilibili\.com\/video\/av\d+\S*/i;
                            var reg_av2 = /av\d+/i;
                            var reg_bv = /\/\/www\.bilibili\.com\/video\/[Bb][Vv][1-9a-km-zA-HJ-NP-Z]{10}\S*/;
                            var reg_bv2 = /[Bb][Vv][1-9a-km-zA-HJ-NP-Z]{10}/;
                            var reg_cv = /\/\/www\.bilibili\.com\/read\/cv\d+\S*/i;
                            var reg_cv2 = /cv\d+/i;
                            var reg_b23av = /https:\/\/b23\.tv\/av\d+\S*/i;
                            var reg_b23bv = /https:\/\/b23\.tv\/[Bb][Vv][1-9a-km-zA-HJ-NP-Z]{10}\S*/;
                            var reg_b23 = /https:\/\/b23\.tv\/\S*/i;
                            var reg_note = /https:\/\/www\.bilibili\.com\/h5\/note-app\/view\?cvid=\d+(&[\S\s]+)?/i;
                            var reg_note2 = /cvid=\d+/i;
                            if (reg_av.test(href_a)) {
                                clipBoardContent_Text += reg_av2.exec(href_a)[0];//www.bilibili.com,av
                            } else if (reg_bv.test(href_a)) {
                                clipBoardContent_Text += reg_bv2.exec(href_a)[0];//www.bilibili.com,bv
                            } else if (reg_cv.test(href_a)) {
                                clipBoardContent_Text += reg_cv2.exec(href_a)[0];//www.bilibili.com,cv
                            } else if (reg_b23av.test(href_a)) {
                                clipBoardContent_Text += reg_av2.exec(href_a)[0];//b23.tv,av
                            } else if (reg_b23bv.test(href_a)) {
                                clipBoardContent_Text += reg_bv2.exec(href_a)[0];//b23.tv,bv
                            } else if (reg_b23.test(href_a)) {
                                clipBoardContent_Text += href_a.substring(8);//b23.tv,other
                            } else if (reg_note.test(href_a)) {
                                clipBoardContent_Text += '[笔记|cv' + reg_note2.exec(href_a)[0].split('=')[1] + ']';//note(cv)
                            } else {
                                clipBoardContent_Text += nodes[y].textContent;//other,@xxx
                            }
                            break;
                        case 'BR':
                            clipBoardContent_Text += '\n';//换行
                            break;
                        case 'IMG':
                            var alt = nodes[y].getAttribute('alt');//表情包带alt
                            if (alt != null) {
                                clipBoardContent_Text += alt;
                            }
                            break;
                        case 'SPAN':
                            clipBoardContent_Text += '[' + nodes[y].textContent + ']';//置顶
                            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');
            }
        }
    }
};