吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 2308|回复: 9
上一主题 下一主题
收起左侧

[Web逆向] B站未登录用户弹窗解决方案

  [复制链接]
跳转到指定楼层
楼主
hostname 发表于 2023-8-30 21:58 回帖奖励
本帖最后由 hostname 于 2023-8-31 15:23 编辑

由于一些原因,不方便登录B站,但是又喜欢在B站上听一些音乐合集,B站肯定不愿意让你白嫖,它会定时1分钟弹出一个mini的登录框,同时暂停歌曲,很烦,所以我就编写了一个油猴插件,拦截获取登录状态的接口并修改其响应,从而避免歌曲断断续续的

经分析获取登录状态的接口为:https://api.bilibili.com/x/web-interface/nav

未登录的响应如下:

{
  "code": -101,
  "message": "账号未登录",
  "ttl": 1,
  "data": {
    "isLogin": false,
    "wbi_img": {
      "img_url": "https://i0.hdslb.com/bfs/wbi/7cd084941338484aae1ad9425b84077c.png",
      "sub_url": "https://i0.hdslb.com/bfs/wbi/4932caff0ff746eab6f01bf08b70ac45.png"
    }
  }
}

登录的响应如下:

{
  "code": 0,
  "message": "0",
  "ttl": 1,
  "data": {
    "isLogin": true,
    "wbi_img": {
      "img_url": 头像图片地址,
      "sub_url": 头像图片地址
    }
  }
}

通过XMLHttpRequest发送请求

有两种思路,一是拦截XMLHttpRequest,二是拦截JSON.parse

脚本如下:

// ==UserScript==
// @name         B站未登录弹窗
// @namespace    http://tampermonkey.net/
// @version      0.1
// @run-at       document-start
// @description  try to take over the world!
// @AuThor       You
// @match        https://www.bilibili.com/*
// @grant        none
// ==/UserScript==

let oldXhrOpen = XMLHttpRequest.prototype.open;
XMLHttpRequest.prototype.open = function() {
    if (arguments[1].indexOf('/web-interface/nav') != -1) {
        let oldGet = Object.getOwnPropertyDescriptor(XMLHttpRequest.prototype, 'responseText').get;
        Object.defineProperty(this, 'responseText', {
            configurable: true,
            enumerable: true,
            get: function get() {
                let res = JSON.parse(oldGet.apply(this, arguments));
                res.code = 0;
                res.message = '0';
                res.data = {
                    isLogin: true,
                    wbi_img: []
                };
                return JSON.stringify(res);
            },
            set: undefined
        });
    }
    return oldXhrOpen.apply(this, arguments);
}
// ==UserScript==
// @name         B站未登录弹窗
// @namespace    http://tampermonkey.net/
// @version      0.1
// @run-at       document-start
// @description  try to take over the world!
// @author       You
// @match        https://www.bilibili.com/*
// @grant        none
// ==/UserScript==

let oldJsonParse = JSON.parse;
JSON.parse = function() {
    if (arguments[0].indexOf('"isLogin":false') != -1) {
        arguments[0] = arguments[0].replace('"code":-101', '"code":0').replace('"isLogin":false', '"isLogin":true');
    }
    return oldJsonParse.apply(this, arguments);
}

当然有些请求使用fetch,这里在给一个拦截fetch的插件

// ==UserScript==
// @name         拦截fetch
// @namespace    http://tampermonkey.net/
// @version      0.1
// @run-at       document-start
// @description  try to take over the world!
// @author       You
// @match        https://www.bilibili.com/*
// @grant        none
// ==/UserScript==

let oldfetch = fetch;
function fuckfetch() {
    if (arguments[0].indexOf('/web-interface/nav') != -1) {
        debugger;
        return new Promise((resolve, reject) => {
            oldfetch.apply(this, arguments).then(response => {
                const oldJson = response.json;
                response.json = function() {
                    return new Promise((resolve, reject) => {
                        oldJson.apply(this, arguments).then(result => {
                            //修改result
                            resolve(result);
                        });
                    });
                };
                resolve(response);
            });
        });
    } else {
        return oldfetch.apply(this, arguments);
    }
}
window.fetch = fuckfetch;

免费评分

参与人数 3吾爱币 +9 热心值 +3 收起 理由
笙若 + 1 + 1 谢谢@Thanks!
涛之雨 + 7 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!
鸭鸭鸭? + 1 + 1 谢谢@Thanks!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

沙发
hipojie 发表于 2023-8-31 15:18
脚本还没上架吗?
3#
HackYike 发表于 2023-9-1 11:18
4#
3729251996 发表于 2023-9-1 11:29
5#
kk007kangkai 发表于 2023-9-5 10:46
支持一下!
6#
Olook 发表于 2023-9-5 11:59
支持,现在国内这些网站不登录就各种恶心用户
7#
ak0000321 发表于 2023-9-8 11:26
费这个劲 我还不如登录一下
8#
lingxing2320 发表于 2023-9-8 19:14
期待一下吧
9#
Afanbaby 发表于 2023-9-20 15:19
支持!支持!支持!
10#
Tongtong110 发表于 2023-9-21 23:04
好像用着还可以哈,学习一下
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-1 12:16

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表