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分钟辅助!hhpoker软件... 1分钟辅助!hhpoker软件靠谱吗,hhpoker有没有作弊辅助,项目教程(有挂实锤)1、游戏颠覆...
第二分钟辅助!川南小闲辅助器,... 第二分钟辅助!川南小闲辅助器,切实有辅助插件(新版有挂)亲,关键说明,川南小闲辅助器透视脚本安卓赛季...
十分钟辅助!德普之星私人局透视... 十分钟辅助!德普之星私人局透视,aapoker辅助软件合法吗,要领教程(有挂猫腻);1、实时aapo...
第五分钟辅助!小逸碰胡辅助,果... 第五分钟辅助!小逸碰胡辅助,果然是有辅助器(有挂讲解)1、全新机制【小逸碰胡辅助ai辅助工具激活码】...
第1分钟辅助!werplan怎... 第1分钟辅助!werplan怎么透视,wepoker怎么设置房间,方式教程(真的有挂)第1分钟辅助!...
第6分钟辅助!长春科乐辅助,竟... 第6分钟辅助!长春科乐辅助,竟然是有辅助攻略(真实有挂)长春科乐辅助是不是有人用挂微扑克wpk插件教...
三分钟辅助!hh poker软... 三分钟辅助!hh poker软件,智星德州可以透视吗,经验教程(揭秘有挂)1、下载好智星德州可以透视...
四分钟辅助!jj斗地主外卦,确... 四分钟辅助!jj斗地主外卦,确实存在有辅助挂(有挂总结)1、让任何用户在无需jj斗地主外卦安装教程第...
十分钟辅助!wepoker辅助... 十分钟辅助!wepoker辅助器免费,wepoker怎么开辅助,窍门教程(讲解有挂)1、wepoke...