@anivire/twitch-embed-ts
Version:
Twitch Embed API Typescript wrapper
461 lines (453 loc) • 12.1 kB
JavaScript
// src/models/embed.model.ts
var TwitchEmbedLayout = /* @__PURE__ */ ((TwitchEmbedLayout2) => {
TwitchEmbedLayout2["VIDEO_WITH_CHAT"] = "video-with-chat";
TwitchEmbedLayout2["VIDEO"] = "video";
return TwitchEmbedLayout2;
})(TwitchEmbedLayout || {});
var TwitchEmbedTheme = /* @__PURE__ */ ((TwitchEmbedTheme2) => {
TwitchEmbedTheme2["LIGHT"] = "light";
TwitchEmbedTheme2["DARK"] = "dark";
return TwitchEmbedTheme2;
})(TwitchEmbedTheme || {});
var TwitchEmbedEvents = /* @__PURE__ */ ((TwitchEmbedEvents2) => {
TwitchEmbedEvents2["PLAY"] = "play";
TwitchEmbedEvents2["READY"] = "ready";
return TwitchEmbedEvents2;
})(TwitchEmbedEvents || {});
// src/models/player.model.ts
var TwitchPlayerEvents = /* @__PURE__ */ ((TwitchPlayerEvents2) => {
TwitchPlayerEvents2["CAPTIONS"] = "captions";
TwitchPlayerEvents2["ENDED"] = "ended";
TwitchPlayerEvents2["PAUSE"] = "pause";
TwitchPlayerEvents2["PLAY"] = "play";
TwitchPlayerEvents2["PLAYBACK_BLOCKED"] = "playbackBlocked";
TwitchPlayerEvents2["PLAYING"] = "playing";
TwitchPlayerEvents2["OFFLINE"] = "offline";
TwitchPlayerEvents2["ONLINE"] = "online";
TwitchPlayerEvents2["READY"] = "ready";
TwitchPlayerEvents2["SEEK"] = "seek";
return TwitchPlayerEvents2;
})(TwitchPlayerEvents || {});
// src/embeds/player.ts
var TwitchPlayer = class _TwitchPlayer {
player;
/**
* Creates a new Twitch player from player instance
* @param player player instance
*/
constructor(player) {
this.player = player;
}
/**
* Creates a new Twitch player given a <div> element and some options for the player
* @param divId the div HTML element where the player will appear
* @param options the player initialization options
* @constructor creates a new Twitch player instance
*/
static CreatePlayer(divId, options) {
try {
if (window.Twitch && window.Twitch.Player) {
const player = new window.Twitch.Player(divId, options);
return new _TwitchPlayer(player);
} else {
throw new Error("Twitch embed library is not provided or loaded, please provide library to page.");
}
} catch (e) {
console.error("Error initializing Twitch player:", e instanceof Error ? e.message : e);
throw e;
}
}
/**
* Add event listener to twitch player
* @param event player event
* @param callback function
*/
addEventListener(event, callback) {
this.player.addEventListener(event, callback);
}
/**
* Remove event listener from twitch player
* @param event player event
* @param callback function
*/
removeEventListener(event, callback) {
this.player.removeEventListener(event, callback);
}
/**
* Disables display of Closed Captions
*/
disableCaptions() {
this.player.disableCaptions();
}
/**
* Enables display of Closed Captions
*/
enableCaptions() {
this.player.enableCaptions();
}
/**
* Pauses the player
*/
pause() {
this.player.pause();
}
/**
* Begins playing the specified video
*/
play() {
this.player.play();
}
/**
* Seeks to the specified `timestamp` (in seconds) in the video
* @param timestamp time
*/
seek(timestamp) {
this.player.seek(timestamp);
}
/**
* Sets the channel to be played
* @param channel channel name
*/
setChannel(channel) {
this.player.setChannel(channel);
}
/**
* Sets the collection to be played.
Optionally also specifies the video within the collection, from which to start playback.
If a `videoId` is not provided here or the specified video is not part of the collection,
playback starts with the first video in the collection
* @param collectionId collection id
* @param videoId video id
*/
setCollection(collectionId, videoId) {
this.player.setCollection(collectionId, videoId);
}
/**
* Sets the quality of the video.
`quality` should be a string value returned by `getQualities()`
* @param quality
*/
setQuality(quality) {
this.player.setQuality(quality);
}
/**
* Sets the video to be played to be played and starts playback at `timestamp` (in seconds)
* @param videoId
* @param timestamp
*/
setVideo(videoId, timestamp) {
this.player.setVideo(videoId, timestamp);
}
/**
* Returns true if the player is muted; otherwise, false
* @returns mute state
*/
getMuted() {
return this.player.getMuted();
}
/**
* If true, mutes the player; otherwise, unmutes it. This is independent of the volume setting
* @param muted mute state
*/
setMuted(muted) {
this.player.setMuted(muted);
}
/**
* Returns the volume level
* @returns value between 0.0 and 1.0
*/
getVolume() {
return this.player.getVolume();
}
/**
* Sets the volume to the specified volume level
* @param volumeLevel value between 0.0 and 1.0
*/
setVolume(volumeLevel) {
this.player.setVolume(volumeLevel);
}
/**
* Returns an object with statistics on the playerded video player and the current live stream or VOD
* @returns object with statistics
*/
getPlaybackStats() {
return this.player.getPlaybackStats();
}
/**
* Returns the channel’s name. Works only for live streams, not VODs
* @returns channel name
*/
getChannel() {
return this.player.getChannel();
}
/**
* Returns the current video’s timestamp, in seconds. Works only for VODs, not live streams
* @returns video timestamp
*/
getCurrentTime() {
return this.player.getCurrentTime();
}
/**
* Returns the duration of the video, in seconds. Works only for VODs,not live streams
* @returns duration of the video
*/
getDuration() {
return this.player.getDuration();
}
/**
* Returns true if the live stream or VOD has ended; otherwise, false
* @returns stream status
*/
getEnded() {
return this.player.getEnded();
}
/**
* Returns the available video qualities
* @returns video qualities
*/
getQualities() {
return this.player.getQualities();
}
/**
* Returns the current quality of video playback
* @returns state of playback
*/
getQuality() {
return this.player.getQuality();
}
/**
* Returns the video ID. Works only for VODs, not live streams
* @returns video id
*/
getVideo() {
return this.player.getVideo();
}
/**
* Returns true if the video is paused; otherwise, false. Buffering or seeking is considered playing.
* @returns pause state
*/
isPaused() {
return this.player.isPaused();
}
};
// src/embeds/embed.ts
var TwitchEmbed = class {
embed;
player;
embedOptions;
divId;
/**
* Creates a new Twitch embed instance
* @param divId the div HTML element where the player will appear
* @param options the player initialization options
*/
constructor(divId, options) {
this.embedOptions = options;
this.divId = divId;
try {
if (window.Twitch && window.Twitch.Embed) {
this.embed = new window.Twitch.Embed(this.divId, this.embedOptions);
this.player = new TwitchPlayer(this.embed.getPlayer());
} else {
throw new Error("Twitch embed library is not provided or loaded, please provide library to page.");
}
} catch (e) {
console.error("Error initializing Twitch embed:", e instanceof Error ? e.message : e);
throw e;
}
}
/**
* Add event listener to twitch embed
* @param event embed event
* @param callback function
*/
addEventListener(event, callback) {
this.embed.addEventListener(event, callback);
}
/**
* Remove event listener from twitch embed
* @param event embed event
* @param callback function
*/
removeEventListener(event, callback) {
this.embed.removeEventListener(event, callback);
}
/**
* Get div id for ijecting
* @returns div id
*/
getDivId() {
return this.divId;
}
/**
* Get twitch embed player
* @returns twitch player object
*/
getPlayer() {
return this.player;
}
/**
* Disables display of Closed Captions
*/
disableCaptions() {
this.embed.disableCaptions();
}
/**
* Enables display of Closed Captions
*/
enableCaptions() {
this.embed.enableCaptions();
}
/**
* Pauses the player
*/
pause() {
this.embed.pause();
}
/**
* Begins playing the specified video
*/
play() {
this.embed.play();
}
/**
* Seeks to the specified `timestamp` (in seconds) in the video
* @param timestamp time
*/
seek(timestamp) {
this.embed.seek(timestamp);
}
/**
* Sets the channel to be played
* @param channel channel name
*/
setChannel(channel) {
this.embed.setChannel(channel);
}
/**
* Sets the collection to be played.
Optionally also specifies the video within the collection, from which to start playback.
If a `videoId` is not provided here or the specified video is not part of the collection,
playback starts with the first video in the collection
* @param collectionId collection id
* @param videoId video id
*/
setCollection(collectionId, videoId) {
this.embed.setCollection(collectionId, videoId);
}
/**
* Sets the quality of the video.
`quality` should be a string value returned by `getQualities()`
* @param quality
*/
setQuality(quality) {
this.embed.setQuality(quality);
}
/**
* Sets the video to be played to be played and starts playback at `timestamp` (in seconds)
* @param videoId
* @param timestamp
*/
setVideo(videoId, timestamp) {
this.embed.setVideo(videoId, timestamp);
}
/**
* Returns true if the player is muted; otherwise, false
* @returns mute state
*/
getMuted() {
return this.embed.getMuted();
}
/**
* If true, mutes the player; otherwise, unmutes it. This is independent of the volume setting
* @param muted mute state
*/
setMuted(muted) {
this.embed.setMuted(muted);
}
/**
* Returns the volume level
* @returns value between 0.0 and 1.0
*/
getVolume() {
return this.embed.getVolume();
}
/**
* Sets the volume to the specified volume level
* @param volumeLevel value between 0.0 and 1.0
*/
setVolume(volumeLevel) {
this.embed.setVolume(volumeLevel);
}
/**
* Returns an object with statistics on the embedded video player and the current live stream or VOD
* @returns object with statistics
*/
getPlaybackStats() {
return this.embed.getPlaybackStats();
}
/**
* Returns the channel’s name. Works only for live streams, not VODs
* @returns channel name
*/
getChannel() {
return this.embed.getChannel();
}
/**
* Returns the current video’s timestamp, in seconds. Works only for VODs, not live streams
* @returns video timestamp
*/
getCurrentTime() {
return this.embed.getCurrentTime();
}
/**
* Returns the duration of the video, in seconds. Works only for VODs,not live streams
* @returns duration of the video
*/
getDuration() {
return this.embed.getDuration();
}
/**
* Returns true if the live stream or VOD has ended; otherwise, false
* @returns stream status
*/
getEnded() {
return this.embed.getEnded();
}
/**
* Returns the available video qualities
* @returns video qualities
*/
getQualities() {
return this.embed.getQualities();
}
/**
* Returns the current quality of video playback
* @returns state of playback
*/
getQuality() {
return this.embed.getQuality();
}
/**
* Returns the video ID. Works only for VODs, not live streams
* @returns video id
*/
getVideo() {
return this.embed.getVideo();
}
/**
* Returns true if the video is paused; otherwise, false. Buffering or seeking is considered playing.
* @returns pause state
*/
isPaused() {
return this.embed.isPaused();
}
};
export {
TwitchEmbed,
TwitchEmbedEvents,
TwitchEmbedLayout,
TwitchEmbedTheme,
TwitchPlayer,
TwitchPlayerEvents
};