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 English tracks containing 'courage', 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: { 'onFetchDone': onFetchDone, 'onVideoChange': onVideoChange, 'onCaptionConsumed': onCaptionConsumed } }); // 4. process the query widget.fetch("courage","english"); } var views = 0, curTrack = 0, totalTracks = 0; // 5. The API will call this method when the search is done function onFetchDone(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: { 'onFetchDone': onFetchDone, 'onVideoChange': onVideoChange, 'onCaptionConsumed': onCaptionConsumed } }); // 4. process the query widget.fetch("courage","english"); }
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.
widthexpand to all its container width.heightexpand to its actual content height.autoStart1 to enable - 0 to disable.
Default: 1
componentscomponents value of 1+4+16 = 21| Component | ID |
|---|---|
| Search box | 1 |
| Accent panel | 2 |
| Title | 4 |
| Caption | 8 |
| Speed controls | 16 |
| Toggle UI (deprecated) | 32 |
| Control Buttons | 64 |
| Dictionary support | 128 |
| Near by panel | 256 |
| Phonetic panel | 512 |
| Draggable | 1024 |
| Minimizable | 2048 |
| Closable | 4096 |
| All Captions | 8192 |
| Toggle light | 16384 |
| Toggle tumbnails | 32768 |
Default: 255
backgroundColorDefault: white
linkColorDefault: #337ab7
titleColorDefault: #555555
captionColorDefault: #6495BF
markerColorDefault: yellow
queryColorDefault: orange
panelsBackgroundColorDefault: #F5F5F5
textColorDefault: #7F98AD
keywordColorDefault: orange
captionSize40restrictionMode1 to enable - 0 to disable. Default: 0
videoQualitydefault
small
medium
highres
Default: default
titleHow 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.fetch(query:String, lang:String, accent:String):Void
autoStart is set to 0.| Name | Type | Required | Meaning | Values |
|---|---|---|---|---|
| query | String | Yes | Query to search for | Any string |
| lang | String | Yes | Audio language | Arabic, Chinese, Dutch, English, French, German, Greek, Hebrew, Hindi, Italian, Japanese, Korean, Persian, Polish, Portuguese, Romanian, Russian, Spanish, Swedish, Thai, Turkish, Ukrainian, Vietnamese, Sign Languages |
| accent | String | No | Audio accent |
Default: |
widget.pause():Void
widget.play():Void
widget.replay():Void
widget.getSpeed():Number
widget.setSpeed(suggestedRate:Number):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.
onFetchDone(event)
function onFetchDone(event){ var query = event.query; var lang = event.lang; var accent = event.accent; 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 tracks found. |
function onVideoChange(event){ var youTubeVideo = event.video; var resultNumber = event.trackNumber; }
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:
|
onSpeedChange(event)
| Name | Type | Meaning | Values |
|---|---|---|---|
| speed | Number | the new speed rate |
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-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.