// ==UserScript== // @name 【意一拾】Bilibili字幕点击复制 // @namespace http://tampermonkey.net/ // @version 0.4 // @license MIT // @icon https://cdn.jsdelivr.net/gh/ijjie/icons@main/114.ico // @description 意一拾 · 吾一试 —— 视频意境,一拾即得。 // @match https://www.bilibili.com/* // @grant none // ==/UserScript== (function() { 'use strict'; // 通用复制函数(优先 Clipboard API,失败则降级) function copyText(text) { if (navigator.clipboard && navigator.clipboard.writeText) { navigator.clipboard.writeText(text) .then(() => { console.log('✅ 已复制字幕(Clipboard API):', text); }) .catch(() => { fallbackCopy(text); }); } else { fallbackCopy(text); } } function fallbackCopy(text) { const textarea = document.createElement('textarea'); textarea.value = text; textarea.style.position = 'fixed'; textarea.style.opacity = '0'; textarea.style.left = '-9999px'; document.body.appendChild(textarea); textarea.focus(); textarea.select(); try { const success = document.execCommand('copy'); if (success) { console.log('✅ 已复制字幕(降级方案):', text); } else { console.error('❌ 降级复制失败'); } } catch (e) { console.error('❌ 复制异常:', e); } document.body.removeChild(textarea); } // 使用捕获阶段监听点击事件 document.addEventListener('click', function(e) { // 调试:查看点击目标(可以注释掉) // console.log('【点击目标】', e.target); // 可能的选择器列表(按优先级) const selectors = [ '.bili-subtitle-x-subtitle-panel-text', // 当前有效 '.bpx-player-subtitle-text', // 旧版备用 '[role="caption"]', // 通用属性 '.subtitle-item' // 其他可能 ]; let subtitle = null; for (let sel of selectors) { subtitle = e.target.closest(sel); if (subtitle) { // console.log('匹配到选择器:', sel); break; } } if (!subtitle) { // 可取消注释以查看未匹配的点击 // console.log('未匹配到字幕元素'); return; } const text = subtitle.textContent.trim(); if (!text) return; // 执行复制 copyText(text); // 视觉反馈:短暂高亮(不影响原有样式) subtitle.style.transition = 'background 0.2s, color 0.2s'; subtitle.style.background = '#ffeb3b'; subtitle.style.color = '#000'; setTimeout(() => { subtitle.style.background = ''; subtitle.style.color = ''; }, 400); }, true); // 捕获阶段 console.log('【脚本】B站字幕点击复制已加载(稳定版)'); })();