You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Firefox扩展重发特定PUT请求问题:背景脚本无加载日志

问题:Firefox插件背景脚本无日志输出且功能未生效

需求说明

  • 开发Firefox插件,拦截所有PUT请求,当请求体JSON包含debug_source:"played_threshold_reached"时,原样重发该请求5次(含相同请求头)

已编写代码

background.js

console.log("Background script loaded");

function arrayBufferToString(buffer) {
  const arr = new Uint8Array(buffer);
  return String.fromCharCode.apply(null, arr);
}

browser.webRequest.onBeforeRequest.addListener(
  async (details) => {
    if (details.method === "PUT") {
      try {
        const requestBody = JSON.parse(arrayBufferToString(details.requestBody.raw[0].bytes));

        if (requestBody.debug_source === "played_threshold_reached") {
          console.log("Intercepted matching request!");

          const repeatRequest = async () => {
            const headers = {};
            details.requestHeaders.forEach(header => {
              headers[header.name] = header.value;
            });

            const response = await fetch(details.url, {
              method: "PUT",
              headers,
              body: JSON.stringify(requestBody),
              mode: "cors",
              credentials: "same-origin"
            });

            if (!response.ok) {
              console.error("Failed to resend request:", response.statusText);
            }
          };

          for (let i = 0; i < 5; i++) {
            console.log(`Sending request ${i + 1}`);
            await repeatRequest();
          }
        }
      } catch (error) {
        console.error("Error processing request:", error);
      }
    }
  },
  { urls: ["<all_urls>"] },
  ["requestBody", "blocking", "extraHeaders", "requestHeaders"]
);

manifest.json

{
  "manifest_version": 2,
  "name": "Request Repeater",
  "version": "1.0",
  "permissions": [
    "webRequest",
    "webRequestBlocking",
    "<all_urls>",
    "storage"
  ],
  "background": {
    "scripts": ["background.js"],
    "persistent": true
  }
}

当前问题

已确认扩展已正确添加,但无法看到背景脚本开头的"Background script loaded"日志,插件功能也未生效。

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 00:42:45