Skip to content

Commit 39a1fb1

Browse files
committed
feat: add danmuku plugin documentation and i18n setup guide
- Created a new documentation file for the danmuku plugin, including code snippets for usage. - Added a new i18n setup guide, detailing language settings, default languages, language imports, and how to add or modify languages. - Included example code for initializing the Artplayer with different languages and importing language files.
1 parent bd43b66 commit 39a1fb1

File tree

82 files changed

+150499
-103
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+150499
-103
lines changed

docs/compiled/artplayer-plugin-ads.mjs

Lines changed: 245 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,248 @@
44
* (c) 2017-2026 Harvey Zhao
55
* Released under the MIT License.
66
*/
7-
function l(e){return n=>{!function(e){const{version:n,utils:{errorHandle:a}}=e.constructor,i=n.split(".").map(Number);a(i[0]+i[1]/100>=5,`Artplayer.js@${n} is not compatible the artplayerPluginAds@${l.version}. Please update it to version Artplayer.js@5.x.x`)}(n);const{template:{$player:a},icons:{volume:i,volumeClose:t,fullscreenOn:r,fullscreenOff:d,loading:o},constructor:{validator:s,utils:{query:p,append:u,setStyle:c}}}=n;e=s({html:"",video:"",url:"",playDuration:5,totalDuration:10,muted:!1,i18n:{close:"\u5173\u95ed\u5e7f\u544a",countdown:"%s\u79d2",detail:"\u67e5\u770b\u8be6\u60c5",canBeClosed:"%s\u79d2\u540e\u53ef\u5173\u95ed\u5e7f\u544a"},...e},{html:"?string",video:"?string",url:"?string",playDuration:"number",totalDuration:"number",muted:"?boolean",i18n:{close:"string",countdown:"string",detail:"string",canBeClosed:"string"}});let y=null,g=null,m=null,f=null,v=null,x=null,h=0,b=null,w=!1,$=!1,k=!1;function D(l,e){return e.replace("%s",l)}function A(){w=!0,n.play(),e.video&&y.pause(),c(n.template.$ads,"display","none"),n.emit("artplayerPluginAds:skip",e)}function C(){w||(b=setTimeout(()=>{h+=1;const l=e.playDuration-h;l>=1?m.innerHTML=D(l,e.i18n.canBeClosed):(m.innerHTML=e.i18n.close,k||(k=!0)),f.innerHTML=D(e.totalDuration-h,e.i18n.countdown),h>=e.totalDuration?A():C()},1e3))}function P(){w||clearTimeout(b)}function B(){$||($=!0,function(){n.template.$ads=u(a,'<div class="artplayer-plugin-ads"></div>'),y=u(n.template.$ads,e.video?`<video class="artplayer-plugin-ads-video" src="${e.video}" loop playsInline></video>`:`<div class="artplayer-plugin-ads-html">${e.html}</div>`),x=u(n.template.$ads,'<div class="artplayer-plugin-ads-loading"></div>'),u(x,o),g=u(n.template.$ads,`<div class="artplayer-plugin-ads-timer">\n <div class="artplayer-plugin-ads-close">${e.playDuration<=0?e.i18n.close:D(e.playDuration,e.i18n.canBeClosed)}</div>\n <div class="artplayer-plugin-ads-countdown">${D(e.totalDuration,e.i18n.countdown)}</div>\n </div>`),m=p(".artplayer-plugin-ads-close",g),f=p(".artplayer-plugin-ads-countdown",g),e.playDuration>=e.totalDuration&&c(m,"display","none"),n.proxy(m,"click",()=>{k&&A()}),v=u(n.template.$ads,`<div class="artplayer-plugin-ads-control">\n <div class="artplayer-plugin-ads-detail">${e.i18n.detail}</div>\n <div class="artplayer-plugin-ads-muted"></div>\n <div class="artplayer-plugin-ads-fullscreen"></div>\n </div>`);const l=p(".artplayer-plugin-ads-detail",v),s=p(".artplayer-plugin-ads-muted",v),h=p(".artplayer-plugin-ads-fullscreen",v);if(e.url?n.proxy(l,"click",()=>{window.open(e.url),n.emit("artplayerPluginAds:click",e)}):c(l,"display","none"),e.video){const l=u(s,i),a=u(s,t);c(a,"display","none"),e.muted&&(y.muted=!0,c(l,"display","none"),c(a,"display","inline-flex")),n.proxy(s,"click",()=>{y.muted=!y.muted,y.muted?(c(l,"display","none"),c(a,"display","inline-flex")):(c(l,"display","inline-flex"),c(a,"display","none"))})}else c(s,"display","none");const b=u(h,r),w=u(h,d);c(w,"display","none"),n.proxy(h,"click",()=>{n.fullscreen=!n.fullscreen,n.fullscreen?(c(b,"display","inline-flex"),c(w,"display","none")):(c(b,"display","none"),c(w,"display","inline-flex"))}),n.proxy(y,"click",()=>{e.url&&window.open(e.url),n.emit("artplayerPluginAds:click",e)})}(),n.pause(),e.video?(n.proxy(y,"error",A),n.proxy(y,"loadedmetadata",()=>{C(),y.play(),c(g,"display","flex"),c(v,"display","flex"),c(x,"display","none")})):(C(),c(g,"display","flex"),c(v,"display","flex"),c(x,"display","none")),n.on("document:visibilitychange",()=>{document.hidden?P():C()}))}return n.on("ready",()=>{n.once("play",B),n.once("video:playing",B)}),{name:"artplayerPluginAds",skip:A,pause:P,play:C}}}if("undefined"!==typeof document&&!document.getElementById("artplayer-plugin-ads")){const l=document.createElement("style");l.id="artplayer-plugin-ads",l.textContent=".artplayer-plugin-ads{position:absolute;z-index:150;inset:0;width:100%;height:100%;overflow:hidden;font-size:13px;line-height:1;color:#fff;background-color:#000}.artplayer-plugin-ads .artplayer-plugin-ads-html{display:flex;align-items:center;justify-content:center;width:100%;height:100%}.artplayer-plugin-ads .artplayer-plugin-ads-video{width:100%;height:100%}.artplayer-plugin-ads .artplayer-plugin-ads-timer{display:none;position:absolute;top:10px;right:10px}.artplayer-plugin-ads .artplayer-plugin-ads-timer>div{display:flex;align-items:center;background-color:#00000080;border-radius:15px;margin-left:5px;padding:5px 10px;cursor:pointer}.artplayer-plugin-ads .artplayer-plugin-ads-control{display:none;position:absolute;bottom:10px;right:10px}.artplayer-plugin-ads .artplayer-plugin-ads-control>div{display:flex;align-items:center;background-color:#00000080;border-radius:15px;margin-left:5px;padding:5px 10px;cursor:pointer}.artplayer-plugin-ads .artplayer-plugin-ads-control .art-icon svg{width:20px;height:20px}.artplayer-plugin-ads .artplayer-plugin-ads-loading{display:flex;align-items:center;justify-content:center;position:absolute;inset:0;width:100%;height:100%}",document.head.appendChild(l)}export{l as default};
7+
const style = ".artplayer-plugin-ads {\n position: absolute;\n z-index: 150;\n left: 0;\n top: 0;\n right: 0;\n bottom: 0;\n width: 100%;\n height: 100%;\n overflow: hidden;\n font-size: 13px;\n line-height: 1;\n color: #fff;\n background-color: #000;\n}\n.artplayer-plugin-ads .artplayer-plugin-ads-html {\n display: flex;\n align-items: center;\n justify-content: center;\n width: 100%;\n height: 100%;\n}\n.artplayer-plugin-ads .artplayer-plugin-ads-video {\n width: 100%;\n height: 100%;\n}\n.artplayer-plugin-ads .artplayer-plugin-ads-timer {\n display: none;\n position: absolute;\n top: 10px;\n right: 10px;\n}\n.artplayer-plugin-ads .artplayer-plugin-ads-timer > div {\n display: flex;\n align-items: center;\n background-color: rgba(0, 0, 0, 0.5);\n border-radius: 15px;\n margin-left: 5px;\n padding: 5px 10px;\n cursor: pointer;\n}\n.artplayer-plugin-ads .artplayer-plugin-ads-control {\n display: none;\n position: absolute;\n bottom: 10px;\n right: 10px;\n}\n.artplayer-plugin-ads .artplayer-plugin-ads-control > div {\n display: flex;\n align-items: center;\n background-color: rgba(0, 0, 0, 0.5);\n border-radius: 15px;\n margin-left: 5px;\n padding: 5px 10px;\n cursor: pointer;\n}\n.artplayer-plugin-ads .artplayer-plugin-ads-control .art-icon svg {\n width: 20px;\n height: 20px;\n}\n.artplayer-plugin-ads .artplayer-plugin-ads-loading {\n display: flex;\n align-items: center;\n justify-content: center;\n position: absolute;\n left: 0;\n top: 0;\n right: 0;\n bottom: 0;\n width: 100%;\n height: 100%;\n}\n";
8+
function checkVersion(art) {
9+
const {
10+
version,
11+
utils: { errorHandle }
12+
} = art.constructor;
13+
const arr = version.split(".").map(Number);
14+
const major = arr[0];
15+
const minor = arr[1] / 100;
16+
errorHandle(
17+
major + minor >= 5,
18+
`Artplayer.js@${version} is not compatible the artplayerPluginAds@${artplayerPluginAds.version}. Please update it to version Artplayer.js@5.x.x`
19+
);
20+
}
21+
function artplayerPluginAds(option) {
22+
return (art) => {
23+
checkVersion(art);
24+
const {
25+
template: { $player },
26+
icons: { volume, volumeClose, fullscreenOn, fullscreenOff, loading },
27+
constructor: {
28+
validator,
29+
utils: { query, append, setStyle }
30+
}
31+
} = art;
32+
option = validator(
33+
{
34+
html: "",
35+
video: "",
36+
url: "",
37+
playDuration: 5,
38+
totalDuration: 10,
39+
muted: false,
40+
i18n: {
41+
close: "关闭广告",
42+
countdown: "%s秒",
43+
detail: "查看详情",
44+
canBeClosed: "%s秒后可关闭广告"
45+
},
46+
...option
47+
},
48+
{
49+
html: "?string",
50+
video: "?string",
51+
url: "?string",
52+
playDuration: "number",
53+
totalDuration: "number",
54+
muted: "?boolean",
55+
i18n: {
56+
close: "string",
57+
countdown: "string",
58+
detail: "string",
59+
canBeClosed: "string"
60+
}
61+
}
62+
);
63+
let $ads = null;
64+
let $timer = null;
65+
let $close = null;
66+
let $countdown = null;
67+
let $control = null;
68+
let $loading = null;
69+
let time = 0;
70+
let timer = null;
71+
let isEnd = false;
72+
let isInit = false;
73+
let isCanClose = false;
74+
function getI18n(val, str) {
75+
return str.replace("%s", val);
76+
}
77+
function skip() {
78+
isEnd = true;
79+
art.play();
80+
if (option.video)
81+
$ads.pause();
82+
setStyle(art.template.$ads, "display", "none");
83+
art.emit("artplayerPluginAds:skip", option);
84+
}
85+
function play() {
86+
if (isEnd)
87+
return;
88+
timer = setTimeout(() => {
89+
time += 1;
90+
const playDuration = option.playDuration - time;
91+
if (playDuration >= 1) {
92+
$close.innerHTML = getI18n(playDuration, option.i18n.canBeClosed);
93+
} else {
94+
$close.innerHTML = option.i18n.close;
95+
if (!isCanClose) {
96+
isCanClose = true;
97+
}
98+
}
99+
$countdown.innerHTML = getI18n(option.totalDuration - time, option.i18n.countdown);
100+
if (time >= option.totalDuration) {
101+
skip();
102+
} else {
103+
play();
104+
}
105+
}, 1e3);
106+
}
107+
function pause() {
108+
if (isEnd)
109+
return;
110+
clearTimeout(timer);
111+
}
112+
function show() {
113+
art.template.$ads = append($player, '<div class="artplayer-plugin-ads"></div>');
114+
$ads = append(
115+
art.template.$ads,
116+
option.video ? `<video class="artplayer-plugin-ads-video" src="${option.video}" loop playsInline></video>` : `<div class="artplayer-plugin-ads-html">${option.html}</div>`
117+
);
118+
$loading = append(art.template.$ads, '<div class="artplayer-plugin-ads-loading"></div>');
119+
append($loading, loading);
120+
$timer = append(
121+
art.template.$ads,
122+
`<div class="artplayer-plugin-ads-timer">
123+
<div class="artplayer-plugin-ads-close">${option.playDuration <= 0 ? option.i18n.close : getI18n(option.playDuration, option.i18n.canBeClosed)}</div>
124+
<div class="artplayer-plugin-ads-countdown">${getI18n(
125+
option.totalDuration,
126+
option.i18n.countdown
127+
)}</div>
128+
</div>`
129+
);
130+
$close = query(".artplayer-plugin-ads-close", $timer);
131+
$countdown = query(".artplayer-plugin-ads-countdown", $timer);
132+
if (option.playDuration >= option.totalDuration) {
133+
setStyle($close, "display", "none");
134+
}
135+
art.proxy($close, "click", () => {
136+
if (isCanClose) {
137+
skip();
138+
}
139+
});
140+
$control = append(
141+
art.template.$ads,
142+
`<div class="artplayer-plugin-ads-control">
143+
<div class="artplayer-plugin-ads-detail">${option.i18n.detail}</div>
144+
<div class="artplayer-plugin-ads-muted"></div>
145+
<div class="artplayer-plugin-ads-fullscreen"></div>
146+
</div>`
147+
);
148+
const $detail = query(".artplayer-plugin-ads-detail", $control);
149+
const $muted = query(".artplayer-plugin-ads-muted", $control);
150+
const $fullscreen = query(".artplayer-plugin-ads-fullscreen", $control);
151+
if (option.url) {
152+
art.proxy($detail, "click", () => {
153+
window.open(option.url);
154+
art.emit("artplayerPluginAds:click", option);
155+
});
156+
} else {
157+
setStyle($detail, "display", "none");
158+
}
159+
if (option.video) {
160+
const $volume = append($muted, volume);
161+
const $volumeClose = append($muted, volumeClose);
162+
setStyle($volumeClose, "display", "none");
163+
if (option.muted) {
164+
$ads.muted = true;
165+
setStyle($volume, "display", "none");
166+
setStyle($volumeClose, "display", "inline-flex");
167+
}
168+
art.proxy($muted, "click", () => {
169+
$ads.muted = !$ads.muted;
170+
if ($ads.muted) {
171+
setStyle($volume, "display", "none");
172+
setStyle($volumeClose, "display", "inline-flex");
173+
} else {
174+
setStyle($volume, "display", "inline-flex");
175+
setStyle($volumeClose, "display", "none");
176+
}
177+
});
178+
} else {
179+
setStyle($muted, "display", "none");
180+
}
181+
const $fullscreenOn = append($fullscreen, fullscreenOn);
182+
const $fullscreenOff = append($fullscreen, fullscreenOff);
183+
setStyle($fullscreenOff, "display", "none");
184+
art.proxy($fullscreen, "click", () => {
185+
art.fullscreen = !art.fullscreen;
186+
if (art.fullscreen) {
187+
setStyle($fullscreenOn, "display", "inline-flex");
188+
setStyle($fullscreenOff, "display", "none");
189+
} else {
190+
setStyle($fullscreenOn, "display", "none");
191+
setStyle($fullscreenOff, "display", "inline-flex");
192+
}
193+
});
194+
art.proxy($ads, "click", () => {
195+
if (option.url)
196+
window.open(option.url);
197+
art.emit("artplayerPluginAds:click", option);
198+
});
199+
}
200+
function init() {
201+
if (isInit)
202+
return;
203+
isInit = true;
204+
show();
205+
art.pause();
206+
if (option.video) {
207+
art.proxy($ads, "error", skip);
208+
art.proxy($ads, "loadedmetadata", () => {
209+
play();
210+
$ads.play();
211+
setStyle($timer, "display", "flex");
212+
setStyle($control, "display", "flex");
213+
setStyle($loading, "display", "none");
214+
});
215+
} else {
216+
play();
217+
setStyle($timer, "display", "flex");
218+
setStyle($control, "display", "flex");
219+
setStyle($loading, "display", "none");
220+
}
221+
art.on("document:visibilitychange", () => {
222+
if (document.hidden) {
223+
pause();
224+
} else {
225+
play();
226+
}
227+
});
228+
}
229+
art.on("ready", () => {
230+
art.once("play", init);
231+
art.once("video:playing", init);
232+
});
233+
return {
234+
name: "artplayerPluginAds",
235+
skip,
236+
pause,
237+
play
238+
};
239+
};
240+
}
241+
if (typeof document !== "undefined") {
242+
if (!document.getElementById("artplayer-plugin-ads")) {
243+
const $style = document.createElement("style");
244+
$style.id = "artplayer-plugin-ads";
245+
$style.textContent = style;
246+
document.head.appendChild($style);
247+
}
248+
}
249+
export {
250+
artplayerPluginAds as default
251+
};

