|
4 | 4 | * (c) 2017-2026 Harvey Zhao |
5 | 5 | * Released under the MIT License. |
6 | 6 | */ |
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 | +}; |
0 commit comments