mirror of
https://github.com/zhw2590582/ArtPlayer.git
synced 2026-10-08 10:56:15 -08:00
feat: add danmuku plugin documentation and i18n setup guide - Created a new documentation file for the danmuku plugin with code examples. - Added a comprehensive guide for language settings (i18n) including default languages, importing languages, adding new languages, and modifying existing languages. - Included code snippets for initializing the Artplayer with different language settings. Add internationalization support for multiple languages in Artplayer - Introduced Arabic (ar), Czech (cs), Spanish (es), Persian (fa), French (fr), Indonesian (id), Polish (pl), Russian (ru), Turkish (tr), Vietnamese (vi), and Traditional Chinese (zh-tw) translations. - Created corresponding JavaScript and ES module files for each language containing translated strings for player controls and settings. - Added a new documentation file for language settings in the project. feat: add initial implementation of start option page with interactive code execution buttons feat: add danmuku plugin documentation and i18n setup guide - Created a new documentation file for the danmuku plugin, providing code examples for usage. - Added a comprehensive guide for language settings, including default languages, importing languages, adding new languages, and modifying existing languages. - Included code snippets for initializing the Artplayer with different language settings and importing language files. refactor: update danmuku worker implementation and remove unused i18n files - Changed the way the Danmuku class creates a Web Worker by using a worker loader instead of creating a blob. - Removed Arabic, Czech, Spanish, Persian, French, Indonesian, Polish, Russian, Turkish, Vietnamese, and Traditional Chinese i18n files as they are no longer needed. - Updated the Vite build configuration to disable banners for inline workers and added a plugin to remove worker banners from generated code. feat: add danmuku plugin and i18n setup documentation - Created a new documentation file for the danmuku plugin, providing code examples for usage. - Added a new documentation file for language settings (i18n), detailing how to set default languages, import languages, and add new languages. - Included code snippets for initializing the Artplayer with different language settings and examples for modifying existing languages. feat: add danmuku plugin and i18n setup documentation - Created a new documentation file for the danmuku plugin with code examples. - Added a new documentation file for language settings (i18n) including import instructions and examples for setting default and custom languages. - Updated the i18n documentation to include details on adding and modifying languages. feat: add danmuku plugin and i18n setup documentation - Created a new JavaScript file for the danmuku plugin documentation with multiple code execution buttons. - Added a new JavaScript file for i18n (internationalization) setup documentation, including instructions for language settings, importing languages, and modifying language options. - Included examples for setting default languages and adding new languages in the i18n documentation. Refactor code structure for improved readability and maintainability
8 lines
14 KiB
JavaScript
8 lines
14 KiB
JavaScript
/*!
|
|
* artplayer-plugin-multiple-subtitles.js v1.1.1
|
|
* Github: https://github.com/zhw2590582/ArtPlayer
|
|
* (c) 2017-2026 Harvey Zhao
|
|
* Released under the MIT License.
|
|
*/
|
|
!function(e,t){"object"===typeof exports&&"undefined"!==typeof module?module.exports=t():"function"==typeof define&&define.amd?(t.ArtplayerPluginMultipleSubtitles=t(),define(function(){return t.ArtplayerPluginMultipleSubtitles})):(e="undefined"!==typeof globalThis?globalThis:e||self).ArtplayerPluginMultipleSubtitles=t()}(this,function(){"use strict";var e=Object.defineProperty,t=Object.defineProperties,n=Object.getOwnPropertyDescriptors,i=Object.getOwnPropertySymbols,a=Object.prototype.hasOwnProperty,r=Object.prototype.propertyIsEnumerable,s=(t,n,i)=>n in t?e(t,n,{enumerable:!0,configurable:!0,writable:!0,value:i}):t[n]=i,o=(e,t)=>{for(var n in t||(t={}))a.call(t,n)&&s(e,n,t[n]);if(i)for(var n of i(t))r.call(t,n)&&s(e,n,t[n]);return e},l=(e,t,n)=>new Promise((i,a)=>{var r=e=>{try{o(n.next(e))}catch(t){a(t)}},s=e=>{try{o(n.throw(e))}catch(t){a(t)}},o=e=>e.done?i(e.value):Promise.resolve(e.value).then(r,s);o((n=n.apply(e,t)).next())}),f={direction:"horizontal",snapToLines:!0,linePosition:"auto",lineAlign:"start",textPosition:"auto",positionAlign:"auto",size:100,alignment:"center"},u=function(e){e||(e={"&":"&","<":"<",">":">","&lrm":"\u200e","&rlm":"\u200f"," ":"\xa0"}),this.entities=e,this.parse=function(t,n){t=t.replace(/\0/g,"\ufffd");var i=Date.now(),a=0,r=t.split(/\r\n|\r|\n/),s=!1,o=[],l=[],u=[];function p(e,t){u.push({message:e,line:a+1,col:t})}var m=r[a],g=m.length,h="WEBVTT",v=0,b=6;for("\ufeff"===m[0]&&(v=1,b+=1),(g<b||m.indexOf(h)!==0+v||g>b&&" "!==m[b]&&"\t"!==m[b])&&p('No valid signature. (File needs to start with "WEBVTT".)'),a++;""!=r[a]&&void 0!=r[a];){if(p("No blank line after the signature."),-1!=r[a].indexOf("--\x3e")){s=!0;break}a++}for(;void 0!=r[a];){for(var x;!s&&""==r[a];)a++;if(!s&&void 0==r[a])break;x=Object.assign({},f,{id:"",startTime:0,endTime:0,pauseOnExit:!1,direction:"horizontal",snapToLines:!0,linePosition:"auto",lineAlign:"start",textPosition:"auto",positionAlign:"auto",size:100,alignment:"center",text:"",tree:null});var T=!0;if(-1==r[a].indexOf("--\x3e")){if(x.id=r[a],/^NOTE($|[ \t])/.test(x.id)){for(a++;""!=r[a]&&void 0!=r[a];)-1!=r[a].indexOf("--\x3e")&&p("Cannot have timestamp in a comment."),a++;continue}if(/^STYLE($|[ \t])/.test(x.id)){var y=[],w=!1;for(a++;""!=r[a]&&void 0!=r[a];)-1!=r[a].indexOf("--\x3e")&&(p("Cannot have timestamp in a style block."),w=!0),y.push(r[a]),a++;if(l.length){p("Style blocks cannot appear after the first cue.");continue}w||o.push(y.join("\n"));continue}if(""==r[++a]||void 0==r[a]){p("Cue identifier cannot be standalone.");continue}if(-1==r[a].indexOf("--\x3e")){T=!1,p("Cue identifier needs to be followed by timestamp.");continue}}s=!1;var O=new c(r[a],p),P=0;if(l.length>0&&(P=l[l.length-1].startTime),!T||O.parse(x,P)){for(a++;""!=r[a]&&void 0!=r[a];){if(-1!=r[a].indexOf("--\x3e")){p("Blank line missing before cue."),s=!0;break}""!=x.text&&(x.text+="\n"),x.text+=r[a],a++}var I=new d(x.text,p,n,e);x.tree=I.parse(x.startTime,x.endTime),l.push(x)}else for(x=null,a++;""!=r[a]&&void 0!=r[a];){if(-1!=r[a].indexOf("--\x3e")){s=!0;break}a++}}return l.sort(function(e,t){return e.startTime<t.startTime?-1:e.startTime>t.startTime?1:e.endTime>t.endTime?-1:e.endTime<t.endTime?1:0}),{cues:l,errors:u,time:Date.now()-i,styles:o}}},c=function(e,t){var n=/[\u0020\t\f]/,i=/[^\u0020\t\f]/,a=(e=e,0),r=function(e){t(e,a+1)};function s(t){for(;void 0!=e[a]&&t.test(e[a]);)a++}function o(t){for(var n="";void 0!=e[a]&&t.test(e[a]);)n+=e[a],a++;return n}function l(){var t,n,i,s,l="minutes";if(void 0!=e[a])if(/\d/.test(e[a]))if(((t=o(/\d/)).length>2||parseInt(t,10)>59)&&(l="hours"),":"==e[a])if(a++,2==(n=o(/\d/)).length){if("hours"==l||":"==e[a]){if(":"!=e[a])return void r("No seconds found or minutes is greater than 59.");if(a++,2!=(i=o(/\d/)).length)return void r("Must be exactly two digits.")}else{if(2!=t.length)return void r("Must be exactly two digits.");i=n,n=t,t="0"}if("."==e[a])if(a++,3==(s=o(/\d/)).length)if(parseInt(n,10)>59)r("You cannot have more than 59 minutes.");else{if(!(parseInt(i,10)>59))return 60*parseInt(t,10)*60+60*parseInt(n,10)+parseInt(i,10)+parseInt(s,10)/1e3;r("You cannot have more than 59 seconds.")}else r("Milliseconds must be given in three digits.");else r('No decimal separator (".") found.')}else r("Must be exactly two digits.");else r("No time unit separator found.");else r("Timestamp must start with a character in the range 0-9.");else r("No timestamp found.")}this.parse=function(t,o){if(s(n),t.startTime=l(),void 0!=t.startTime)if(t.startTime<o&&r("Start timestamp is not greater than or equal to start timestamp of previous cue."),i.test(e[a])&&r("Timestamp not separated from '--\x3e' by whitespace."),s(n),"-"==e[a])if(a++,"-"==e[a])if(a++,">"==e[a]){if(a++,i.test(e[a])&&r("'--\x3e' not separated from timestamp by whitespace."),s(n),t.endTime=l(),void 0!=t.endTime)return t.endTime<=t.startTime&&r("End timestamp is not greater than start timestamp."),i.test(e[a]),s(n),function(e,t){for(var i=e.split(n),a=[],s=0;s<i.length;s++)if(""!=i[s]){var o=i[s].indexOf(":"),l=i[s].slice(0,o),f=i[s].slice(o+1);if(-1!=a.indexOf(l)&&r("Duplicate setting."),a.push(l),""==f)return void r("No value for setting defined.");if("vertical"==l){if("rl"!=f&&"lr"!=f){r("Writing direction can only be set to 'rl' or 'rl'.");continue}t.direction=f}else if("line"==l){if(/,/.test(f)){var u=f.split(",");f=u[0];var c=u[1]}if(!/^[-\d](\d*)(\.\d+)?%?$/.test(f)){r("Line position takes a number or percentage.");continue}if(-1!=f.indexOf("-",1)){r("Line position can only have '-' at the start.");continue}if(-1!=f.indexOf("%")&&f.indexOf("%")!=f.length-1){r("Line position can only have '%' at the end.");continue}if("-"==f[0]&&"%"==f[f.length-1]){r("Line position cannot be a negative percentage.");continue}var d=f,p=!1;if("%"==f[f.length-1]&&(p=!0,d=f.slice(0,f.length-1),parseInt(f,10)>100)){r("Line position cannot be >100%.");continue}if(""===d||isNaN(d)||!isFinite(d)){r("Line position needs to be a number");continue}if(void 0!==c){if(!["start","center","end"].includes(c)){r("Line alignment needs to be one of start, center or end");continue}t.lineAlign=c}t.snapToLines=!p,t.linePosition=parseFloat(d),parseFloat(d).toString()!==d&&(t.nonSerializable=!0)}else if("position"==l){if(/,/.test(f)){u=f.split(","),f=u[0];var m=u[1]}if("%"!=f[f.length-1]){r("Text position must be a percentage.");continue}if(parseInt(f,10)>100||parseInt(f,10)<0){r("Text position needs to be between 0 and 100%.");continue}if(""===(d=f.slice(0,f.length-1))||isNaN(d)||!isFinite(d)){r("Line position needs to be a number");continue}if(void 0!==m){if(!["line-left","center","line-right"].includes(m)){r("Position alignment needs to be one of line-left, center or line-right");continue}t.positionAlign=m}t.textPosition=parseFloat(d)}else if("size"==l){if("%"!=f[f.length-1]){r("Size must be a percentage.");continue}if(parseInt(f,10)>100){r("Size cannot be >100%.");continue}var g=f.slice(0,f.length-1);if(void 0===g||""===g||isNaN(g)){r("Size needs to be a number"),g=100;continue}if((g=parseFloat(g))<0||g>100){r("Size needs to be between 0 and 100%.");continue}t.size=g}else if("align"==l){var h=["start","center","end","left","right"];if(-1==h.indexOf(f)){r("Alignment can only be set to one of "+h.join(", ")+".");continue}t.alignment=f}else r("Invalid setting.")}}(e.substring(a),t),!0}else r("No valid timestamp separator found.");else r("No valid timestamp separator found.");else r("No valid timestamp separator found.")},this.parseTimestamp=function(){var t=l();if(void 0==e[a])return t;r("Timestamp must not have trailing characters.")}},d=function(e,t,n,i){this.entities=i;var a=this,r=(e=e,0),s=function(e){"metadata"!=n&&t(e,r+1)};function l(){for(var t="data",n="",o="",l=[];void 0!=e[r-1]||0==r;){var f=e[r];if("data"==t)if("&"==f)o=f,t="escape";else if("<"==f&&""==n)t="tag";else{if("<"==f||void 0==f)return["text",n];n+=f}else if("escape"==t){if("<"==f||void 0==f){let e;return s("Incorrect escape."),(e=o.match(/^&#([0-9]+)$/))?n+=String.fromCharCode(e[1]):a.entities[o]?n+=a.entities[o]:n+=o,["text",n]}if("&"==f)s("Incorrect escape."),n+=o,o=f;else if(/[a-z#0-9]/i.test(f))o+=f;else if(";"==f){let e;(e=o.match(/^&#(x?[0-9]+)$/))?n+=String.fromCharCode("0"+e[1]):a.entities[o+f]?n+=a.entities[o+f]:(e=Object.keys(i).find(e=>o.startsWith(e)))?n+=a.entities[e]+o.slice(e.length)+f:(s("Incorrect escape."),n+=o+";"),t="data"}else s("Incorrect escape."),n+=o+f,t="data"}else if("tag"==t)if("\t"==f||"\n"==f||"\f"==f||" "==f)t="start tag annotation";else if("."==f)t="start tag class";else if("/"==f)t="end tag";else if(/\d/.test(f))n=f,t="timestamp tag";else{if(">"==f||void 0==f)return">"==f&&r++,["start tag","",[],""];n=f,t="start tag"}else if("start tag"==t)if("\t"==f||"\f"==f||" "==f)t="start tag annotation";else if("\n"==f)o=f,t="start tag annotation";else if("."==f)t="start tag class";else{if(">"==f||void 0==f)return">"==f&&r++,["start tag",n,[],""];n+=f}else if("start tag class"==t)if("\t"==f||"\f"==f||" "==f)o&&l.push(o),o="",t="start tag annotation";else if("\n"==f)o&&l.push(o),o=f,t="start tag annotation";else if("."==f)o&&l.push(o),o="";else{if(">"==f||void 0==f)return">"==f&&r++,o&&l.push(o),["start tag",n,l,""];o+=f}else if("start tag annotation"==t){if(">"==f||void 0==f)return">"==f&&r++,["start tag",n,l,o=o.split(/[\u0020\t\f\r\n]+/).filter(function(e){if(e)return!0}).join(" ")];o+=f}else if("end tag"==t){if(">"==f||void 0==f)return">"==f&&r++,["end tag",n];n+=f}else if("timestamp tag"==t){if(">"==f||void 0==f)return">"==f&&r++,["timestamp",n];n+=f}else s("Never happens.");r++}}this.parse=function(t,i){var a={children:[]},f=a,u=[];function d(e){f.children.push({type:"object",name:e[1],classes:e[2],children:[],parent:f}),f=f.children[f.children.length-1]}function p(e){for(var t=f;t;){if(t.name==e)return!0;t=t.parent}}for(;void 0!=e[r];){var m=l();if("text"==m[0])f.children.push({type:"text",value:m[1],parent:f});else if("start tag"==m[0]){"chapters"==n&&s("Start tags not allowed in chapter title text.");var g=m[1];"v"!=g&&"lang"!=g&&""!=m[3]&&s("Only <v> and <lang> can have an annotation."),"c"==g||"i"==g||"b"==g||"u"==g||"ruby"==g||"rt"==g&&"ruby"==f.name?d(m):"v"==g?(p("v")&&s("<v> cannot be nested inside itself."),d(m),f.value=m[3],m[3]||s("<v> requires an annotation.")):"lang"==g?(d(m),f.value=m[3]):s("Incorrect start tag.")}else if("end tag"==m[0])"chapters"==n&&s("End tags not allowed in chapter title text."),m[1]==f.name?f=f.parent:"ruby"==m[1]&&"rt"==f.name?f=f.parent.parent:s("Incorrect end tag.");else if("timestamp"==m[0]){"chapters"==n&&s("Timestamp not allowed in chapter title text.");var h=new c(m[1],s).parseTimestamp();void 0!=h&&((h<=t||h>=i)&&s("Timestamp must be between start timestamp and end timestamp."),u.length>0&&u[u.length-1]>=h&&s("Timestamp must be greater than any previous timestamp."),f.children.push({type:"timestamp",value:h,parent:f}),u.push(h))}}for(;f.parent;)"v"!=f.name&&s("Required end tag missing."),f=f.parent;return function e(t){const n=o({},t);return t.children&&(n.children=t.children.map(e)),n.parent&&delete n.parent,n}(a)}},p=function(){function e(e){const t=("00"+1e3*(e-Math.floor(e)).toFixed(3)).slice(-3);let n=0,i=0,a=0;return e>=3600&&(n=Math.floor(e/3600)),i=Math.floor((e-3600*n)/60),a=Math.floor(e-3600*n-60*i),(n?n+":":"")+(""+i).padStart(2,"0")+":"+(""+a).padStart(2,"0")+"."+t}function t(n){for(var i="",a=0;a<n.length;a++){var r=n[a];if("text"==r.type)i+=r.value.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">");else if("object"==r.type){if(i+="<"+r.name,r.classes)for(var s=0;s<r.classes.length;s++)i+="."+r.classes[s];r.value&&(i+=" "+r.value),i+=">",r.children&&(i+=t(r.children)),i+="</"+r.name+">"}else"timestamp"==r.type?i+="<"+e(r.value)+">":i+="<"+r.value+">"}return i}function n(n){return(void 0!==n.id?n.id+"\n":"")+e(n.startTime)+" --\x3e "+e(n.endTime)+function(e){var t="";const n=Object.keys(f).filter(t=>e[t]!==f[t]);return n.includes("direction")&&(t+=" vertical:"+e.direction),n.includes("alignment")&&(t+=" align:"+e.alignment),n.includes("size")&&(t+=" size:"+e.size+"%"),(n.includes("lineAlign")||n.includes("linePosition"))&&(t+=" line:"+e.linePosition+(e.snapToLines?"":"%")+(e.lineAlign&&e.lineAlign!=f.lineAlign?","+e.lineAlign:"")),(n.includes("textPosition")||n.includes("positionAlign"))&&(t+=" position:"+e.textPosition+"%"+(e.positionAlign&&e.positionAlign!==f.positionAlign?","+e.positionAlign:"")),t}(n)+"\n"+t(n.tree.children)+"\n\n"}function i(e){return"STYLE\n"+e+"\n\n"}this.serialize=function(e,t){var a="WEBVTT\n\n";if(t)for(var r=0;r<t.length;r++)a+=i(t[r]);for(r=0;r<e.length;r++)a+=n(e[r]);return a}};function m({subtitles:e=[]}){return i=>l(null,null,function*(){const{unescape:a,getExt:r,srtToVtt:s,assToVtt:f}=i.constructor.utils,c=new u,d=new p,m=yield Promise.all(e.map(e=>function(e,t){return l(this,arguments,function*(e,{getExt:t,srtToVtt:n,assToVtt:i}){const a=yield fetch(e.url),r=yield a.arrayBuffer(),s=new TextDecoder(e.encoding||"utf-8").decode(r);switch(e.type||t(e.url)){case"srt":return n(s);case"ass":return i(s);case"vtt":return s;default:return""}})}(e,{getExt:r,srtToVtt:s,assToVtt:f}))),g=m.map((t,n)=>{const i=c.parse(t,"metadata");return i.url=e[n].url,i.name=e[n].name,i});let h="";function v(e=[]){const r=function(e){const t=(new u).parse("","metadata");for(let n=0;n<e.length;n++){const i=e[n];if(!i.updated){i.updated=!0;for(let e=0;e<i.cues.length;e++){const t=i.cues[e];for(let e=0;e<t.tree.children.length;e++){const n=t.tree.children[e];n.value=`<div class="art-subtitle-${i.name}">${n.value}</div>`}}}t.cues.push(...i.cues)}return t}(e),s=d.serialize(r.cues);URL.revokeObjectURL(h);const l=URL.createObjectURL(new Blob([s],{type:"text/vtt"}));var f;h=l,i.option.subtitle.escape=!1,i.subtitle.init((f=o({},i.option.subtitle),t(f,n({url:l,type:"vtt",onVttLoad:a}))))}return v(g),{name:"multipleSubtitles",tracks:(e=[])=>v(e.map(e=>g.find(t=>t.name===e))),reset:()=>v(g)}})}return"undefined"!==typeof window&&(window.artplayerPluginMultipleSubtitles=m),m});
|