上一主题 下一主题
ScriptCat,新一代的脚本管理器脚本站,与全世界分享你的用户脚本油猴脚本开发指南教程目录
返回列表 发新帖

获取网站请求的响应内容

[复制链接]
  • TA的每日心情
    开心
    6 天前
  • 签到天数: 2 天

    [LV.1]初来乍到

    1

    主题

    0

    回帖

    4

    积分

    助理工程师

    积分
    4
    发表于 2024-7-17 16:50:49 | 显示全部楼层 | 阅读模式
    悬赏11油猫币已解决

    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 [Report Only] 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".

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

    最佳答案

    查看完整内容

    你这个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); respons ...
  • TA的每日心情
    开心
    6 小时前
  • 签到天数: 302 天

    [LV.8]以坛为家I

    6

    主题

    29

    回帖

    216

    积分

    高级工程师

    积分
    216

    挑战者 lv2

    发表于 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
      });
    };
    回复

    使用道具 举报

  • TA的每日心情
    擦汗
    2024-7-16 09:20
  • 签到天数: 192 天

    [LV.7]常住居民III

    691

    主题

    5563

    回帖

    6462

    积分

    管理员

    非物质文化遗产社会摇传承人

    积分
    6462

    荣誉开发者管理员油中2周年生态建设者喜迎中秋

    发表于 2024-7-17 19:05:57 | 显示全部楼层
    哥哥最好提供一个可以复现的脚本和地址测试一下
    混的人。
    ------------------------------------------
    進撃!永遠の帝国の破壊虎---李恒道

    入驻了爱发电https://afdian.net/a/lihengdao666
    个人宣言:この世界で私に胜てる人とコードはまだ生まれていません。死ぬのが怖くなければ来てください。
    回复

    使用道具 举报

    发表回复

    本版积分规则

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