您需要先安装一个扩展,例如 篡改猴、Greasemonkey 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 Userscripts ,之后才能安装此脚本。
您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。
您需要先安装用户脚本管理器扩展后才能安装此脚本。
替换原有分享按钮,点击后调用api生成短链接并复制页面标题和短链接到剪切板
当前为
// ==UserScript== // @name 更好的B站分享按钮 // @namespace http://tampermonkey.net/ // @version 1.3 // @description 替换原有分享按钮,点击后调用api生成短链接并复制页面标题和短链接到剪切板 // @match *://www.bilibili.com/* // @grant none // @license MIT // ==/UserScript== (function() { 'use strict'; // B站短链接 API 的 URL const SHORTEN_API_URL = 'https://api.bilibili.com/x/share/click'; // 清理 URL 中的查询参数 function cleanUrl(url) { const urlObj = new URL(url); urlObj.search = ''; // 移除所有查询参数 return urlObj.toString(); } // 清理标题中的常见后缀 function cleanTitle(title) { const suffixes = ['_哔哩哔哩', 'bilibili', '_bilibili', '哔哩哔哩', '_单机游戏热门视频']; for (const suffix of suffixes) { const index = title.indexOf(suffix); if (index !== -1) { return title.substring(0, index).trim(); } } return title.trim(); } // 将文本复制到剪切板 function copyToClipboard(text) { navigator.clipboard.writeText(text).then(() => { // 成功复制到剪切板后无需额外反馈 }).catch(err => { console.error('复制失败: ', err); }); } // 获取短链接 async function getShortenedUrl(longUrl) { try { // 生成随机参数 const build = Math.floor(Math.random() * (9000000 - 6000000 + 1)) + 6000000; const buvid = Array.from({ length: 32 }, () => Math.random().toString(36)[2]).join('') + 'infoc'; const platform = ['android', 'ios'][Math.floor(Math.random() * 2)]; const shareMode = Math.floor(Math.random() * 10) + 1; // 发送请求以获取短链接 const response = await fetch(SHORTEN_API_URL, { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded', 'User-Agent': navigator.userAgent }, body: new URLSearchParams({ build: build, buvid: buvid, oid: longUrl, platform: platform, share_channel: 'COPY', share_id: 'public.webview.0.0.pv', share_mode: shareMode }).toString() }); const data = await response.json(); if (data.data && data.data.content) { return data.data.content; // 返回短链接 } else { throw new Error('B站没有返回短链接。'); } } catch (error) { console.error('获取短链接时出错:', error); return longUrl; // 如果获取短链接失败,则返回原始长链接 } } // 创建替代按钮 function createReplacementButton() { const button = document.createElement('button'); button.innerText = '点击复制分享链接'; button.style.padding = '10px 20px'; button.style.border = 'none'; // 无边框 button.style.color = '#61666D'; // 初始文本颜色 button.style.backgroundColor = 'transparent'; // 无背景颜色 button.style.borderRadius = '5px'; button.style.cursor = 'pointer'; button.style.fontSize = '14px'; button.style.fontWeight = 'bold'; button.style.transition = 'color 0.3s'; // 平滑的颜色变化 button.addEventListener('click', async (event) => { event.stopPropagation(); // 阻止事件冒泡 // 改变按钮的文本颜色以表示成功 button.style.color = '#00AEEC'; // 更改为蓝色 setTimeout(() => { button.style.color = '#61666D'; // 一秒后恢复为原始颜色 }, 1000); const title = cleanTitle(document.title); const url = cleanUrl(window.location.href); try { const shortUrl = await getShortenedUrl(url); const textToCopy = `【${title}】\n${shortUrl}`; copyToClipboard(textToCopy); } catch (error) { console.error('获取短链接失败:', error); } }); return button; } // 替换原有分享按钮 function replaceShareButton() { const shareButton = document.getElementById('share-btn-outer'); if (shareButton) { // 移除原有按钮 shareButton.remove(); // 创建新的替代按钮并插入到页面中 const replacementButton = createReplacementButton(); const toolbar = document.querySelector('.video-toolbar-left-main'); // 选择工具栏容器 if (toolbar) { toolbar.style.position = 'relative'; // 确保工具栏具有相对定位 toolbar.appendChild(replacementButton); } } else { setTimeout(replaceShareButton, 500); // 如果未找到按钮,则每500ms重试一次 } } // 初始调用函数开始检查并替换分享按钮 replaceShareButton(); })();
QingJ © 2025
镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址