docs/compiled/artplayer-plugin-ambilight.mjs

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,101 @@
44
* (c) 2017-2026 Harvey Zhao
55
* Released under the MIT License.
66
*/
7-
function t(t={}){return e=>{const{$video:n}=e.template,{createElement:r,addClass:o,setStyles:i}=e.constructor.utils,{blur:a="50px",opacity:l=.5,frequency:s=10,duration:c=.3}=t,u=r("div"),d=((f=u).innerHTML=Array.from({length:9}).fill("<div></div>").join(""),Array.from(f.children));var f,m,g,p;!function(t,e){o(t,"artplayer-plugin-ambilight"),e.parentNode.insertBefore(t,e),i(t,{position:"absolute",top:0,left:0,zIndex:9,inset:0,display:"grid",gridTemplateColumns:"1fr 1fr 1fr",gridTemplateRows:"1fr 1fr 1fr"})}(u,n),m=a,g=l,p=c,d.forEach(t=>{i(t,{opacity:g,filter:`blur(${m})`,transition:`background-color ${p}s ease`})});const y=function(t,n,o){const i=r("canvas"),a=i.getContext("2d");i.width=3,i.height=3;let l=0;return function r(){const i=performance.now();if(i-l<1e3/o||!e.playing)return void(h=requestAnimationFrame(r));l=i;const s=t.videoWidth/3,c=t.videoHeight/3,u=[[0,0],[s,0],[2*s,0],[0,c],[s,c],[2*s,c],[0,2*c],[s,2*c],[2*s,2*c]].map(([e,n])=>function(e,n,r,o){a.drawImage(t,e,n,r,o,0,0,1,1);const[i,l,s]=a.getImageData(0,0,1,1).data;return`rgb(${i}, ${l}, ${s})`}(e,n,s,c));n.forEach((t,e)=>{t.style.backgroundColor=u[e]}),h=requestAnimationFrame(r)}}(n,d,s);let h=null;function v(){h||y()}function b(){h&&(cancelAnimationFrame(h),h=null)}return e.on("ready",v),e.on("destroy",b),{name:"artplayerPluginAmbilight",start:v,stop:b}}}export{t as default};
7+
function artplayerPluginAmbilight(option = {}) {
8+
return (art) => {
9+
const { $video } = art.template;
10+
const { createElement, addClass, setStyles } = art.constructor.utils;
11+
const { blur = "50px", opacity = 0.5, frequency = 10, duration = 0.3 } = option;
12+
const $ambilight = createElement("div");
13+
const gridItems = createGridItems($ambilight);
14+
setupAmbilight($ambilight, $video);
15+
setupGridItems(gridItems, blur, opacity, duration);
16+
const updateColors = createColorUpdater($video, gridItems, frequency);
17+
let animationFrameId = null;
18+
function start() {
19+
if (!animationFrameId) {
20+
updateColors();
21+
}
22+
}
23+
function stop() {
24+
if (animationFrameId) {
25+
cancelAnimationFrame(animationFrameId);
26+
animationFrameId = null;
27+
}
28+
}
29+
art.on("ready", start);
30+
art.on("destroy", stop);
31+
return {
32+
name: "artplayerPluginAmbilight",
33+
start,
34+
stop
35+
};
36+
function createGridItems($ambilight2) {
37+
$ambilight2.innerHTML = Array.from({ length: 9 }).fill("<div></div>").join("");
38+
return Array.from($ambilight2.children);
39+
}
40+
function setupAmbilight($ambilight2, $video2) {
41+
addClass($ambilight2, "artplayer-plugin-ambilight");
42+
$video2.parentNode.insertBefore($ambilight2, $video2);
43+
setStyles($ambilight2, {
44+
position: "absolute",
45+
top: 0,
46+
left: 0,
47+
zIndex: 9,
48+
inset: 0,
49+
display: "grid",
50+
gridTemplateColumns: "1fr 1fr 1fr",
51+
gridTemplateRows: "1fr 1fr 1fr"
52+
});
53+
}
54+
function setupGridItems(gridItems2, blur2, opacity2, duration2) {
55+
gridItems2.forEach(($item) => {
56+
setStyles($item, {
57+
opacity: opacity2,
58+
filter: `blur(${blur2})`,
59+
transition: `background-color ${duration2}s ease`
60+
});
61+
});
62+
}
63+
function createColorUpdater($video2, gridItems2, frequency2) {
64+
const canvas = createElement("canvas");
65+
const ctx = canvas.getContext("2d");
66+
canvas.width = 3;
67+
canvas.height = 3;
68+
function getAverageColor(x, y, w, h) {
69+
ctx.drawImage($video2, x, y, w, h, 0, 0, 1, 1);
70+
const [r, g, b] = ctx.getImageData(0, 0, 1, 1).data;
71+
return `rgb(${r}, ${g}, ${b})`;
72+
}
73+
let lastUpdateTime = 0;
74+
return function updateColors2() {
75+
const now = performance.now();
76+
if (now - lastUpdateTime < 1e3 / frequency2 || !art.playing) {
77+
animationFrameId = requestAnimationFrame(updateColors2);
78+
return;
79+
}
80+
lastUpdateTime = now;
81+
const w = $video2.videoWidth / 3;
82+
const h = $video2.videoHeight / 3;
83+
const colors = [
84+
[0, 0],
85+
[w, 0],
86+
[2 * w, 0],
87+
[0, h],
88+
[w, h],
89+
[2 * w, h],
90+
[0, 2 * h],
91+
[w, 2 * h],
92+
[2 * w, 2 * h]
93+
].map(([x, y]) => getAverageColor(x, y, w, h));
94+
gridItems2.forEach(($item, index) => {
95+
$item.style.backgroundColor = colors[index];
96+
});
97+
animationFrameId = requestAnimationFrame(updateColors2);
98+
};
99+
}
100+
};
101+
}
102+
export {
103+
artplayerPluginAmbilight as default
104+
};

0 commit comments

Comments
 (0)