B站未登录用户弹窗解决方案

由于一些原因,不方便登录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;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

mYlEaVeiSmVp

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值