gpt4 book ai didi

javascript - Google Chrome - 使用 iframe 时屏幕捕获失败,相同的脚本在没有 iframe 的情况下工作

转载 作者:数据小太阳 更新时间:2023-10-29 04:42:21 24 4
gpt4 key购买 nike

当我使用以下脚本时,它适用于普通浏览器。但是当使用 iframe 时,它​​会向我显示此错误:

有谁知道造成这种情况的原因并且可以解决吗?

错误:

channel message Object {type: "getScreenPending", id: 24504, request: 6} content.js:4
channel message Object {type: "gotScreen", id: 24504, request: 6} content.js:4
>>> ShareScreen: if any err NavigatorUserMediaError {constraintName: "", message: "", name: "InvalidStateError"} test.js:1616

list .json:

{
"name": "Screen sharing",
"description": "Screensharing utility",
"version": "0.0.2",
"manifest_version": 2,
"minimum_chrome_version": "34",
"icons": {
"48" : "icon.png"
},
"permissions": [
"desktopCapture"
],
"background": {
"scripts": ["background.js"]
},
"content_scripts": [ {
"js": [ "content.js" ],
"all_frames": true,
"run_at": "document_start",
"matches": ["*://*.a.com/*", "*://*.b.com/*"]
}],
"web_accessible_resources": [
"icon.png"
]
}

背景.js:

/* background page, responsible for actually choosing media */
chrome.runtime.onConnect.addListener(function (channel) {
channel.onMessage.addListener(function (message) {
switch(message.type) {
case 'getScreen':
var pending = chrome.desktopCapture.chooseDesktopMedia(message.options || ['screen', 'window'],
channel.sender.tab, function (streamid) {
// communicate this string to the app so it can call getUserMedia with it
message.type = 'gotScreen';
message.sourceId = streamid;
channel.postMessage(message);
});
// let the app know that it can cancel the timeout
message.type = 'getScreenPending';
message.request = pending;
channel.postMessage(message);
break;
case 'cancelGetScreen':
chrome.desktopCapture.cancelChooseDesktopMedia(message.request);
message.type = 'canceledGetScreen';
channel.postMessage(message);
break;
}
});
});

content.js:

/* the chrome content script which can listen to the page dom events */
var channel = chrome.runtime.connect();
channel.onMessage.addListener(function (message) {
console.log('channel message', message);
window.postMessage(message, '*');
});

window.addEventListener('message', function (event) {
if (event.source != window)
return;
if (!event.data && (event.data.type == 'getScreen' || event.data.type == 'cancelGetScreen'))
return;
channel.postMessage(event.data);
});

最佳答案

这是因为 a 流只能由 URL 与选项卡来源匹配的框架使用。从 Chrome 40 开始,如果将 tab.url 设置为来源与框架匹配的 URL ( crbug.com/425344 ),您也可以在框架中使用流。

流只有十秒有效,所以你必须按照以下流程:

  1. 加载包含应处理流的页面的 iframe。此页面必须通过安全方案提供,例如https:chrome-extension:
  2. 将框架的原点 (location.origin) 发送到后台页面。
  3. 使用标签信息请求桌面流,并将 tab.url 设置为框架的 URL 或来源。
  4. 将 streamId 发送回框架并使用它(在十秒内)。

示例(基于问题中的代码):

var tab = channel.sender.tab;
// NEW (Chrome 40+)
tab.url = message.url; // Your custom message, e.g. {url: location.origin}
chrome.desktopCapture.chooseDesktopMedia(['screen', 'window'], tab,
function (streamid) {
// ... see question for the rest of the code
});

关于javascript - Google Chrome - 使用 iframe 时屏幕捕获失败,相同的脚本在没有 iframe 的情况下工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26424366/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com