通过浏览器cdp协议中的实现
1. Network.responseReceived 事件
2. Network.getResponseBody 方法
//需要获取响应数据api网址,可以是一部分,网址包含就行
const substr = '*****';
// 附加调试器并开始监听
function attachDebugger(tabId) {
chrome.debugger.attach({ tabId }, '1.3', () => {
if (chrome.runtime.lastError) {
console.error("Failed to attach debugger:", chrome.runtime.lastError.message);
return;
}
console.log(`Debugger attached to tab ${tabId}`);
// 启用 Network 域
chrome.debugger.sendCommand({ tabId }, 'Network.enable', {}, () => {
if (chrome.runtime.lastError) {
console.error("Failed to enable Network domain:", chrome.runtime.lastError.message);
return;
}
console.log("Network domain enabled.");
});
});
}
// 分离调试器
function detachDebugger(tabId) {
chrome.debugger.detach({ tabId }, () => {
if (chrome.runtime.lastError) {
// 忽略分离时的错误,比如标签页已关闭
console.log("Info:", chrome.runtime.lastError.message);
return;
}
console.log(`Debugger detached from tab ${tabId}`);
});
}
// 监听 CDP 事件
chrome.debugger.onEvent.addListener((source, method, params) => {
if (method === 'Network.responseReceived') {
const { requestId, response } = params;
// 只获取目标 API 的响应
if (response.url.includes(substr)) {
console.log(`111: ${requestId}`);
// 使用 requestId 获取响应体
chrome.debugger.sendCommand(
source,
'Network.getResponseBody',
{ requestId: requestId },
(result) => {
if (chrome.runtime.lastError) {
console.error("Failed to get response body:", chrome.runtime.lastError.message);
return;
}
//响应数据body为字符串
let bodyContent = result.body;
console.log("222:", bodyContent);
}
);
}
}
});
大佬这个咋用呢,浏览器控制选 运行后台脚本(MV2版扩展) 吗
已现浏览器插件的模式,不太好将其封装成子程序调用,如果你对浏览器插件开发和请求响应有研究或有这方面的知识储备,我可以提供思路,你可以自己尝试封装调用
就是不会这个,看来只能放弃了.0.0.