The Youglish API lets you embed Youglish functionality in your website and control it using JavaScript.
Using the API's JavaScript functions, you can easily control every aspect of the widget: play, pause, or stop it, move to the previous or next track, adjust speed, and much more.
Samples illustrating the use of the API are available on the demo page.
onYouglishAPIReady
– The API will call this function when the page has finished downloading the JavaScript for the widget API, which enables you to then use the API on your page. Thus, this function might create the widget objects that you want to display when the page loads.
The HTML page below creates an embedded widget that searches for 'courage' tracks with 'US' accent, and plays each track 3 times before moving to the next one.
<!DOCTYPE html> <html> <body> <!-- 1. The widget will replace this <div> tag. --> <div id="widget-1"></div> <script> // 2. This code loads the widget API code asynchronously. var tag = document.createElement('script'); tag.src = "https://youglish.com/public/emb/widget.js"; var firstScriptTag = document.getElementsByTagName('script')[0]; firstScriptTag.parentNode.insertBefore(tag, firstScriptTag); // 3. This function creates a widget after the API code downloads. var widget; function onYouglishAPIReady(){ widget = new YG.Widget("widget-1", { width: 640, components:9, //search box & caption events: { 'onSearchDone': onSearchDone, 'onVideoChange': onVideoChange, 'onCaptionConsumed': onCaptionConsumed } }); // 4. process the query widget.search("courage","US"); } var views = 0, curTrack = 0, totalTracks = 0; // 5. The API will call this method when the search is done function onSearchDone(event){ if (event.totalResult === 0) alert("No result found"); else totalTracks = event.totalResult; } // 6. The API will call this method when switching to a new video. function onVideoChange(event){ curTrack = event.trackNumber; views = 0; } // 7. The API will call this method when a caption is consumed. function onCaptionConsumed(event){ if (++views < 3) widget.replay(); else if (curTrack < totalTracks) widget.next(); } </script> </body> </html>
After the API's JavaScript code loads, the API will call the onYouglishAPIReady
function, at which point you can construct a YG.Widget object to insert a widget in your page. The HTML excerpt below shows the onYouglishAPIReady function from the example above:
var widget; function onYouglishAPIReady(){ widget = new YG.Widget("widget-1", { width: 640, components:9, //search box & caption events: { 'onSearchDone': onSearchDone, 'onVideoChange': onVideoChange, 'onCaptionConsumed': onCaptionConsumed } }); // 4. process the query widget.search("courage","US"); }
The constructor for the widget specifies the following parameters:
width
(number) – The width of the video widget.
components
(number) – The components selected (see them all here).
events
(object) - The object's properties identify the events that the API fires and the functions that the API will call when those events occur.
Click here to see all the properties that can be used to customize the widget.
width
expand to all its container width.
height
expand to its actual content height.
autoStart
1
to enable - 0
to disable.
Default: 1
components
components
value of 1+4+8 = 13Check out demo #1 to see all the components in action.
Component | ID |
---|---|
Search box | 1 |
Accent panel | 2 |
Title | 4 |
Caption | 8 |
Speed controls | 16 |
Toggle UI | 32 |
Control Buttons | 64 |
Dictionary support | 128 |
Near by panel | 256 |
Phonetic panel | 512 |
Draggable | 1024 |
Minimizable | 2048 |
Closable | 4096 |
All Captions | 8192 |
Default: 255
backgroundColor
Default: white
linkColor
Default: #337ab7
titleColor
Default: #555555
captionColor
Default: #6495BF
markerColor
Default: yellow
queryColor
Default: orange
panelsBackgroundColor
Default: #F5F5F5
textColor
Default: #7F98AD
keywordColor
Default: orange
captionSize
40
restrictionMode
1
to enable - 0
to disable. Default: 0
videoQuality
default
small
medium
highres
Default: default
toggleUI
0
,
1
,
2
,
3
,
4
,
5
Default: 0
title
How to say %query%
in %lang%
(%i%
out of %total%
)
%query%
will be replace by the query.
%lang%
will be replace by the accent.
%i%
will be replace by the current index.
%total%
will be replace by the total result.
Default: the string above
widget.search(query:String, accent:String):Void
autoStart
is set to 0.Name | Type | Meaning | Values |
---|---|---|---|
query | String | Query to search for | Any string |
accent | String | Accent required |
English: all , us , uk , aus
French: allfrench , french , quebec , belgian
Default: |
widget.pause():Void
widget.play():Void
widget.replay():Void
widget.move(seconds:Number):Void
Name | Type | Meaning | Values |
---|---|---|---|
seconds | number | number of seconds to move the cursor. Negative number will cause the cursor to move backward. |
widget.next():Void
widget.previous():Void
widget.close():Void
widget.setAdsLocation(locations:Number):Void
Name | Type | Meaning | Values |
---|---|---|---|
locations | number | locations allowed | YG.AdLocations.RIGHT/LEFT/BOTTOM/TOP |
//display ads on LEFT and BOTTOM location: widget.setAdsLocation(YG.AdLocations.LEFT|YG.AdLocations.BOTTOM);
widget.addEventListener(event:String, listener:String):Void
Name | Type | Meaning | Values |
---|---|---|---|
event | String | The event to listen for. | Possible values: |
listener | String | The function name. |
widget.removeEventListener(event:String, listener:String):Void
The API fires events to notify your application of changes to the embedded widget. As noted in the previous section, you can subscribe to events by adding an event listener when constructing the YG.Widget object, and you can also use the addEventListener function.
The API will pass an event object as the sole argument to each of those functions. The event object properties are explained for each event below.
For an example of how to work with events, check out demo #1 which features an events viewer as part of the demo.
onSearchDone(event)
Name | Type | Meaning | Values |
---|---|---|---|
query | String | query processed | |
accent | String | accent used |
all , us , uk , aus
allfrench , french , quebec , belgian
|
totalResult | number | Total results found |
function onSearchDone(event){ var query = event.query; //query processed var accent = event.accent;//us, uk, etc.. var totalResult = event.totalResult; }
onVideoChange(event)
next
or previous
functions. Your application should implement this function
if you need information on the track being played, like the video ID or the track number in the search result.
Name | Type | Meaning | Values |
---|---|---|---|
video | String | Youtube video ID | |
trackNumber | Number | track position out of total tracks found. | Any number between 0 and the total results found. |
function onVideoChange(event){ var youTubeVideo = event.video; var resultNumber = event.index; }
onCaptionChange(event)
Name | Type | Meaning | Values |
---|---|---|---|
caption | String | The caption string. |
Search terms in the caption will appear between "[[[" and "]]]". Useful when you need to highlight the query. |
id | Number | the caption id |
When playing a video continuously, the id value will typically increment by one after each caption change. |
onCaptionConsumed(event)
Name | Type | Meaning | Values |
---|---|---|---|
id | Number | the caption id |
onPlayerReady(event)
onPlayerStateChange(event)
Name | Type | Meaning | Values |
---|---|---|---|
state | number | The new player state |
Possible values are:
|
onError(event)
Name | Type | Meaning | Values |
---|---|---|---|
code | number | An error code |
Possible values are:
|
widget.setAdsLocation(locations:Number):Void
Name | Type | Meaning | Values |
---|---|---|---|
locations | number | Ads locations allowed | YG.AdLocations.RIGHT/LEFT/BOTTOM/TOP |
//Allow ads on LEFT and BOTTOM location: widget.setAdsLocation(YG.AdLocations.LEFT|YG.AdLocations.BOTTOM);
onYouglishDisplayAd(id:String, location:Number, parent:HTMLElement, suggestedWidth:Number):Void
Name | Type | Meaning | Values |
---|---|---|---|
id | string | Widget id | |
location | number | Location of the ad unit. | YG.AdLocations.RIGHT/LEFT/BOTTOM/TOP |
parent | HTML Element | Reference to the Element object that will hold the ad unit. | |
suggestedWidth | number | Suggested ad unit width, useful for responsive ad unit |
function onYouglishAPIReady(){ ... //allow ads on the bottom and right locations. widget.setAdsLocation(YG.AdLocations.BOTTOM | YG.AdLocations.RIGHT); ... } //display Adsense ad on the bottom and a dummy HTML ad on the right. function onYouglishDisplayAd(id, location, parent, sugWidth){ if (location === YG.AdLocations.BOTTOM){ //responsive ad require to setup the width of the parent. Lets use the suggested width. parent.style.width = sugWidth + "px"; //call adsense parent.innerHTML = "<ins class='adsbygoogle' style='display:block' data-ad-client='ca-pub-11111' data-ad-slot='22222' data-ad-format='auto'></ins>"; (adsbygoogle = window.adsbygoogle || []).push({}); } else if (location === YG.AdLocations.RIGHT){ //just a simple div block. Ignore the sugWidth since the div has a 200px width. parent.innerHTML = "<div style='color:white;background-color:#6e94ce;display: table;min-height:80px;height:100%;width: 200px;'><div style='display: table-cell;text-align: center;vertical-align: middle;'>Partner Right Ad</div></div>"; } }
YG.setParnterKey(key:String)
Name | Type | Meaning |
---|---|---|
key | String | The key is provided by Youglish to the Partner in order to uniquely identify each Partner. |
YG.setAdWidthRatio(ratio:Number)
Name | Type | Meaning | Values |
---|---|---|---|
ratio | number | Width ratio, used to calculate the width of responsive left or right ads. | Any number between 10 and 50. Default:35% |
YG.getwidget(id:String):Widget
Name | Type | Meaning |
---|---|---|
id | String | element ID |
<html> <body> <!-- Widget added by copy/paste a widget code (via Widget Builder) --> <a id="yg-widget-0" class="youglish-widget" data-accent="all" data-components="243" data-bkg-color="FFFFFF" data-link-color="337AB7" data-ttl-color="555555" data-cap-color="6495BF" data-marker-color="FFFF00" data-cap-size="40" data-toggle-ui="0" data-scroll="inner" href="https://youglish.com">Visit YouGlish.com</a> <script async src="https://youglish.com/public/emb/widget.js" charset="utf-8"></script> <!-- Interact with the widget --> <script> var widget; <!-- Wait for YG library to be loaded --> function onYouglishAPIReady() { <!-- retrieve the widget object--> widget = YG.getWidget("yg-widget-0"); <!-- use the API...--> widget.search("welcome"); } </script> </body> </html>
YG.setParnterKey(key:String)
Name | Type | Meaning |
---|---|---|
key | String | The key is provided by Youglish to the Partner in order to uniquely identify each Partner. |
YG.setAdWidthRatio(ratio:Number)
Name | Type | Meaning | Values |
---|---|---|---|
ratio | number | Width ratio, used to calculate the width of responsive left or right ads. | Any number between 10 and 50. Default:35% |
Restricted Mode
in the customization options.