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

相关内容

热门资讯

交流学习经验辅助器!毛豆娱乐辅... 交流学习经验辅助器!毛豆娱乐辅助器多功能AI脚本,毛豆娱乐一贯是有辅助器脚本1、毛豆娱乐辅助软件下载...
玩家必备教程辅助!九游互娱辅助... 玩家必备教程辅助!九游互娱辅助多功能AI攻略,九游互娱本来真的是有辅助攻略1、在九游互娱插件功能辅助...
玩家必备科普外挂!新天地外挂多... 玩家必备科普外挂!新天地外挂多功能AI技巧,新天地本来真的有外挂技巧1、起透看视 新天地辅助软件价格...
揭秘真相作弊器!长盈互娱作弊器... 揭秘真相作弊器!长盈互娱作弊器多功能AI软件,长盈互娱一贯是真的有作弊器软件1、任何长盈互娱透视是真...
教学盘点辅助!四通娱乐3辅助多... 教学盘点辅助!四通娱乐3辅助多功能AIapp,四通娱乐3本来有辅助app亲,关键说明,四通娱乐3透视...
必备攻略外挂!千秋互娱外挂多功... 必备攻略外挂!千秋互娱外挂多功能AI工具,千秋互娱一直真的是有外挂工具1、玩家可以在千秋互娱透视最简...
推荐攻略开挂!七天娱乐开挂多功... 推荐攻略开挂!七天娱乐开挂多功能AIapp,七天娱乐真是存在有开挂app1、推荐攻略开挂!七天娱乐开...
记者发布透视挂!大大娱乐透视挂... 记者发布透视挂!大大娱乐透视挂多功能AI方法,大大娱乐切实真的是有透视挂方法1、玩家可以在大大娱乐透...
一分钟了解透视器!新九五透视器... 一分钟了解透视器!新九五透视器多功能AI脚本,新九五都是存在有透视器脚本进入游戏-大厅左侧-新手福利...
总算了解外挂!嘻嘻娱乐外挂多功... 总算了解外挂!嘻嘻娱乐外挂多功能AI脚本,嘻嘻娱乐一贯是有外挂脚本1、每一步都需要思考,不同水平的挑...