bilibili分享小助手

bilibili复制分享纯净链接

// ==UserScript==
// @name         bilibili分享小助手
// @namespace    http://tampermonkey.net/
// @version      2.0
// @description  bilibili复制分享纯净链接
// @author       You
// @match        https://www.bilibili.com/video/*
// @match        https://www.bilibili.com/list/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=bilibili.com
// @grant        GM_addStyle
// @license MIT
// ==/UserScript==
(function() {
    'use strict';

    // 第一个脚本的功能
    function addFirstButton() {
        var targetElement = document.querySelector('div.video-toolbar-left-main');
        if (targetElement) {
            var newDivWrap = document.createElement('div');
            newDivWrap.className = 'toolbar-left-item-wrap-csx';

            var newButton = document.createElement('button');
            newButton.style.cssText = 'background-color: #1c2022; color: white; border: none; padding: 10px 20px; cursor: pointer;';
            newButton.textContent = '点击复制';
            newButton.className = 'new-custom-button';

            newButton.addEventListener('click', function() {
                var titleElement = document.querySelector('h1.video-title.special-text-indent');
                var title = titleElement ? titleElement.getAttribute('data-title') : '';
                var url = window.location.href.substring(0, 44);
                var fullText = `「${title}」 ${url}`;

                navigator.clipboard.writeText(fullText).then(() => {
                    showTooltip(newButton, '已复制: ' + fullText);
                }).catch(err => {
                    console.error('复制文本失败: ', err);
                });
            });

            newDivWrap.appendChild(newButton);
            targetElement.appendChild(newDivWrap);
        }
    }

    // 第二个脚本的功能
    function addSecondButton() {
        var targetElement = document.querySelector('div.video-toolbar-left-main');
        if (targetElement) {
            var newDivWrap = document.createElement('div');
            newDivWrap.className = 'toolbar-left-item-wrap-csx';

            var newButton = document.createElement('button');
            newButton.style.cssText = 'background-color: #1c2022; color: white; border: none; padding: 10px 20px; cursor: pointer;';
            newButton.textContent = '点击复制(精准空降)';
            newButton.className = 'new-custom-button';

            newButton.addEventListener('click', function() {
                var titleElement = document.querySelector('h1.video-title.special-text-indent');
                var title = titleElement ? titleElement.getAttribute('data-title') : '';

                var currentTime = 0;
                var video = document.querySelector('video');
                if (video) {
                    currentTime = video.currentTime;
                } else {
                    var timeElement = document.querySelector('span.bpx-player-ctrl-time-current');
                    if (timeElement) {
                        var timeParts = timeElement.textContent.trim().split(':').reverse();
                        currentTime = timeParts.reduce((acc, part, index) => acc + parseFloat(part) * Math.pow(60, index), 0);
                    }
                }

                var baseUrl = window.location.href.split('?')[0];
                var url = `${baseUrl}?t=${currentTime.toFixed(1)}`.replace(/(\.\d*?[1-9])0+$/, '$1');
                var fullText = `「${title}」 ${url}`;

                navigator.clipboard.writeText(fullText).then(() => {
                    showTooltip(newButton, '已复制: ' + fullText);
                }).catch(err => {
                    console.error('复制失败:', err);
                });
            });

            newDivWrap.appendChild(newButton);
            targetElement.appendChild(newDivWrap);
        }
    }

    // 共用的提示功能
    function showTooltip(button, message) {
        var tooltip = document.createElement('div');
        tooltip.style.cssText = 'position: absolute; background-color: black; color: white; padding: 5px 10px; border-radius: 5px; font-size: 14px; z-index: 1000; opacity: 0.9; transform: translateY(-10px); transition: opacity 0.5s ease-out;';
        tooltip.textContent = message;

        var rect = button.getBoundingClientRect();
        tooltip.style.left = rect.left + 'px';
        tooltip.style.top = rect.top - tooltip.offsetHeight - 5 + 'px';

        document.body.appendChild(tooltip);
        setTimeout(() => document.body.removeChild(tooltip), 2000);
    }

    // 同时初始化两个按钮
    setTimeout(() => {
        addFirstButton();
        addSecondButton();
    }, 3500);
})();