// ==UserScript== // @name 抖音直播 · 弹幕减负助手 // @namespace https://local.douyin.danmaku // @version 4.0.0 // @description 降低抖音网页版直播弹幕的渲染开销:把弹幕拉回主线程 2D 绘制、跳过文字描边、降低画布位图分辨率、关闭礼物特效层。不减少任何弹幕条数。 // @author wzyhhd // @license MIT // @match https://*.douyin.com/* // @run-at document-start // @grant none // ==/UserScript== (function () { 'use strict'; const DPR_LEVELS = [0, 1.5, 1.33, 1]; const DPR_LABELS = ['原生', '1.50', '1.33', '1.00']; const FX_LAYERS = ['#GiftEffectLayout', '#LikeLayout', '#LinkMicAnimationLayout']; const HUD_MS = 10000; const DETECT_AFTER = 120; const OPT_ENABLED = true; const FX_HIDDEN = true; const K_DPR = 'dm-tk6-dpr'; let dprIdx = parseInt(localStorage.getItem(K_DPR) || '2', 10); if (isNaN(dprIdx) || dprIdx < 0 || dprIdx >= DPR_LEVELS.length) dprIdx = 2; const stats = { fill: 0, skipped: 0, passedStroke: 0, shadow: 0 }; const fillCount = new WeakMap(); const danmakuCanvas = new WeakSet(); const canvasList = []; /* ---------- 1. 禁离屏画布 ---------- */ const killed = []; try { Object.defineProperty(window, 'OffscreenCanvas', { value: undefined, writable: true, configurable: true }); killed.push('OffscreenCanvas'); } catch (e) {} try { if (HTMLCanvasElement.prototype.transferControlToOffscreen) { Object.defineProperty(HTMLCanvasElement.prototype, 'transferControlToOffscreen', { value: undefined, writable: true, configurable: true }); killed.push('transferControlToOffscreen'); } } catch (e) {} /* ---------- 2. 伪造 DPR ---------- */ const nativeDpr = window.devicePixelRatio || 1; Object.defineProperty(window, 'devicePixelRatio', { configurable: true, get: function () { const v = DPR_LEVELS[dprIdx]; return v === 0 ? nativeDpr : v; } }); /* ---------- 3. Canvas 2D 钩子 ---------- */ function byDom(c) { if (!c || !c.closest) return false; try { return !!(c.closest('#DanmakuLayout') || c.closest('[id*="Danmaku"]') || c.closest('[id*="danmaku"]')); } catch (e) { return false; } } function mark(c) { danmakuCanvas.add(c); if (canvasList.indexOf(c) === -1) canvasList.push(c); } function isDanmaku(c) { if (!c) return false; if (danmakuCanvas.has(c)) return true; if (byDom(c)) { mark(c); return true; } if ((fillCount.get(c) || 0) >= DETECT_AFTER) { mark(c); return true; } return false; } const proto = window.CanvasRenderingContext2D && CanvasRenderingContext2D.prototype; if (proto) { const origFill = proto.fillText; const origStroke = proto.strokeText; if (origFill) { proto.fillText = function () { const c = this.canvas; if (c) { stats.fill++; const n = (fillCount.get(c) || 0) + 1; fillCount.set(c, n); if (n === DETECT_AFTER && !danmakuCanvas.has(c)) { mark(c); console.log('[DM-TK] 识别到弹幕画布 ' + c.width + 'x' + c.height + ' 父级id=' + ((c.parentElement && c.parentElement.id) || '(无)')); } } return origFill.apply(this, arguments); }; } if (origStroke) { proto.strokeText = function () { const c = this.canvas; const dm = isDanmaku(c); if (dm && OPT_ENABLED) { stats.skipped++; return; } if (dm) stats.passedStroke++; return origStroke.apply(this, arguments); }; } ['shadowBlur', 'shadowOffsetX', 'shadowOffsetY'].forEach(function (prop) { const d = Object.getOwnPropertyDescriptor(proto, prop); if (!d || !d.set) return; Object.defineProperty(proto, prop, { configurable: true, enumerable: d.enumerable, get: function () { return d.get.call(this); }, set: function (v) { if (OPT_ENABLED && isDanmaku(this.canvas)) { stats.shadow++; return d.set.call(this, 0); } return d.set.call(this, v); } }); }); } /* ---------- 4. 特效层 ---------- */ function injectStyle() { if (document.getElementById('dm-tk-style')) return; const s = document.createElement('style'); s.id = 'dm-tk-style'; s.textContent = 'html[data-dm-fx="off"] ' + FX_LAYERS.join(',html[data-dm-fx="off"] ') + '{display:none !important;}'; document.documentElement.appendChild(s); } function applyFx() { document.documentElement.setAttribute('data-dm-fx', FX_HIDDEN ? 'off' : 'on'); } /* ---------- 5. HUD ---------- */ let hud = null; let hideTimer = null; function paint() { if (!hud) return; let size = '未识别'; if (canvasList.length) { size = ''; for (let i = 0; i < canvasList.length; i++) { size += (i ? ' / ' : '') + canvasList[i].width + 'x' + canvasList[i].height; } } hud.textContent = '弹幕助手 · 离屏' + (killed.length ? '已禁' : '未禁') + ' · DPR ' + DPR_LABELS[dprIdx] + '(' + nativeDpr + ')' + ' · 画布 ' + size + ' · 描边跳过 ' + stats.skipped + ' 放过 ' + stats.passedStroke + ' · 特效' + (FX_HIDDEN ? '关' : '开') + ' · F2档位 F7显示'; } function showHud(ms) { if (!hud) return; hud.style.opacity = '1'; paint(); if (hideTimer) clearTimeout(hideTimer); hideTimer = setTimeout(function () { if (hud) hud.style.opacity = '0'; }, ms || HUD_MS); } function buildHud() { if (hud) return; hud = document.createElement('div'); hud.style.cssText = [ 'position:fixed', 'top:8px', 'left:8px', 'z-index:2147483647', 'padding:5px 9px', 'border-radius:6px', 'background:rgba(0,0,0,.62)', 'color:#fff', 'font:11px/1.5 -apple-system,Segoe UI,sans-serif', 'pointer-events:none', 'transition:opacity .35s ease', 'opacity:1', 'max-width:72vw', 'white-space:nowrap', 'overflow:hidden', 'text-overflow:ellipsis' ].join(';'); (document.body || document.documentElement).appendChild(hud); showHud(HUD_MS); } function kick() { try { window.dispatchEvent(new Event('resize')); } catch (e) {} } document.addEventListener('keydown', function (e) { if (e.key === 'F2') { e.preventDefault(); dprIdx = (dprIdx + 1) % DPR_LEVELS.length; localStorage.setItem(K_DPR, String(dprIdx)); kick(); showHud(HUD_MS); } else if (e.key === 'F7') { e.preventDefault(); showHud(HUD_MS); } }, true); injectStyle(); applyFx(); if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', buildHud); } else { buildHud(); } setTimeout(kick, 1500); setInterval(function () { if (hud && hud.style.opacity === '1') paint(); }, 1500); setTimeout(function () { console.log('[DM-TK] 离屏画布已禁:', killed.join(',') || '无'); console.log('[DM-TK] 描边:跳过 ' + stats.skipped + ' 次,放过 ' + stats.passedStroke + ' 次' + (stats.passedStroke === 0 ? '(全部跳过,无遗漏)' : '(存在未跳过的描边)')); }, 8000); })();