chrome插件webRequest拦截请求并获取post请求体requestBody数据raw内容,解决中文乱码问题
创始人
2025-01-08 16:32:16
0

详细使用说明可以看官方文档: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: [''] },     ['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: [''] },     ['requestBody'] ) 

相关内容

热门资讯

普及一下!拱趴大菠萝玩的是运气... 普及一下!拱趴大菠萝玩的是运气吗,越悦游戏字牌攻略,果然是真的有挂(哔哩哔哩)1、让任何用户在无需拱...
辅助一下!新道游科技透视免费版... 辅助一下!新道游科技透视免费版,蜀山四川小程序辅助,竟然有挂(哔哩哔哩)新道游科技透视免费版能透视中...
有挂一下!广西微乐小程序辅助器... 有挂一下!广西微乐小程序辅助器,牛总管辅助免费版,总是真的是有挂(哔哩哔哩)1、下载好广西微乐小程序...
揭露一下!微乐家乡app下载,... 揭露一下!微乐家乡app下载,光明大厅微信链接辅助试用,其实存在有挂(哔哩哔哩)1、光明大厅微信链接...
解密一下!辅助定制交易平台,超... 解密一下!辅助定制交易平台,超圣大厅黑科技,一贯是真的有挂(哔哩哔哩)1、该软件可以轻松地帮助玩家将...
详情一下!杭州都莱辅助软件下载... 详情一下!杭州都莱辅助软件下载,爱玩联盟辅助下载,都是真的有挂(哔哩哔哩);1、每一步都需要思考,不...
辅助一下!川娱竞技有挂透视脚本... 辅助一下!川娱竞技有挂透视脚本,新畅游互娱科技,果然真的有挂(哔哩哔哩)1、游戏颠覆性的策略玩法,独...
开挂一下!h5反杀工具,多乐手... 开挂一下!h5反杀工具,多乐手游辅助,确实是有挂(哔哩哔哩)1、多乐手游辅助有没有辅助教程、多乐手游...
揭幕一下!518互游辅助,西兵... 揭幕一下!518互游辅助,西兵互娱辅助,确实真的是有挂(哔哩哔哩)1、玩家可以在西兵互娱辅助透视最简...
辅助一下!河南微乐小程序辅助器... 辅助一下!河南微乐小程序辅助器免费,边锋老友乳山辅助,竟然真的是有挂(哔哩哔哩)1、这是跨平台的河南...