I can't play youtube chromeless player on chrome extension

ⅰ亾dé卋堺 提交于 2019-12-06 12:19:04

问题


I'm newbie at chrome extension. I'm making chrome extension that play youtube chromeless player.

It worked on chrome web browser. But, it isn't working on chrome extension.

I tested local .swf file. That is worked on chrome extension.

I think, chrome extension can't call onYouTubePlayerReady().

So I called window.onYouTubePlayerReady() after swfobject.embedSWF(). But, it isn't worked at ytplayer.loadVideoById("xa8TBfPw3u0", 0); with error message.

The error message was Uncaught TypeError: Object #<HTMLObjectElement> has no method 'loadVideoById'.

Is there a problem in manifest.json? Or in YouTube API? I don't know why isn't working on chrome extension.

popup.html is

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>YouTube Play</title>
    </head>
    <body>
    <table width="1000" height="390">
        <tr>
            <td>
            <div id="videoDiv">
                Loading...
            </div></td>
            <td valign="top">
            <div id="videoInfo">
                <p>
                    Player state: <span id="playerState">--</span>
                </p>
                <p>
                    Current Time: <span id="videoCurrentTime">--:--</span> | Duration: <span id="videoDuration">--:--</span>
                </p>
                <p>
                    Bytes Total: <span id="bytesTotal">--</span> | Start Bytes: <span id="startBytes">--</span> | Bytes Loaded: <span id="bytesLoaded">--</span>
                </p>
                <p>
                    Controls: <input type="button" id="play" value="Play" />
                    <input type="button" id="pause" value="Pause" />
                    <input type="button" id="mute" value="Mute" />
                    <input type="button" id="unmute" value="Unmute" />
                </p>
                <p>
                    <input id="volumeSetting" type="text" size="3" />
                    &nbsp;<input type="button" id="setVolume" value="Set Volume" /> | Volume: <span id="volume">--</span>
                </p>
            </div></td>
        </tr>
    </table>

    <script type="text/javascript" src="jsapi.js"></script>
    <script type="text/javascript" src="my_script.js"></script>
    <script type="text/javascript" src="swfobject.js"></script>
    </body>
</html>

manifest.json is

{
    "manifest_version": 2,
    "name": "YouTube Player",
    "description": "This extension is YouTube Player",
    "version": "1.0",
    "browser_action": {
        "default_icon": "icon.png",
        "default_popup": "popup.html"
    },
    "permissions": ["tabs", "http://*/*", "https://*/*", "background"],
    "content_scripts": [
        {
            "matches": ["http://www.youtube.com/*"],
            "js": ["my_script.js", "swfobject.js", "jsapi.js"]
        }
    ]
}

my_script.js is

/*
* Chromeless player has no controls.
*/

// Update a particular HTML element with a new value
function updateHTML(elmId, value) {
    document.getElementById(elmId).innerHTML = value;
}

// This function is called when an error is thrown by the player
function onPlayerError(errorCode) {
    alert("An error occured of type:" + errorCode);
}

// This function is called when the player changes state
function onPlayerStateChange(newState) {
    updateHTML("playerState", newState);
}

// Display information about the current state of the player
function updatePlayerInfo() {
    // Also check that at least one function exists since when IE unloads the
    // page, it will destroy the SWF before clearing the interval.
    if (ytplayer && ytplayer.getDuration) {
        updateHTML("videoDuration", ytplayer.getDuration());
        updateHTML("videoCurrentTime", ytplayer.getCurrentTime());
        updateHTML("bytesTotal", ytplayer.getVideoBytesTotal());
        updateHTML("startBytes", ytplayer.getVideoStartBytes());
        updateHTML("bytesLoaded", ytplayer.getVideoBytesLoaded());
        updateHTML("volume", ytplayer.getVolume());
    }
}

// Allow the user to set the volume from 0-100
function setVideoVolume() {
    var volume = parseInt(document.getElementById("volumeSetting").value);
    if (isNaN(volume) || volume < 0 || volume > 100) {
        alert("Please enter a valid volume between 0 and 100.");
    } else if (ytplayer) {
        ytplayer.setVolume(volume);
    }
}

function playVideo() {
    if (ytplayer) {
        ytplayer.playVideo();
    }
}

function pauseVideo() {
    if (ytplayer) {
        ytplayer.pauseVideo();
    }
}

function muteVideo() {
    if (ytplayer) {
        ytplayer.mute();
    }
}

function unMuteVideo() {
    if (ytplayer) {
        ytplayer.unMute();
    }
}

// This function is automatically called by the player once it loads
function onYouTubePlayerReady(playerId) {
    ytplayer = document.getElementById("ytPlayer");
    // This causes the updatePlayerInfo function to be called every 250ms to
    // get fresh data from the player
    setInterval(updatePlayerInfo, 250);
    updatePlayerInfo();
    ytplayer.addEventListener("onStateChange", "onPlayerStateChange");
    ytplayer.addEventListener("onError", "onPlayerError");
    //Load an initial video into the player
    ytplayer.loadVideoById("xa8TBfPw3u0", 0);
}

// The "main method" of this sample. Called when someone clicks "Run".
function loadPlayer() {
    // Lets Flash from another domain call JavaScript
    var params = {
        allowScriptAccess : "always"
    };
    // The element id of the Flash embed
    var atts = {
        id : "ytPlayer"
    };
    // All of the magic handled by SWFObject (http://code.google.com/p/swfobject/)
    swfobject.embedSWF("http://www.youtube.com/apiplayer?" + "&enablejsapi=1&playerapiid=ytplayer", "videoDiv", "640", "390", "8", null, null, params, atts);

    //window.onYouTubePlayerReady();

    document.getElementById("play").onclick = playVideo;
    document.getElementById("pause").onclick = pauseVideo;
    document.getElementById("mute").onclick = muteVideo;
    document.getElementById("unmute").onclick = unMuteVideo;
    document.getElementById("setVolume").onclick = setVideoVolume;
}

function _run() {
    loadPlayer();
}

google.setOnLoadCallback(_run);

Please help me.


回答1:


Recently I've encountered the same issue when working on a Chrome extension. When testing, the calls are simply not triggered. Until I've read this:

To test any of these calls, you must have your file running on a webserver, as the Flash player restricts calls between local files and the internet.

From YouTube's Player API Documentation



来源:https://stackoverflow.com/questions/19475074/i-cant-play-youtube-chromeless-player-on-chrome-extension

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!