Last active
June 10, 2018 06:15
-
-
Save DenVdmj/ac3d1b914bc71f66bcc8ab3e475d952b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
console.info('flac player init start'); | |
const copy = (el, props) => { | |
for (let [key, val] of Object.entries(props)) { | |
el[key] = val; | |
} | |
}; | |
const audioElements = []; | |
const initElement = element => { | |
element.classList.add('audio_loaded'); | |
const audio = document.createElement('audio'); | |
const parent = element.parentNode; | |
audioElements.push(audio); | |
audio.setAttribute('src', element.getAttribute('href')); | |
audio.setAttribute('controls', 'controls'); | |
audio.setAttribute('title', element.innerText); | |
//copy(audio.style, { display: 'inline-block', height: '30px', width: 'calc(100% - 38px)' }); | |
//copy(element.style, { display: 'none' }); | |
copy(audio.style, { display: 'inline-block', marginRight: '30px', maxHeight: '30px' }); | |
copy(element.style, { display: 'inline-block', maxWidth: '100px', maxHeight: '30px', fontSize: '10px', textOverflow: 'ellipsis' }); | |
parent.querySelector(':scope .page_doc_description_row').style.display = 'none'; | |
parent.insertBefore(audio, element); | |
audio.addEventListener('play', event => { | |
for (let el of audioElements) { | |
if (el !== event.target) { | |
el.pause(); | |
} | |
} | |
}); | |
audio.addEventListener('ended', event => { | |
const index = audioElements.indexOf(event.target); | |
if (index < audioElements.length) { | |
audioElements[index].play(); | |
} | |
}); | |
audio.addEventListener('volumechange', event => { | |
for (let el of audioElements) { | |
el.volume = event.target.volume; | |
} | |
}); | |
chrome.runtime.sendMessage({ showAction: true }); | |
console.info('flac player init end'); | |
}; | |
const initPage = () => { | |
for (let target of document.querySelectorAll('a.page_doc_title:not(.audio_loaded)')) | |
if (target.innerText.match(/^.+.flac$/)) | |
initElement(target) | |
}; | |
document.addEventListener('DOMContentLoaded', initPage); | |
window.addEventListener('scroll', initPage); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment