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'] ) 

相关内容

热门资讯

透视挂透视!德普之星辅助器,w... 透视挂透视!德普之星辅助器,wpk插件,方式教程(有人有挂)-哔哩哔哩1.德普之星辅助器 ai辅助创...
第十分钟教你!We poker... 第十分钟教你!We poker辅助器下载(透视)真是有挂,AI教程(有挂总结)-哔哩哔哩1、起透看视...
透视有挂!wepoker私人局... 透视有挂!wepoker私人局外卦(透视)竟然有挂,可靠教程(有挂稳定)-哔哩哔哩wepoker私人...
透视免费!wepoker辅助器... 透视免费!wepoker辅助器下载,wepoker私人局辅助,方案教程(有挂规律)-哔哩哔哩1、we...
第三分钟专业!wepoker私... 第三分钟专业!wepoker私人局有透视吗(透视)真是真的有挂,2025新版(的确有挂)-哔哩哔哩小...
辅助透视!约局吧如何查看是否有... 辅助透视!约局吧如何查看是否有挂(透视)一直是有挂,介绍教程(有挂存在)-哔哩哔哩1、下载好约局吧如...
透视总结!德普之星有辅助软件吗... 透视总结!德普之星有辅助软件吗,hhpoker是正品吗,手段教程(有挂总结)-哔哩哔哩1、德普之星有...
3分钟专业!wepoker手机... 3分钟专业!wepoker手机版透视脚本(透视)切实是有挂,解说技巧(有挂秘诀)-哔哩哔哩1、操作简...
透视教学!菠萝德州透视脚本(透... 透视教学!菠萝德州透视脚本(透视)都是有挂,详细教程(有挂黑科技)-哔哩哔哩;菠萝德州透视脚本软件透...
透视辅助!wpk透视工作室,h... 透视辅助!wpk透视工作室,hhpoker德州真的假的,指南教程(真的有挂)-哔哩哔哩1)hhpok...