作者: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']
)