Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

abandon yt js api #192

Open
ThaUnknown opened this issue Dec 4, 2024 · 5 comments
Open

abandon yt js api #192

ThaUnknown opened this issue Dec 4, 2024 · 5 comments

Comments

@ThaUnknown
Copy link

ThaUnknown commented Dec 4, 2024

youtube's JS API for the most part isn't needed, because I think the autoplay bug can be fixed by appending &playlist={id}, however I don't have a Mac to verify this, instead the JS API can be "spoofed":

  let frame: HTMLIFrameElement
  function ytCall (action: string, arg: string | null = null) {
    frame.contentWindow?.postMessage('{"event":"command", "func":"' + action + '", "args":' + arg + '}', '*')
  }
  let timeout: ReturnType<typeof setInterval>
  frame.onload = () {
    timeout = setInterval(() => {
      frame.contentWindow?.postMessage('{"event":"listening","id":1,"channel":"widget"}', '*')
    }, 100)
    frame.contentWindow?.postMessage('{"event":"listening","id":1,"channel":"widget"}', '*')
  }
  function ytMessage (e: MessageEvent) {
    if (e.origin !== 'https://www.youtube-nocookie.com') return
    clearInterval(timeout)
    const json = JSON.parse(e.data as string) as { event: string, info: {videoData: {isPlayable: boolean}, playerState?: number} }
    if (json.event === 'onReady') ytCall('playVideo')

    if (json.event === 'initialDelivery' && !json.info.videoData.isPlayable) {
      throw new Error('video not playable')
    }

    if (json.event === 'infoDelivery' && json.info.playerState === 1) {
      // video playable
    }
  }
  window.onmessage = ytMessage

this saves loading their 200kb or so API

@paulirish
Copy link
Owner

&playlist={id} sounds quite clever, for sure.

Will have to try that out.

Really appreciate you filing!

@ThaUnknown
Copy link
Author

the id for playlist is ofc the video id

@Garbee
Copy link
Contributor

Garbee commented Dec 5, 2024

I think the question before deciding to abandon (past ensuring this works as expected for the use cases) is, do we not want to expose the API? It is quite powerful for developers to control the player. Is there a way for them to attach the JS API on top of the embed if we remove it?

Currently we don't expose it against the video, sure. But it has come up that some do want that made public.

If we want to try and "trim everything down", then maybe loading the YT API and exposing the instance against the video is something we could let developers opt-in to.

@ThaUnknown
Copy link
Author

I think the main goal of this lib is to make as light of an yt embed as possible, the js api isn't always included anyways, only on some specific browsers, so it's not like it's a compatibility change

@ThaUnknown
Copy link
Author

and again, all this stuff can be exposed via this api spoof I showed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants