更好的B站分享按钮

Replaces the share button with a new button to copy the page title and URL (without query parameters)

目前為 2024-08-14 提交的版本,檢視 最新版本

// ==UserScript==
// @name         更好的B站分享按钮
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Replaces the share button with a new button to copy the page title and URL (without query parameters)
// @match        *://www.bilibili.com/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    // Function to clean the URL
    function cleanUrl(url) {
        const urlObj = new URL(url);
        urlObj.search = ''; // Remove all query parameters
        return urlObj.toString();
    }

    // Function to clean the title
    function cleanTitle(title) {
        // Define possible suffixes to remove
        const suffixes = ['_哔哩哔哩', 'bilibili', '_bilibili', '哔哩哔哩', '_单机游戏热门视频'];
        // Remove suffix and anything after it
        for (const suffix of suffixes) {
            const index = title.indexOf(suffix);
            if (index !== -1) {
                return title.substring(0, index).trim();
            }
        }
        return title.trim();
    }

    // Function to copy text to clipboard
    function copyToClipboard(text) {
        navigator.clipboard.writeText(text).then(() => {
            // No visual feedback needed here
        }).catch(err => {
            console.error('Failed to copy: ', err);
        });
    }

    // Create new button to replace the old one
    function createReplacementButton() {
        const button = document.createElement('button');
        button.innerText = '点击复制分享链接';
        button.style.padding = '10px 20px';
        button.style.border = 'none'; // No border
        button.style.color = '#61666D'; // Initial text color
        button.style.backgroundColor = 'transparent'; // No background color
        button.style.borderRadius = '5px';
        button.style.cursor = 'pointer';
        button.style.fontSize = '14px';
        button.style.fontWeight = 'bold';
        button.style.transition = 'color 0.3s'; // Smooth transition for color change

        button.addEventListener('click', (event) => {
            event.stopPropagation(); // Prevent click event from bubbling up

            // Change button text color to indicate success
            button.style.color = '#00AEEC'; // Change to blue
            setTimeout(() => {
                button.style.color = '#61666D'; // Change back to original color
            }, 1000); // Restore color after 1 second

            const title = cleanTitle(document.title);
            const url = cleanUrl(window.location.href);
            const textToCopy = `【${title}】\n${url}`;
            copyToClipboard(textToCopy);
        });

        return button;
    }

    // Function to replace the original button with the new one
    function replaceShareButton() {
        const shareButton = document.getElementById('share-btn-outer');
        if (shareButton) {
            // Remove the original button
            shareButton.remove();

            // Create and position the replacement button
            const replacementButton = createReplacementButton();
            const toolbar = document.querySelector('.video-toolbar-left-main'); // Select the toolbar container
            if (toolbar) {
                toolbar.style.position = 'relative'; // Ensure the toolbar has relative positioning
                toolbar.appendChild(replacementButton);
            }
        } else {
            setTimeout(replaceShareButton, 500); // Retry after 500ms if the button is not yet found
        }
    }

    // Initial function call to start checking for the share button
    replaceShareButton();
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址