admin管理员组

文章数量:1391951

I'm trying to capture the active YouTube tab using chrome.tabCapture.getMediaStreamId, but I'm getting an error saying:

chrome.tabCapture.getMediaStreamId error: Error: Extension has not been invoked for the current page (see activeTab permission). Chrome pages cannot be captured.

Here is the relevant part of my code in popup.js:

document.getElementById("captureButton").addEventListener("click", async () => {
    console.log("Capture button clicked");

    const [activeTab] = await chrome.tabs.query({ active: true, currentWindow: true });
    if (!activeTab?.id) {
        console.error("No active tab found");
        return;
    }
    console.log("Active tab:", activeTab.url);

    try {
        const streamId = await chrome.tabCapture.getMediaStreamId({
            targetTabId: activeTab.id,
        });

        if (!streamId) {
            console.warn("No streamId was returned.");
            return;
        } else {
            console.log("Got MediaStreamId:", streamId);
        }
    } catch (err) {
        console.error("chrome.tabCapture.getMediaStreamId error:", err);
    }
});

The log shows that the active tab URL is /, but I still get the error:

chrome.tabCapture.getMediaStreamId error: Error: Extension has not been invoked for the current page (see activeTab permission). Chrome pages cannot be captured.

I have already added the following permissions in my manifest.json:

{
    "manifest_version": 3,
    "name": "TabCapture Test",
    "version": "1.0",
    "permissions": [
      "tabCapture",
      "activeTab",
      "tabs"
    ],
    "action": {
      "default_popup": "popup.html"
    }
  }
  

But it doesn’t seem to solve the problem. The error message mentions that "Chrome pages cannot be captured," but in this case, the active tab is YouTube, which is not a Chrome internal page.

Question: Why am I still getting this error even though the active tab is YouTube and how can I successfully get the streamId for a YouTube page using chrome.tabCapture? Are there additional permissions or settings required to make this work?

Any help or guidance is appreciated!

本文标签: