检测B站直播弹幕拦截

检测你的弹幕是否真的发出去了

< Opiniones de 检测B站直播弹幕拦截

Pregunta o comentario

§
Publicado: 04/03/2025

从昨天开始会导致完全发不出弹幕

§
Publicado: 04/03/2025

我也是,看不到自己发的,不知道发没发出去

§
Publicado: 03/04/2025

找deepseek修复了一下,但愿管用

(function () {
  // 标记是否已经处理过当前请求
  let isIntercepting = false;

  function main() {
    const originFetch = fetch;

    unsafeWindow.fetch = async function(...args) {
      const [url, options] = args;

      // 只处理特定请求且避免重复处理
      if (!isIntercepting && typeof url === 'string' && url.includes('api.live.bilibili.com/msg/send')) {
        isIntercepting = true;
        try {
          console.log('拦截到弹幕发送请求:', url);

          // 1. 先发送我们的检测请求
          const checkResult = await PostRequest(args);

          // 2. 根据检测结果处理
          if (checkResult?.msg === "f") {
            prompt("你的弹幕被吞了,该弹幕是全站屏蔽(msg:f)\n(Ctrl+C复制弹幕内容)",
                  getMessageFromOptions(options));
            isIntercepting = false;
            return Promise.reject(new Error('弹幕被全站屏蔽'));
          }
          else if (checkResult?.msg === "k") {
            prompt("你的弹幕被吞了,该弹幕是主播屏蔽(msg:k)\n(Ctrl+C复制弹幕内容)",
                  getMessageFromOptions(options));
            isIntercepting = false;
            return Promise.reject(new Error('弹幕被主播屏蔽'));
          }

          // 3. 如果检测通过,放行原始请求
          isIntercepting = false;
          return originFetch(...args);

        } catch (e) {
          console.error('拦截器出错:', e);
          isIntercepting = false;
          return originFetch(...args); // 出错时放行原始请求
        }
      }

      // 非目标请求直接放行
      return originFetch(...args);
    };
  }

  // 辅助函数:从options中提取消息
  function getMessageFromOptions(options) {
    try {
      if (options?.body) {
        const params = new URLSearchParams(options.body);
        return params.get('msg') || '';
      }
    } catch (e) {
      console.error('解析消息出错:', e);
    }
    return '';
  }

  // 使用axios创建API客户端(自动处理cookies)
  const apiClient = axios.create({
    baseURL: 'https://api.live.bilibili.com',
    withCredentials: true // 自动发送cookies
  });

  async function PostRequest(args) {
    try {
      const [_, options] = args;
      const body = options?.body ? new URLSearchParams(options.body) : null;

      const data = new FormData();
      // 安全地添加所有可能参数
      const fields = ['dm_type', 'bubble', 'color', 'fontsize', 'mode', 'msg', 'roomid', 'csrf', 'csrf_token'];
      fields.forEach(field => {
        if (body?.has(field)) {
          data.append(field, body.get(field));
        }
      });

      // 添加必要字段
      data.append('rnd', Math.round(Date.now()/1000));

      // 这里不再手动设置Cookie头,withCredentials会自动处理
      const response = await apiClient.post('/msg/send', data);

      return response.data;
    } catch (e) {
      console.error('检测请求出错:', e);
      return { code: -1, message: e.message };
    }
  }

  main();
})();

Publicar respuesta

Inicia sesión para responder.