6

I'm trying to set up in Angular a way of only showing Twitch player if the stream is online, so in the Documentation there is this inline HTML:

<script src= "https://player.twitch.tv/js/embed/v1.js"></script>
<div id="<player div ID>"></div>
<script type="text/javascript">
  var options = {
    width: <width>,
    height: <height>,
    channel: "<channel ID>",
    video: "<video ID>",
    collection: "<collection ID>",
  };
  var player = new Twitch.Player("<player div ID>", options);
  player.setVolume(0.5);
</script>

Of course though in Angular Components we can't use < script > tags and I don't think it's currently possible to use require or import to use this in my components TypeScript.

So how would I go about adding this functionality? I understand what to do after I can actually reference that script tag.

Daniel Turcich
  • 1,764
  • 2
  • 26
  • 48

4 Answers4

5

You can create a script tag dynamically, but I think it's easier to just download the script and put it in assets folder and then add the script to script list in angular.json

or you can add the script to index.html head section

index.html

<head>
    <script src= "https://player.twitch.tv/js/embed/v1.js"></script>
    ...
</head>

add this to component ngOninit method

      ngOnInit() {
        var options = {
            width: <width>,
            height: <height>,
            channel: "<channel ID>",
            video: "<video ID>",
            collection: "<collection ID>",
          };
          var player = new Twitch.Player("<player div ID>", options);
          player.setVolume(0.5);
       }

Component template

    <div id="<player div ID>"></div>

You may find another solution like angular package for twitch can be more cleaner way

Updated

typescript will give an error like [ts] Cannot find name 'Twitch'. [2304] some library has type definition files but in our case if you use Twitch object in this file only you can add this statement to tell typescript about the Twitch object

declare const Twitch: any;

if you want to use Twitch on multiple components

src/global.d.ts

 declare const Twitch: any;

final recommendation

install npm i twitch-js and import Twitch object like this

import Twitch from 'twitch-js'

this will be bundled by webpack, so you don't need to add any script tag in the html.

twitch-devs

jnovack
  • 7,629
  • 2
  • 26
  • 40
Muhammed Albarmavi
  • 23,240
  • 8
  • 66
  • 91
1

Struggled with this for a while. My script was asynchronous, and eventually I discovered postscribe package. Works like magic.

npm install --save postscribe

In the code:

import postscribe from 'postscribe'

//The script you want to load
const script = '<script src="https://darksky.net/map-embed/@radar,41.88,-87.62,10.js?embed=true&timeControl=false&fieldControl=false&defaultField=radar"></script>';

postscribe('#yourDivName', script);
NearHuscarl
  • 66,950
  • 18
  • 261
  • 230
Yalzeee
  • 187
  • 2
  • 13
1

This is how I made it work:

  1. Copy everything from Twitch JS URL.
  2. Create file twitch.js in your assets folder and paste everything. ( src/assets/scripts/twitch.js ).
  3. In your component add import Twitch from 'src/assets/scripts/twitch.js'.
  4. Also in your component add the twitch init code

Your component should look like this:

import { Component, OnInit } from '@angular/core';
import Twitch from 'src/assets/scripts/twitch.js';

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.scss']
})
export class HomeComponent implements OnInit {

  embedTwitch! : any;
  
  constructor() { }

  ngOnInit() {
    this.embedTwitch = new Twitch.Embed("twitch-embed", {
      width: 854,
      height: 480,
      channel: "monstercat"
    });
  }

}
  1. Your component html must have twitch placeholder <div id="twitch-embed"></div>
  2. Open your app and enjoy :)
Karmidzhanov
  • 229
  • 3
  • 14
0
  1. Manually download the v1.js script from https://embed.twitch.tv/embed/v1.js
  2. Create new folder src/twitch/ and add the v1.js script to it
  3. Add "src/twitch/v1.js" to the scripts array in angular.json
  4. Create a new file twitch.d.ts in src/twitch/
  5. Add declare const Twitch: any; to twitch.d.ts

The Twitch library can now be used anywhere without importing any module like so:

ngOnInit(): void {
  new Twitch.Embed('containerId', {
    channel: 'asmongold',
    height: 500,
    width: 400,
 });
}
yooloobooy
  • 493
  • 6
  • 12