陈陈陈陈 发表于 2024-7-17 16:50:49

获取网站请求的响应内容

B站上我想获取搜索接口的响应内容,根据它来修改一下页面样式。
接口:**https://api.bilibili.com/x/web-interface/wbi/search/all/v2?__refresh__=true**
它是使用fetch来发送的,所以我在脚本上进行了重写
```
const originalFetch = window.fetch;
window.fetch = function (...args) {
        return originalFetch.apply(this, args).then((response) => {
                console.log(`Request URL: ${response.url}`);
                return response.text().then((text) => {
                        console.log(`Response Text: ${text}`);
                        return response;
                });
        });
   };
```
> 然后控制台就报错了CSP:core.a8c001e3.js:49 Refused to connect to 'https://xy182x201x240x26xy240ey90dy1101y4702yy21xy.mcdn.bilivideo.cn:4483/upgcxcode/57/33/1613663357/1613663357_x1-1-100023.m4s?agrr=1&bw=92431&logo=A0020000' because it violates the following Content Security Policy directive: "connect-src 'self' data: wss://*.bilibili.com:* *.bilibili.com *.hdslb.com *.biliapi.net *.biliapi.com".

想问一下大佬们有什么解决方法吗?

yhzc2023 发表于 2024-7-17 16:50:50

你这个hook有问题,fetch的response的body只能读一次,你在这里读了,别人再读就报错了。你用我改这个试一下
const originalFetch = window.fetch;
window.fetch = function (...args) {
return originalFetch.apply(this, args).then((response) => {
    console.log(`Request URL: ${response.url}`);
    let origin_text = response.text.bind(response);
    let origin_json = response.json.bind(response);
    response.text = async function () {
      let result = await origin_text();
      console.log(result);
      return result;
    }
    response.json = async function () {
      let result = await origin_json();
      console.log(result);
      return result;
    }
    return response
});
};

李恒道 发表于 2024-7-17 19:05:57

哥哥最好提供一个可以复现的脚本和地址测试一下
页: [1]
查看完整版本: 获取网站请求的响应内容