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

相关内容

热门资讯

透视挂!aapoker发牌逻辑... 透视挂!aapoker发牌逻辑(透视)底牌透视挂辅助app(可靠开挂辅助详细教程)-哔哩哔哩是一款可...
透视真的!哈糖大菠萝有挂(透视... 透视真的!哈糖大菠萝有挂(透视)底牌透视挂辅助机器人(可靠开挂辅助技巧教程)-哔哩哔哩;相信小伙伴都...
透视新版!wepoker插件功... 透视新版!wepoker插件功能辅助器(透视)底牌透视挂辅助工具(可靠开挂辅助AA德州教程)-哔哩哔...
透视数据!wepoker养号规... 透视数据!wepoker养号规律(透视)底牌透视挂辅助程序(可靠开挂辅助规律教程)-哔哩哔哩;大家肯...
透视黑科技!wepoker辅助... 透视黑科技!wepoker辅助器软件下载(透视)底牌透视挂辅助系统(可靠开挂辅助专业教程)-哔哩哔哩...
透视工具!德普之星辅助器(透视... 透视工具!德普之星辅助器(透视)底牌透视挂辅助软件(可靠开挂辅助普及教程)-哔哩哔哩是一款可以让一直...
透视好友房!wepoker代打... 您好,wepoker代打辅助机器人这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302...
透视总结!wepoker透视功... 透视总结!wepoker透视功能下载(透视)底牌透视挂辅助插件(可靠开挂辅助扑克教程)-哔哩哔哩是一...
透视免费!hhpoker辅助挂... 透视免费!hhpoker辅助挂是真的(透视)底牌透视挂辅助软件(可靠开挂辅助可靠技巧)-哔哩哔哩;亲...
透视攻略!德州局透视脚本下载安... 透视攻略!德州局透视脚本下载安装最新版本(透视)底牌透视挂辅助器(可靠开挂辅助安装教程)-哔哩哔哩;...