WhatsApp云控,Line云控,Telegram云控,Viber云控,Zalo云控,TikTok云控
WhatsApp云控|WhatsApp协议号

合作咨询 TG:XH518178
创造有活力的品牌网站 提升用户体验和品牌价值感

如何拦截并重定向请求?

人气 

作者:Ran

2020-10-15 Ran

以WhatsApp网页版为例,通过拦截并重定向请求可以注入自定义逻辑。

/**

* 请求重定向

*/

chrome.webRequest.onBeforeRequest.addListener(

function (details) {

const matchURL = details.url

const replaceBaseURL = ‘http://localhost:3001’

const scriptReg = /https:\/\/web\.whatsapp\.com\/([^.]+)\.?([^.]*)\.js/

const fileName = scriptReg.exec(matchURL)[1]

const fileHash = scriptReg.exec(matchURL)[2]

const replaceURL = `${replaceBaseURL}/whatsapp/override/${fileName}${fileHash ? ‘.’ + fileHash : ”}.js`

console.log(`onBeforeRequest: ${matchURL} → ${replaceURL}`, new Date())

return {

redirectUrl: replaceURL

}

},

{

urls: [

‘https://web.whatsapp.com/app.*.js’,

‘https://web.whatsapp.com/app2.*.js’,

],

types: [‘script’]

},

[‘blocking’]

)