当前位置:首页 » 《关注互联网》 » 正文

chrome插件webRequest拦截请求并获取post请求体requestBody数据raw内容,解决中文乱码问题

22 人参与  2024年04月19日 16:30  分类 : 《关注互联网》  评论

点击全文阅读


详细使用说明可以看官方文档:https://developer.chrome.com/docs/extensions/reference/api/webRequest?hl=zh-cn

拦截操作 

想要通过浏览器插件拦截请求的话,需要在manifest.json里面添加webRequet权限:

拦截请求代码放在background.js里面:(下面代码解析中文乱码)

export {}console.log(    'Live now; make now always the most precious time. Now will never come again.')// 监听发送请求chrome.webRequest.onBeforeRequest.addListener(    function (details) {        console.log('请求详情', details)        if (details.method == 'POST') {            var postedString = decodeURIComponent(                String.fromCharCode.apply(                    null,                    new Uint8Array(details.requestBody.raw[0].bytes)                )            )            console.log('请求体内容', postedString)        }        return { cancel: false }    },    { urls: ['<all_urls>'] },    ['requestBody'])

 拦截到的post请求详情是:

可以看到requestBody内容是raw格式数据: 里面是bytes字节类型

想要看到原始内容,需要解析raw数据:(这种方式会中文乱码)

        if (details.method == 'POST') {            var postedString = decodeURIComponent(                String.fromCharCode.apply(                    null,                    new Uint8Array(details.requestBody.raw[0].bytes)                )            )            console.log('请求体内容', postedString)        }

解析后的数据就是明文了: 

解决中文乱码

中文乱码主要出现是因为 String.fromCharCode.apply 引起的。想要解决中文乱码就要换一种解码方式,使用TextDecoder解码就可以了:

            const decoder = new TextDecoder('utf-8')            var postedString = decoder.decode(                new Uint8Array(details?.requestBody?.raw[0].bytes)            )            console.log('请求体内容', postedString)

可以看到中文正常显示了: 

 总的代码在background.js里面:

import { escape } from 'querystring'export {}console.log(    'Live now; make now always the most precious time. Now will never come again.')// 监听发送请求chrome.webRequest.onBeforeRequest.addListener(    function (details) {        console.log('请求详情', details)        if (details.method == 'POST') {            const decoder = new TextDecoder('utf-8')            var postedString = decoder.decode(                new Uint8Array(details?.requestBody?.raw[0].bytes)            )            console.log('请求体内容', postedString)        }        return { cancel: false }    },    { urls: ['<all_urls>'] },    ['requestBody'])

点击全文阅读


本文链接:http://zhangshiyu.com/post/97679.html

<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

最新文章

  • 完结文真相与背叛来的让人猝不及防赵宇程列表_完结文真相与背叛来的让人猝不及防赵宇程(时云悦)
  • [星月]完结版免费在线阅读_靳时川靳总星星节选免费试读
  • 被休后我盖房屯粮肉满仓免费(叶盼汣)_被休后我盖房屯粮肉满仓免费叶盼汣
  • (流年落尽空白首免费)全文资源(向婉宁顾辞)_流年落尽空白首免费列表_笔趣阁向婉宁顾辞
  • 他曾满眼温柔季乐汐在线列表_他曾满眼温柔季乐汐在线(方慕琛)
  • 霍司年梁岁花(似月光吻野风:结局+番外)结局_(霍司年梁岁花似月光吻野风:结局+番外全书结局)结局列表_笔趣阁(霍司年梁岁花)
  • [为了竹马,妻子催眠我四次]小说免费在线阅读_林婉清韩慕辰慕辰章节世界观揭秘篇‌
  • 「爱是沉默的孤单」精彩节选试读_[陈行简林姐过敏]完结版全文
  • 宋逸琛安熙柠:+后续+结局必读爽文完本_完本宋逸琛安熙柠:+后续+结局必读爽文
  • (番外)+(全书)瑶华澹台冥苍(神祭台上仙缘尽:结局+番外)_(瑶华澹台冥苍)列表_笔趣阁(神祭台上仙缘尽:结局+番外)
  • (番外)+(全书)我结婚后,前世为了白月光殉情的丈夫却哭疯了:结局+番外(许念苏瑾安)_(我结婚后,前世为了白月光殉情的丈夫却哭疯了:结局+番外)列表_笔趣阁(许念苏瑾安)
  • 触摸不到的月光:结局+番外(许信川周羽瑶)_(触摸不到的月光:结局+番外)列表_笔趣阁(许信川周羽瑶)

    关于我们 | 我要投稿 | 免责申明

    Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1