mstf-kit
Version:
一个现代化的 JavaScript/TypeScript 工具库,提供了丰富的常用工具函数
2 lines (1 loc) • 8.17 kB
JavaScript
;Object.create,Object.defineProperty,Object.getOwnPropertyDescriptor,Object.getOwnPropertyNames,Object.getPrototypeOf,Object.prototype.hasOwnProperty,Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e={controls:["play-large","play","progress","current-time","mute","volume","captions","settings","pip","airplay","fullscreen"],hideControls:!0,resetOnEnd:!1,fullscreen:{enabled:!0,fallback:!0,iosNative:!0},invertTime:!1,displayDuration:!0,storage:{enabled:!0,key:"plyr-volume"},speed:{selected:1,options:[.5,.75,1,1.25,1.5,1.75,2]},markers:{enabled:!1,points:[]},loadSprite:!0,debug:!1,i18n:{restart:"重播",play:"播放",pause:"暂停",volume:"音量",mute:"静音",pip:"画中画",normal:"默认",quality:"画质",download:"下载",enterFullscreen:"全屏",exitFullscreen:"关闭全屏",captions:"字幕",settings:"设置",speed:"倍速",loop:"循环播放"}};async function t(e,t,r=!0){var o;if(!1!==r&&await(o=t,new Promise(((e,t)=>{if("undefined"==typeof document)return void t(new Error("Document is not available"));if(document.querySelector(`link[href="${o}"]`))return void e();const r=document.createElement("link");r.rel="stylesheet",r.href=o,r.onload=()=>e(),r.onerror=()=>t(new Error(`Failed to load CSS: ${o}`)),document.head.appendChild(r)}))),await function(e){return new Promise(((t,r)=>{if("undefined"==typeof document)return void r(new Error("Document is not available"));const o=document.createElement("script");o.src=e,o.async=!0,o.onload=()=>t(),o.onerror=()=>r(new Error(`Failed to load script: ${e}`)),document.head.appendChild(o)}))}(e),"undefined"!=typeof window&&window.Plyr)return window.Plyr;throw new Error("Failed to load Plyr from CDN")}async function r(r){const o=await async function(e){var r,o,n,i;const a=(null==(r=null==e?void 0:e.plyrLibOptions)?void 0:r.source)||"npm",l=(null==(o=null==e?void 0:e.plyrLibOptions)?void 0:o.cdnUrl)||"https://unpkg.com/plyr@3.7.8/dist/plyr.min.js",s=(null==(n=null==e?void 0:e.plyrLibOptions)?void 0:n.cssUrl)||"https://unpkg.com/plyr@3.7.8/dist/plyr.css";if("external"===a&&(null==(i=null==e?void 0:e.plyrLibOptions)?void 0:i.externalLib))return e.plyrLibOptions.externalLib;if("undefined"!=typeof window&&window.Plyr)return window.Plyr;if("npm"===a)try{return null==e||e.autoCreateCss,await import("plyr").then((e=>e.default||e))}catch(c){return console.error("Failed to load Plyr from npm:",c),console.warn("Falling back to CDN loading..."),t(l,s,null==e?void 0:e.autoCreateCss)}if("cdn"===a)return t(l,s,null==e?void 0:e.autoCreateCss);throw new Error("Failed to load Plyr library")}(r);let n;if("string"==typeof r.container){const e=document.querySelector(r.container);if(!e)throw new Error(`Container element not found: ${r.container}`);n=e}else if("function"==typeof r.container){const e=r.container();if(!e)throw new Error("Container ref function returned null or undefined");n=e}else n=r.container;let i=n.querySelector("audio, video");if(!i&&r.source){const e=r.source.type||"video";i=document.createElement(e),r.source.sources&&r.source.sources.length>0&&r.source.sources.forEach((e=>{const t=document.createElement("source");t.src=e.src,e.type&&(t.type=e.type),null==i||i.appendChild(t)})),"video"===e&&r.source.poster&&(i.poster=r.source.poster),r.source.tracks&&r.source.tracks.length>0&&r.source.tracks.forEach((e=>{const t=document.createElement("track");t.kind=e.kind,t.label=e.label,t.src=e.src,e.srclang&&(t.srclang=e.srclang),e.default&&(t.default=e.default),null==i||i.appendChild(t)})),n.appendChild(i)}if(!i)throw new Error("No media element found and no source configuration provided");const a={...e,...r},l=new o(i,a),s=l;return s.getContainer=()=>n,s.getDefaultOptions=()=>({...e}),s.setSource=async e=>{if("string"==typeof e){const t=e.match(/\.(mp4|webm|ogg|ogv)$/i),r={type:t?"video":"audio",sources:[{src:e,type:t?`video/${e.split(".").pop()}`:`audio/${e.split(".").pop()}`}]};l.source=r}else l.source=e;return new Promise((e=>{const t=()=>{l.off("canplay",t),e()};l.on("canplay",t)}))},s.changeAspectRatio=e=>{n.style.setProperty("--plyr-aspect-ratio",e)},s.addShortcut=(e,t)=>{const r=r=>{r.key===e&&t()};document.addEventListener("keydown",r);const o=l.destroy;l.destroy=()=>{document.removeEventListener("keydown",r),o.call(l)}},s.getCaptureImage=()=>{if("video"!==l.source.type)return null;const e=l.media,t=document.createElement("canvas");t.width=e.videoWidth,t.height=e.videoHeight;const r=t.getContext("2d");null==r||r.drawImage(e,0,0,t.width,t.height);try{return t.toDataURL("image/jpeg")}catch(o){return console.error("Failed to capture image:",o),null}},s.downloadMedia=e=>{var t;const r=null==(t=l.source.sources[0])?void 0:t.src;if(!r)return void console.error("No media source found");const o=document.createElement("a");o.href=r,o.download=e||r.split("/").pop()||"media",o.style.display="none",document.body.appendChild(o),o.click(),document.body.removeChild(o)},s.setPoster=e=>{"video"===l.source.type&&(l.media.poster=e)},s.isFullscreen=()=>l.fullscreen.active,s.enterPictureInPicture=async()=>{if("video"!==l.source.type)throw new Error("Picture-in-picture is only available for videos");const e=l.media;if(document.pictureInPictureElement!==e){if(!e.requestPictureInPicture)throw new Error("Picture-in-picture not supported");await e.requestPictureInPicture()}},s.exitPictureInPicture=async()=>{document.pictureInPictureElement&&await document.exitPictureInPicture()},s.togglePictureInPicture=async()=>{document.pictureInPictureElement?await s.exitPictureInPicture():await s.enterPictureInPicture()},s.reload=()=>{const e=l.currentTime;l.media.load(),l.media.currentTime=e},s.getQualityOptions=()=>{var e;return(null==(e=a.quality)?void 0:e.options)||[]},s.setQuality=e=>{void 0!==l.quality&&(l.quality=e)},s.getSpeedOptions=()=>{var e;return(null==(e=a.speed)?void 0:e.options)||[]},s.setCustomControls=e=>{const t=l.source,r=l.currentTime,n=l.volume,i=l.muted;l.destroy();const s=new o(l.media,{...a,controls:e});s.source=t,s.currentTime=r,s.volume=n,s.muted=i,Object.keys(l).forEach((e=>{l[e]=s[e]}))},s.addMarker=(e,t,r)=>{var o,n;if(!(null==(o=a.markers)?void 0:o.enabled)||!(null==(n=a.markers)?void 0:n.points))return void console.error("Markers are not enabled");const i={time:e,label:t,color:r};a.markers.points.push(i),setTimeout((()=>{const e=new CustomEvent("plyr:markers-update");l.media.dispatchEvent(e)}),0)},s.removeMarker=e=>{var t,r;if(!(null==(t=a.markers)?void 0:t.enabled)||!(null==(r=a.markers)?void 0:r.points))return void console.error("Markers are not enabled");const o=a.markers.points.findIndex((t=>t.time===e));-1!==o&&(a.markers.points.splice(o,1),setTimeout((()=>{const e=new CustomEvent("plyr:markers-update");l.media.dispatchEvent(e)}),0))},s.clearMarkers=()=>{var e,t;(null==(e=a.markers)?void 0:e.enabled)&&(null==(t=a.markers)?void 0:t.points)?(a.markers.points=[],setTimeout((()=>{const e=new CustomEvent("plyr:markers-update");l.media.dispatchEvent(e)}),0)):console.error("Markers are not enabled")},s}async function o(e,t,o={}){const n="string"==typeof e?{type:e.match(/\.(mp4|webm|ogg|ogv)$/i)?"video":"audio",sources:[{src:e,type:e.match(/\.(mp4|webm|ogg|ogv)$/i)?`video/${e.split(".").pop()}`:`audio/${e.split(".").pop()}`}]}:e,i=await r({container:t,source:n,autoplay:o.autoplay||!1,muted:o.muted||!1,volume:void 0!==o.volume?o.volume:1,...o});if(o.autoplay)try{await i.play()}catch(a){console.warn("Autoplay failed:",a),i.muted=!0,await i.play().catch((e=>{console.error("Muted autoplay also failed:",e)}))}return i}exports.createPlaylist=async function(e,t,r={}){var n;if(!e.length)throw new Error("Empty playlist");let i=0;const a=e[0],l=await o(a,t,r);async function s(){i<e.length-1?i++:i=0,await l.setSource(e[i]),await l.play()}return(null==(n=r.loop)?void 0:n.active)&&l.on("ended",(()=>{s()})),{player:l,next:s,previous:async function(){i>0?i--:i=e.length-1,await l.setSource(e[i]),await l.play()},playIndex:async function(t){if(t<0||t>=e.length)throw new Error(`Invalid playlist index: ${t}`);i=t,await l.setSource(e[i]),await l.play()},get currentIndex(){return i},sources:e}},exports.createPlyrPlayer=r,exports.defaultPlyrOptions=e,exports.playWithPlyr=o;