2

Chrome 拡張機能を試してchrome.desktopCapture.chooseDesktopMediaいますが、デスクトップ ストリームを問題なく取得できます。

次のように、バックグラウンド スクリプトでストリームをblob:-URL に変換するために次を使用しています。

var objectUrl = URL.createObjectURL(stream);

うまくいかないように見えるのは、挿入されたページのビデオ要素の src 属性としてこれを設定する方法です。

次のことを試しましたが、それぞれが機能しません。

Background.js で:

var video     = document.getElementById("video");
var objectUrl = URL.createObjectURL(stream);
video.src     = objectUrl;

Content.js 内

//objectUrl is a string received in a message from the background page by the content page
var video     = document.getElementById("video");
video.src     = objectUrl;

javascript コンソールで次のように表示されます。

ローカル リソースの読み込みが許可されていません: blob:chrome-extension://panahgiakgfjeioddhenaabbacfmkclm/48ff3e53-ff6a-4bee-a1dd-1b8844591a91

挿入されたページまでずっとメッセージに URL を投稿した場合も同じ結果になります。これは機能するはずですか?ここでアドバイスをいただければ幸いです。

私のマニフェストにもあります "web_accessible_resources": [ "*" ]が、それはこの問題を解決したかどうかを確認するためだけでした(解決しませんでした)。

4

2 に答える 2

5

コンテンツ スクリプトでは、DOM はページと共有されるため、すべての DOM 操作 ( video の設定など) は、拡張機能ではなく、ページの同一生成元ポリシーsrcに従います。

タブのコンテンツを表示したい場合は、tab.Tabオブジェクトを に渡さなければなりませんchrome.desktopCapture.chooseDesktopMediaこのオブジェクトは、メッセージ パッシングタブAPIなど、さまざまな方法で取得できます。拡張ボタンを使用した例を次に示します。

background.js

chrome.browserAction.onClicked.addListener(function(tab) {
    // NOTE: If you want to use the media stream in an iframe on an origin
    // different from the top-level frame (e.g. http://example.com), set
    //  tab.url = 'http://example.com'; before calling chooseDesktopMedia!
    //  (setting tab.url only works in Chrome 40+)
    chrome.desktopCapture.chooseDesktopMedia([
        'screen', 'window'//, 'tab'
    ], tab, function(streamId) {
        if (chrome.runtime.lastError) {
            alert('Failed to get desktop media: ' +
                chrome.runtime.lastError.message);
            return;
        }   

        // I am using inline code just to have a self-contained example.
        // You can put the following code in a separate file and pass
        // the stream ID to the extension via message passing if wanted.
        var code = '(' + function(streamId) {
            navigator.webkitGetUserMedia({
                audio: false,
                video: {
                    mandatory: {
                        chromeMediaSource: 'desktop',
                        chromeMediaSourceId: streamId
                    }   
                }   
            }, function onSuccess(stream) {
                var url = URL.createObjectURL(stream);
                var vid = document.createElement('video');
                vid.src = url;
                document.body.appendChild(vid);
            }, function onError() {
                alert('Failed to get user media.');
            }); 
        } + ')(' + JSON.stringify(streamId) + ')';

        chrome.tabs.executeScript(tab.id, {
            code: code
        }, function() {
            if (chrome.runtime.lastError) {
                alert('Failed to execute script: ' +
                    chrome.runtime.lastError.message);
            }   
        }); 
    }); 
});

マニフェスト.json

{
    "name": "desktopCapture.chooseDesktopMedia for a tab",
    "version": "1",
    "manifest_version": 2,
    "background": {
        "scripts": ["background.js"]
    },
    "browser_action": {
        "default_title": "Show desktop capture request"
    },
    "permissions": [
        "desktopCapture",
        "activeTab"
    ]
}
于 2014-07-15T20:58:22.743 に答える