You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Chrome扩展需求:实现跨所有标签页监听指定服务器网络请求

Got it, let's fix this so your extension can listen to matching requests across all tabs instead of just the one you had open when launching it. The core issue right now is that your code ties the webRequest listeners to a single tab's ID—here's how to adjust it:

Step 1: Update background.js to Stop Passing Tab ID

Your current background script passes the active tab's ID to the popup, which restricts your listener to that specific tab. We'll remove that parameter since we want to monitor all tabs, and also fix your broken window positioning logic:

chrome.browserAction.onClicked.addListener(function() {
  chrome.windows.create({
    url: "headers.html", // Removed the tab.id query parameter
    type: "popup",
    width: 1800,
    height: 600
  }, function(createdWindow) {
    // Fix for your broken moveWin function: use Chrome's window API to position and focus
    chrome.windows.update(createdWindow.id, {left: 500, top: 100});
    chrome.windows.update(createdWindow.id, {focused: true});
  });
});

Step 2: Modify headers.js to Listen to All Tabs

We'll strip out the tab-specific logic and adjust the filters to target every tab that hits your desired URL pattern:

// Remove the tabId parsing—we don't need it anymore
// var tabId = parseInt(window.location.search.substring(1));

// Update filters to exclude tabId (this makes listeners apply to ALL tabs)
var filters = {urls: ["*://www.specific-page.de/*"]};

// Keep your addListeners function as-is, just using the updated filters
function addListeners() {
  chrome.webRequest.onBeforeRequest.addListener(handleEvent, filters, ['requestBody']);
  chrome.webRequest.onSendHeaders.addListener(handleEvent, filters, ['requestHeaders']);
  chrome.webRequest.onBeforeRedirect.addListener(handleEvent, filters, ['responseHeaders']);
  chrome.webRequest.onCompleted.addListener(handleEvent, filters, ['responseHeaders']);
  chrome.webRequest.onErrorOccurred.addListener(handleEvent, filters);
}

// Add cleanup to avoid duplicate listeners when re-opening the popup
window.addEventListener('unload', function() {
  removeListeners();
});

// Keep your existing removeListeners, handleEvent, formatPost, formatHeaders functions unchanged

Quick Explanation:

  • By removing tabId from your filters object, the webRequest APIs will trigger for any tab that makes a request matching your *://www.specific-page.de/* pattern.
  • The unload event listener ensures we clean up old listeners every time the popup closes, preventing duplicate event handlers from piling up.
  • Your existing manifest permissions are already sufficient for this (you have webRequest, <all_urls>, and tabs which cover everything needed).

内容的提问来源于stack exchange,提问作者peeete

火山引擎 最新活动