JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 159927
  • Score
    100M100P100Q165780F
  • License MIT

Helps you create a custom element w/ a HTMLMediaElement API.

Package Exports

  • super-media-element
  • super-media-element/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (super-media-element) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Super Media Element

A custom element that helps save alienated player API's to bring back their true inner HTMLMediaElement API, or to extend a native media element like <audio> or <video>.

Usage

import { SuperVideoElement } from 'super-media-element';

class MyVideoElement extends SuperVideoElement {
  constructor() {
    super();

    this.loadComplete = new Promise((resolve) => {
      this.loadResolve = resolve;
    });
  }

  async attributeChangedCallback(attrName, oldValue, newValue) {
    // This is required to come before the await for resolving loadComplete.
    if (attrName === 'src' && newValue) {
      this.load();
      return;
    }

    super.attributeChangedCallback(attrName, oldValue, newValue);
  }

  async load() {
    if (this.hasLoaded) {
      this.loadComplete = new Promise((resolve) => {
        this.loadResolve = resolve;
      });
    }
    this.hasLoaded = true;

    // Wait 1 tick to allow other attributes to be set.
    await Promise.resolve();

    // code to load a video element from a script
    // example: https://github.com/luwes/jwplayer-video-element/blob/main/src/jwplayer-video-element.js#L49-L69

    this.loadResolve();
  }

  get nativeEl() {
    return this.querySelector('.loaded-video-element');
  }

  get src() {
    return this.getAttribute('src');
  }

  set src(val) {
    if (this.src == val) return;
    this.setAttribute('src', val);
  }
}

if (!globalThis.customElements.get('my-video')) {
  globalThis.customElements.define('my-video', MyVideoElement);
  globalThis.MyVideoElement = MyVideoElement;
}

export { MyVideoElement };