n/shareu/godeluoo · 3 个月前
注册 登录
n/share · 3 个月前 · 180 次浏览
u/godeluoo黄金

【硬核干货】B站偷降码率陷阱!Tampermonkey 油猴脚本

image
image
其他请自行测试

📄 完整脚本源码

// ==UserScript==
// @name         B站 理论最高画质 跨刷新绝对死锁版 12.2
// @namespace    http://tampermonkey.net/
// @version      12.2
// @description  利用 localStorage 跨刷新永久死锁最高 AVC 码率,切 AV1 绝不缩减
// @match        https://www.bilibili.com/bangumi/play/*
// @match        https://www.bilibili.com/video/*
// @run-at       document-start
// ==/UserScript==

(function() {
'use strict';

// 获得该视频在 localStorage 里的永久最高码率 Key
function getStorageKey() {
  return 'bili_max_quality_' + location.pathname;
}

function getStoredMax() {
  try {
    const str = localStorage.getItem(getStorageKey());
    return str ? JSON.parse(str) : null;
  } catch(e) {
    return null;
  }
}

function saveStoredMax(obj) {
  try {
    localStorage.setItem(getStorageKey(), JSON.stringify(obj));
  } catch(e) {}
}

let officialMax = getStoredMax() || {
  codec: '解析中...',
  mbps: '0.00 Mbps',
  sizeGB: '0.00 GB',
  bandwidth: 0
};

let currentUrl = location.href;
let currentDuration = 1234;
let hideTimer = null;

function parseDashData(dash) {
  if (!dash || !dash.video || dash.video.length === 0) return;
  const validVideos = dash.video.filter(v => v.bandwidth && v.bandwidth > 100000);
  if (validVideos.length === 0) return;

  const sorted = [...validVideos].sort((a, b) => b.bandwidth - a.bandwidth);
  const top = sorted[0];

  const cached = getStoredMax() || { bandwidth: 0 };

  if (top.bandwidth > cached.bandwidth) {
    const duration = dash.duration || currentDuration || 1234;
    currentDuration = duration;

    const bw = top.bandwidth;
    const mbps = (bw / 1000000).toFixed(2) + ' Mbps';
    const sizeGB = ((bw * duration / 8) / (1024 * 1024 * 1024)).toFixed(2) + ' GB';

    const codecRaw = top.codecs || '';
    let codecStr = 'AVC (H.264 / 最高原画)';
    if (codecRaw.includes('av01')) codecStr = 'AV1 (10-bit)';
    else if (codecRaw.includes('hvc') || codecRaw.includes('hev')) codecStr = top.id === 125 ? 'HEVC (HDR10)' : 'HEVC (10-bit)';

    const newMax = {
      codec: codecStr,
      mbps: mbps,
      sizeGB: sizeGB,
      bandwidth: bw
    };

    saveStoredMax(newMax);
    officialMax = newMax;
  } else if (cached.bandwidth > 0) {
    officialMax = cached;
  }
}

function processPlayUrlJSON(jsonStr) {
  try {
    const data = JSON.parse(jsonStr);
    const dash = data?.result?.video_info?.dash || data?.data?.dash || data?.result?.dash;
    if (dash?.video && dash.video.length > 0) {
      parseDashData(dash);
      triggerShow();
      return data;
    }
  } catch(e){}
  return null;
}

function getUrlString(arg) {
  if (typeof arg === 'string') return arg;
  if (arg && typeof arg === 'object') {
    if (arg.url) return arg.url;
    if (arg.href) return arg.href;
    return arg.toString();
  }
  return '';
}

const originFetch = window.fetch;
window.fetch = async function (...args) {
  const res = await originFetch.apply(this, args);
  const urlStr = getUrlString(args[0]);
  if (urlStr.includes('playurl')) {
    const clone = res.clone();
    try {
      const text = await clone.text();
      processPlayUrlJSON(text);
    } catch(e){}
  }
  return res;
};

const origOpen = XMLHttpRequest.prototype.open;
const origSend = XMLHttpRequest.prototype.send;
XMLHttpRequest.prototype.open = function(method, url) {
  this._url = getUrlString(url);
  return origOpen.apply(this, arguments);
};
XMLHttpRequest.prototype.send = function() {
  if (this._url && this._url.includes('playurl')) {
    this.addEventListener('readystatechange', function() {
      if (this.readyState === 4 && this.responseText) {
        processPlayUrlJSON(this.responseText);
      }
    });
  }
  return origSend.apply(this, arguments);
};

function checkDOMPlayInfo() {
  const playInfo = window.__playinfo__;
  const dash = playInfo?.data?.dash || playInfo?.result?.video_info?.dash || playInfo?.result?.dash;
  if (dash && dash.video && dash.video.length > 0) {
    parseDashData(dash);
    triggerShow();
  }
}

function checkUrlChange() {
  if (location.href !== currentUrl) {
    currentUrl = location.href;
    const stored = getStoredMax();
    if (stored) {
      officialMax = stored;
    } else {
      officialMax = { codec: '解析中...', mbps: '0.00 Mbps', sizeGB: '0.00 GB', bandwidth: 0 };
    }
    triggerShow();
  }
}

function triggerShow() {
  const hud = document.getElementById('bili-quality-hud-v122');
  if (hud) {
    hud.style.opacity = '1';
    if (hideTimer) clearTimeout(hideTimer);
    hideTimer = setTimeout(() => {
      hud.style.opacity = '0';
    }, 5000);
  }
}

function createHUD() {
  if (document.getElementById('bili-quality-hud-v122')) return;

  const playerArea = document.querySelector('.bpx-player-video-area') || 
                     document.querySelector('.bpx-player-container') || 
                     document.querySelector('.bilibili-player-video-wrap') ||
                     document.querySelector('#bilibili-player');
                     
  if (!playerArea) return;

  playerArea.style.position = 'relative';

  const hud = document.createElement('div');
  hud.id = 'bili-quality-hud-v122';
  hud.style.cssText = 'position: absolute; top: 15px; left: 15px; z-index: 99999; background: rgba(0,0,0,0.85); color: #fff; padding: 10px 16px; border-radius: 10px; font-size: 12px; font-family: sans-serif; pointer-events: none; backdrop-filter: blur(8px); border: 1px solid rgba(255,255,255,0.2); box-shadow: 0 6px 16px rgba(0,0,0,0.4); line-height: 1.5; transition: opacity 0.8s ease; opacity: 1;';
  hud.innerHTML = '<div style="font-weight: bold; color: #38bdf8; font-size: 13px; margin-bottom: 6px; border-bottom: 1px solid rgba(255,255,255,0.15); padding-bottom: 4px;">🎥 画质实况对比</div><div style="margin-bottom: 8px;"><div style="font-weight: bold; color: #facc15;">🔥 1. 账号理论最高画质 (全编码上限):</div><div style="padding-left: 8px;"><div>编码类型: <span id="off-codec" style="color:#facc15; font-weight:bold;">--</span></div><div>真实码率: <span id="off-bitrate" style="color:#facc15; font-weight:bold;">--</span></div><div>整体大小: <span id="off-size" style="color:#facc15; font-weight:bold;">--</span></div></div></div><div><div style="font-weight: bold; color: #4ade80;">▶️ 2. 当前正在播放 (实际加载):</div><div style="padding-left: 8px;"><div>编码类型: <span id="cur-codec" style="color:#4ade80; font-weight:bold;">--</span></div><div>真实码率: <span id="cur-bitrate" style="color:#4ade80; font-weight:bold;">--</span></div><div>整体大小: <span id="cur-size" style="color:#4ade80; font-weight:bold;">--</span></div><div id="cur-file" style="color: #94a3b8; font-size: 11px;">切片: --</div></div></div>';

  playerArea.appendChild(hud);
  triggerShow();
}

setInterval(() => {
  checkUrlChange();
  checkDOMPlayInfo();
  createHUD();
  const hud = document.getElementById('bili-quality-hud-v122');
  if (!hud) return;

  const storedMax = getStoredMax();

  const v = document.querySelector('video');
  const resources = performance.getEntriesByType('resource')
    .filter(r => r.name.includes('.m4s') && r.name.includes('/upgcxcode/'))
    .filter(r => !r.name.includes('-30280') && !r.name.includes('-30216') && !r.name.includes('-30232'))
    .reverse();

  if (resources.length > 0) {
    const latest = resources.find(r => !r.name.includes('-30280')) || resources[0];
    const url = latest.name;
    const filename = url.split('/').pop().split('?')[0];
    const bwMatch = url.match(/bw=(\d+)/);
    const bw = (bwMatch && parseInt(bwMatch[1]) > 0) ? parseInt(bwMatch[1]) : officialMax.bandwidth;
    const mbps = (bw / 1000000).toFixed(2) + ' Mbps';
    const duration = v?.duration || currentDuration || 1234;
    const sizeGB = ((bw * duration / 8) / (1024 * 1024 * 1024)).toFixed(2) + ' GB';

    let codecName = 'AVC (H.264)';
    if (filename.includes('hr1') || filename.includes('30125')) codecName = 'HEVC (HDR10)';
    else if (filename.includes('100241') || filename.includes('100027') || filename.includes('100026')) codecName = 'AV1 (10-bit)';
    else if (filename.includes('100036')) codecName = 'HEVC (10-bit)';

    if (bw > (storedMax?.bandwidth || 0)) {
      const newMax = {
        codec: codecName + ' (最高原画)',
        mbps: mbps,
        sizeGB: sizeGB,
        bandwidth: bw
      };
      saveStoredMax(newMax);
      officialMax = newMax;
    }

    const curCodecEl = document.getElementById('cur-codec');
    const curBitrateEl = document.getElementById('cur-bitrate');
    const curSizeEl = document.getElementById('cur-size');
    const curFileEl = document.getElementById('cur-file');
    
    if (curCodecEl) curCodecEl.innerText = codecName;
    if (curBitrateEl) curBitrateEl.innerText = mbps;
    if (curSizeEl) curSizeEl.innerText = sizeGB;
    if (curFileEl) curFileEl.innerText = '切片: ' + filename.slice(0, 24) + '...';
  }

  const activeMax = getStoredMax() || officialMax;

  const offCodecEl = document.getElementById('off-codec');
  const offBitrateEl = document.getElementById('off-bitrate');
  const offSizeEl = document.getElementById('off-size');
  if (offCodecEl) offCodecEl.innerText = activeMax.codec;
  if (offBitrateEl) offBitrateEl.innerText = activeMax.mbps;
  if (offSizeEl) offSizeEl.innerText = activeMax.sizeGB;
}, 1000);

})();
0
3
登录后参与讨论。
u/udc2b6nr 广告
QuicksilverVPN

快银 VPN,一键连接全球节点,线路稳定、延迟低。支持 Windows、macOS 等多平台,TUN 全局模式覆盖全部应用流量。无需复杂配置,下载即用。

umami.dnsl.top 了解更多

3 条回复

排序
  1. fiona 黄金 3 个月前

    这不得会员才能用?普通账号没法看4K吧?

    0
  2. Penelope_Udell 黄金 3 YEARS 3 个月前

    添加了脚本好像没反应

    0
  3. zxc233 黄金 3 个月前

    出租房网速限制,要求已经降级了,唉!

    0