Youtube Iframe API
Easily create and interact with embedded YouTube videos
|
view on
github
|
This library is a reworking of YouTube's default embedding iframe API. It contains a few extra features and has readable and modifiable source code. It can be directly integrated into an existing script or loaded as a stand-alone script.
It can also be used to learn how the basic postMessage
system communicates between the main window and the embedded iframe.
onYouTubeIframeAPIReady
event requiredonYouTubeIframeAPIReady
to exist somewhere in a <script>
tag on the page.
This makes it very difficult (or impossible) to use their API inside of a userscript, because the event (and subsequently the YT
object) will exist on the main window
object and not inside the userscript.
listening
messagepostMessage
's a listening
event to the iframe every 250ms.
Testing seems to indicate that this message is needed only once, so this custom script only sends it once.
There could be a reason for this on some untested edge cases or mobile platforms, but none have been found yet.
iframe_api
script effectively loads 2 scripts instead of one.
The iframe_api
is just a loader script and inserts another <script>
node for the actual API.
The second script is automatically async
as well with no options to control this.
This documentation is fairly minimal in an effort to not over-rewrite or copy YouTube's original documentation. For most of the functions listed on this page, their parallels can be found in the original documentation, which may contain some extra info about arguments and such.
The extended descriptions are minimized by default for ease of locating functions by name. Click the +/− or the function name to expand/shrink the description. (expand all | shrink all)
new Youtube.(settings...) : instance
settings : object
An object describing the settings to create the player with. The format of the object should be as follows:
return : instance
Player
class
Note that for the following three functions, only the first argument is required. The remaining arguments are optional, and a default is used if they are set to null
or undefined
.
Player.(video_id, autoplay, quality, start_time, end_time...)
video_id : string
autoplay : boolean
true
, the video will (under most circumstances) autoplayquality : string
start_time : number
end_time : number
Player.(video_url, autoplay, quality, start_time, end_time...)
video_url : string
autoplay : boolean
true
, the video will (under most circumstances) autoplayquality : string
start_time : number
end_time : number
Player.(playlist, type, index, autoplay, quality, start_time...)
playlist : string | Array
playlist_type
:"playlist"
"search"
"user_uploads"
type : string
"playlist"
, "search"
, or "user_uploads"
. If unspecified, the default is "playlist"
. If playlist
is an array, this argument is ignored.index : number
autoplay : boolean
true
, the playlist will (under most circumstances) autoplayquality : string
start_time : number
Player.(timecode, allow_seek_ahead...)
timecode : number
allow_seek_ahead : boolean
true
, the video is allowed to skip out of the presently buffered region.false
and called again with true
when seeking is done (i.e. on a mouseup event.)
Player.() : integer
return : integer
Player.() : null | Array
return : null | Array
null
is returned. Otherwise, it's an array of video ids. If set_shuffle
was called, this return value reflects the shuffled playlist.The extended API covers other settings YouTube provides control over that may or may not be available on every video.
Note that YouTube's documentation on their similar getOption
, getOptions
, and setOption
functions is dated. Explore the get_api
return value to discover which options are available.
Unless otherwise specified, the event data of all events are called with an event_data
argument of the form:
Player.(event_name, callback...) : boolean
event_name : string
callback : function
A function to be called when the event is triggered. The this
object of the call will be the player instance, and the first argument contains any event-specific data.
callback.call(player_instance, event_data)
return : boolean
true
if the event exists and the callback was added, false
otherwisePlayer.(event_name, callback...) : boolean
event_name : string
callback : function
return : boolean
true
if the callback was successfully removed, false
otherwiseThis event will be called as soon as it can be. This means, if the iframe triggers it's ready event, all callbacks will trigger at this point. If a callback is added after the iframe event triggers, it will fire inside the on
call.
The event_data
is of the form:{ immediate: /* true or false */ }
The immediate
entry will be true
if it was called during the on
call, and false
otherwise.
Actions | Playback | Loading | Playlist |
State | Extended API | Event Log | |