0

我正在尝试记录用户屏幕以创建 gif,尝试使用 chrome API captureTab但没有成功,我尝试从未定义的内容脚本启动 API,然后从带有我的扩展代码的 iframe 启动 API,并得到异常 Unchecked runtime。 lastError:错误启动选项卡捕获 我正在使用清单版本 3

通过阅读文档,捕获 API 仅适用于前台脚本,所以也许我不明白,

我的清单

{
  "short_name": "***",
  "name": "***",
  "manifest_version": 3,
  "version": "2.1.0",
  "icons": {
    "16": "screenShotIcon.png",
    "48": "screenShotIcon.png",
    "128": "screenShotIcon.png"
  },

  "key": "***",
  "action": {
    "default_title": "***",
    "default_icon": {
      "19": "screenShotIcon.png",
      "38": "screenShotIcon.png"
    }
  },
  "permissions": ["storage", "identity", "scripting", "activeTab"],
  "optional_permissions": ["tabCapture", "downloads"],
  "web_accessible_resources": [
    {
      "resources": [
        "gif.html",
        "gif.js",

      ],
      "matches": ["<all_urls>"]
    }
  ],
  "options_page": "options.html",
  "background": {
    "service_worker": "background.js"
  },
  "commands": {
    "_execute_action": {
      "suggested_key": {
        "default": "Ctrl+Shift+Q",
        "mac": "MacCtrl+Shift+Q"
      }
    },
    "default-action": {
      "suggested_key": {
        "default": "Ctrl+Q",
        "mac": "Command+Q"
      },
      "description": "default operation "
    }
  },
  "externally_connectable": {
    "matches": ["***"]
  },
  "content_scripts": [
    {
      "matches": ["<all_urls>"],
      "js": ["content.js"]
    }
  ]
}

也是启动 iframe 的代码

iFrame = document.getElementById(popupGifName);
  if (!iFrame) {
    iFrame = document.createElement("iframe");
    iFrame.id = popupGifName;
    iFrame.style.position = "fixed";
    iFrame.style.height = "100%";
    iFrame.style.width = "100%";
    iFrame.style.border = "none";
    iFrame.style.top = "0px";
    iFrame.style.right = "0px";
    iFrame.style.zIndex = "9000000000000000000";
    document.body.appendChild(iFrame);
  }
  iFrame.src = chrome.runtime.getURL("gif.html");

最后开始捕获

const getStream = async () => {
    const active = await getActiveTab();

    chrome.tabCapture.capture(
      {
        video: true,
        audio: true,
        videoConstraints: {
          mandatory: {
            minWidth: 16,
            minHeight: 9,
            maxWidth: 854,
            maxHeight: 480,
            maxFrameRate: 60, // Note: Frame rate is variable (0 <= x <= 60).
          },
        },
      },
      (stream) => {
        console.log(stream);
      }
    );
  };

  return (
    <div>
      <button onClick={() => getStream()}>start rerecord</button>
      <video
        ref={videoRef}
        width="100px"
        height="100px"
        style={{ position: "absolute" }}
      />
    </div>
  );
}

我尝试了从后台运行 API 到弹出窗口的所有方法,但似乎没有任何效果

谢谢您的帮助

4

0 回答 0