UNPKG

@sanyueqi/web-components

Version:

Web components

2 lines (1 loc) 8.76 kB
!function(t){"function"==typeof define&&define.amd?define(t):t()}(function(){"use strict";const t=new class{playId="aplayer-container";constructor(){this.loadedAPlayer=!1,this.loadedMetingJS=!1,this.hasMusicInteracted=!1}init(t){t.enable&&(t.playId&&(this.playId=t.playId),"undefined"!=typeof window&&(this.config=t,this.processConfig(),this.loadCSS(),this.initPlayer()))}processConfig(){this.aplayerCssPath=this.config.aplayerCss?this.config.aplayerCss.startsWith("http://")||this.config.aplayerCss.startsWith("https://")?this.config.aplayerCss:this.processUrl(this.config.aplayerCss):"https://cdn.jsdelivr.net/npm/aplayer@1.10.1/dist/APlayer.min.css",this.aplayerJsPath=this.config.aplayerJs?this.config.aplayerJs.startsWith("http://")||this.config.aplayerJs.startsWith("https://")?this.config.aplayerJs:this.processUrl(this.config.aplayerJs):"https://cdn.jsdelivr.net/npm/aplayer@1.10.1/dist/APlayer.min.js",this.metingJsPath=this.config.meting?.jsPath?this.config.meting.jsPath.startsWith("http://")||this.config.meting.jsPath.startsWith("https://")?this.config.meting.jsPath:this.processUrl(this.config.meting.jsPath):"https://cdn.jsdelivr.net/npm/meting@2/dist/Meting.min.js",this.processedLocalPlaylist="local"===this.config.mode&&this.config.local?.playlist?this.config.local.playlist.map(t=>({...t,url:this.processUrl(t.url),cover:t.cover?this.processUrl(t.cover):void 0})):null}processUrl(t){return t.startsWith("http")?t:this.joinUrl("",this.getBaseUrl(),t)}getBaseUrl(){return"undefined"!=typeof window&&window.location.pathname.includes("/")?window.location.pathname.substring(0,window.location.pathname.lastIndexOf("/")+1):""}joinUrl(...t){return t.join("/").replace(/\/+/g,"/")}loadCSS(){if(document.querySelector(`link[href="${this.aplayerCssPath}"]`))return;const t=document.createElement("link");t.rel="stylesheet",t.href=this.aplayerCssPath,document.head.appendChild(t)}loadAPlayer(){return new Promise(t=>{if(window.APlayer)return this.loadedAPlayer=!0,void t(window.APlayer);const e=document.createElement("script");e.src=this.aplayerJsPath,e.async=!0,e.onload=()=>{this.loadedAPlayer=!0,t(window.APlayer)},e.onerror=()=>{console.error("Failed to load APlayer"),t(null)},document.head.appendChild(e)})}loadMetingJS(){return new Promise(t=>{if(window.customElements?.get("meting-js"))return this.loadedMetingJS=!0,void t(!0);const e=document.createElement("script");e.src=this.metingJsPath,e.async=!0,e.onload=()=>{this.loadedMetingJS=!0,"meting"===this.config.mode&&this.config.meting?.api&&(window.meting_api=this.config.meting.api),this.setupMetingElements(),t(!0)},e.onerror=()=>{console.error("Failed to load MetingJS"),t(!1)},document.head.appendChild(e)})}setupMetingElements(){setTimeout(()=>{document.querySelectorAll("meting-js").forEach(t=>{this.setupAPlayerContainer(t.aplayer)})},100)}setupAPlayerContainer(t){if(!t)return;const e=setInterval(()=>{t&&t.container&&(clearInterval(e),t.container.setAttribute("data-positioned","right"),requestAnimationFrame(()=>{t.container&&(t.container.style.right="0",t.container.style.left="unset",setTimeout(()=>{if(t.container.setAttribute("data-initialized","true"),this.config.player?.lrcHidden&&t.lrc){t.lrc.hide();const e=t.container.querySelector(".aplayer-icon-lrc");e&&e.classList.add("aplayer-icon-lrc-inactivity")}this.config.player?.autoplay&&t.paused&&this.tryAutoplay(t)},200))}))},50);setTimeout(()=>clearInterval(e),1e4)}tryAutoplay(t){if(this.config.player?.autoplay)if(this.hasMusicInteracted){if(t&&t.paused){const e=t.play();e&&"function"==typeof e.catch&&e.catch(t=>{"NotAllowedError"!==t.name&&console.log("自动播放失败:",t.name)})}}else{const e=()=>{if(!this.hasMusicInteracted&&(this.hasMusicInteracted=!0,t&&t.paused)){const e=t.play();e&&"function"==typeof e.catch&&e.catch(t=>{"NotAllowedError"!==t.name&&console.log("自动播放失败:",t.name)})}};["click","keydown","touchstart","scroll"].forEach(t=>{document.addEventListener(t,e,{once:!0,passive:!0})})}}createContainer(){let t=document.getElementById(`#${this.playId}`);return t||(t=document.createElement("div"),t.id=this.playId,this.config.responsive?.mobile?.hide&&t.classList.add("mobile-hide"),document.body.appendChild(t),t)}renderMetingPlayer(t){if("meting"!==this.config.mode||!this.config.meting)return;const e=document.createElement("meting-js");e.setAttribute("server",this.config.meting.server||"netease"),e.setAttribute("type",this.config.meting.type||"playlist"),e.setAttribute("id",this.config.meting.id||""),this.config.meting.api&&e.setAttribute("api",this.config.meting.api),this.config.meting.auth&&e.setAttribute("auth",this.config.meting.auth),this.setPlayerAttributes(e),t.appendChild(e)}renderLocalPlayer(t){if("local"===this.config.mode&&this.processedLocalPlaylist&&0!==this.processedLocalPlaylist.length)if(1===this.processedLocalPlaylist.length){const e=this.processedLocalPlaylist[0],i=document.createElement("meting-js");if(i.setAttribute("name",e.name||""),i.setAttribute("artist",e.artist||""),i.setAttribute("url",e.url||""),e.cover&&i.setAttribute("cover",e.cover),this.setPlayerAttributes(i,e.lrc?"2":"0"),e.lrc){const t=document.createElement("pre");t.hidden=!0,t.textContent=e.lrc,i.appendChild(t)}t.appendChild(i)}else{const e=document.createElement("div");e.id="local-aplayer",t.appendChild(e)}}setPlayerAttributes(t,e=null){const i=this.config.player||{};t.setAttribute("fixed",i.fixed??1?"true":"false"),t.setAttribute("mini",i.mini??1?"true":"false"),t.setAttribute("autoplay",i.autoplay?"true":"false"),t.setAttribute("theme",i.theme||"#b7daff"),t.setAttribute("loop",i.loop||"all"),t.setAttribute("order",i.order||"list"),t.setAttribute("preload",i.preload||"auto"),t.setAttribute("volume",String(i.volume??.7)),t.setAttribute("mutex",!1!==i.mutex?"true":"false"),t.setAttribute("lrc-type",null!==e?e:String(i.lrcType??3)),t.setAttribute("list-folded",i.listFolded?"true":"false"),t.setAttribute("list-max-height",i.listMaxHeight||"340px"),t.setAttribute("storage-name",i.storageName||"aplayer-setting")}async initLocalPlayer(){if("local"!==this.config.mode||!this.processedLocalPlaylist||this.processedLocalPlaylist.length<=1)return;const t=await this.loadAPlayer();if(!t)return;const e=document.getElementById("local-aplayer");if(!e)return;const i={container:e,audio:this.processedLocalPlaylist.map(t=>({name:t.name,artist:t.artist,url:t.url,cover:t.cover,lrc:t.lrc||void 0,type:"auto"})),mutex:!1!==this.config.player?.mutex,lrcType:void 0!==this.config.player?.lrcType?this.config.player.lrcType:0,fixed:this.config.player?.fixed??!0,mini:this.config.player?.mini??!0,autoplay:this.config.player?.autoplay||!1,theme:this.config.player?.theme||"#b7daff",loop:this.config.player?.loop||"all",order:this.config.player?.order||"list",preload:this.config.player?.preload||"auto",volume:this.config.player?.volume||.7,listFolded:this.config.player?.listFolded||!1,listMaxHeight:this.config.player?.listMaxHeight||"340px",storageName:this.config.player?.storageName||"aplayer-setting"};try{const e=new t(i);this.setupAPlayerContainer(e)}catch(t){console.error("Failed to initialize APlayer:",t)}}addStyles(){const t=document.createElement("style");t.textContent=`\n #${this.playId} {\n position: relative;\n z-index: 1000;\n }\n\n #${this.playId} .aplayer-fixed {\n z-index: 9999;\n }\n\n #${this.playId} .aplayer.aplayer-fixed {\n animation: none !important;\n }\n \n #${this.playId} .aplayer.aplayer-fixed .aplayer-body {\n animation: none !important;\n transition: none !important;\n }\n \n #${this.playId} .aplayer.aplayer-fixed[data-positioned="right"] {\n right: 0 !important;\n left: unset !important;\n }\n\n @media (max-width: 768px) {\n #${this.playId}.mobile-hide {\n display: none !important;\n }\n }\n `,document.head.appendChild(t)}async initPlayer(){const t=this.createContainer();this.addStyles(),"meting"===this.config.mode?(this.renderMetingPlayer(t),await this.loadAPlayer(),await this.loadMetingJS()):"local"===this.config.mode&&(this.renderLocalPlayer(t),this.processedLocalPlaylist&&this.processedLocalPlaylist.length>1?"loading"===document.readyState?document.addEventListener("DOMContentLoaded",()=>this.initLocalPlayer()):await this.initLocalPlayer():(await this.loadAPlayer(),await this.loadMetingJS()))}destroy(){const t=document.getElementById(`#${this.playId}`);t&&t.remove();document.querySelectorAll("style").forEach(t=>{t.textContent.includes(`#${this.playId}`)&&t.remove()})}};function e(e){t.init(e)}function i(){t.destroy()}"undefined"!=typeof module&&module.exports&&(module.exports={init:e,destroy:i}),"undefined"!=typeof window&&(window.MusicPlayer={init:e,destroy:i})});