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

相关内容

热门资讯

微扑克辅助软件!pokerma... 微扑克辅助软件!pokermastersteam外挂,(德扑之星)往昔有挂(详细透视科技教程)是一款...
德扑之星有猫腻!德扑之星可以看... 相信很多朋友都在电脑上玩过德扑之星有猫腻吧,但是很多朋友都在抱怨用电脑玩起来不方便。为此小编给大家带...
WePoKe外挂!微扑克可以用... WePoKe外挂!微扑克可以用模拟器,(AAPOKER)一贯是真的有挂(详细辅助2025教程);We...
智星德州菠萝外挂!cloudp... 智星德州菠萝外挂!cloudpoker云扑克辅助,(aapOKER)一贯真的有挂(详细透视技巧教程)...
德扑起手牌胜率图!德扑人工智能... 德扑起手牌胜率图!德扑人工智能,(来玩德州)往昔真的是有挂(详细透视微扑克教程);wpk透视辅助官方...
微扑克wpk透视辅助!德扑ai... 微扑克wpk透视辅助!德扑ai智能机器人线上代打,(Aapoker)竟然存在有挂(详细透视必胜教程)...
wpk辅助挂!aa poker... wpk辅助挂!aa poker有没有挂,(aa扑克)一贯是有挂(详细辅助大神讲解);亲,其实确实真的...
aapoker透视辅助!德州a... aapoker透视辅助!德州aapoker下载,(aa poker)都是是有挂(详细辅助第三方教程)...
透视ai代打(wPK)wpk透... 透视ai代打(wPK)wpk透视辅助工具(透视)详细辅助可靠教程(一贯存在有挂)1、全新机制【wpk...
aapoker透明挂!wpk微... aapoker透明挂!wpk微扑克有挂吗,(WEpoke)本然真的是有挂(详细辅助科技教程);超受欢...