第1行: |
第1行: |
| /* 这里的任何JavaScript将为所有用户在每次页面载入时加载。 */ | | /* 这里的任何JavaScript将为所有用户在每次页面载入时加载。 */ |
− |
| |
− | mw.loader.load("https://voicewiki.cn/w/extrafiles/voicewiki-audio.js?action=raw&ctype=text/javascript&rand="+Math.random(), "text/javascript");
| |
| | | |
| /* 生成b站播放窗口 */ | | /* 生成b站播放窗口 */ |
第195行: |
第193行: |
| audio.append(button); | | audio.append(button); |
| }; | | }; |
| + | })(); |
| + | |
| + | |
| + | (function () { |
| + | var audios = document.getElementsByClassName('audio-player'); |
| + | var notValidAudio = /wpDestFile/; |
| + | var currentPlaying = null; |
| + | |
| + | function playAudio() { |
| + | for (var i = 0; i < audios.length; i++) { |
| + | var audio = audios[i]; |
| + | var linkEl = audio.firstElementChild; |
| + | |
| + | if (!linkEl) { |
| + | continue; |
| + | } |
| + | |
| + | var link = linkEl.href; |
| + | |
| + | if (notValidAudio.test(link)) { |
| + | continue; |
| + | } |
| + | |
| + | audio.innerHTML = ''; |
| + | audio.classList.add('has-audio-element'); |
| + | |
| + | var a = document.createElement('audio'); |
| + | |
| + | a.src = link; |
| + | a.preload = 'none'; |
| + | |
| + | a.onended = function () { |
| + | currentPlaying = null; |
| + | this.parentNode.classList.remove('audio-player-playing'); |
| + | }; |
| + | |
| + | var button = document.createElement('button'); |
| + | |
| + | button.classList.add('audio-player-button'); |
| + | button.append(a); |
| + | button.onclick = function () { |
| + | var a = this.firstElementChild; |
| + | |
| + | if (currentPlaying != null && currentPlaying != a) { |
| + | currentPlaying.pause(); |
| + | currentPlaying.currentTime = 0; |
| + | currentPlaying.parentNode.classList.remove('audio-player-playing'); |
| + | } |
| + | |
| + | if (a.paused) { |
| + | a.play(); |
| + | currentPlaying = a; |
| + | this.classList.add('audio-player-playing'); |
| + | this.classList.remove('audio-player-paused'); |
| + | } else { |
| + | a.pause(); |
| + | this.classList.add('audio-player-paused'); |
| + | this.classList.remove('audio-player-playing'); |
| + | } |
| + | }; |
| + | |
| + | audio.append(button); |
| + | } |
| + | |
| + | // 设置下一次执行的随机延迟,这里设定为 5 到 10 秒之间的随机值 |
| + | var nextDelay = Math.floor(Math.random() * (10000 - 5000 + 1)) + 5000; |
| + | setTimeout(playAudio, nextDelay); |
| + | } |
| + | |
| + | // 第一次执行 |
| + | playAudio(); |
| })(); | | })(); |