|
769 | 769 |
|
770 | 770 | // run the change handler for the first time
|
771 | 771 | stateEls.minified.dispatchEvent(newEvent('change'));
|
| 772 | + |
| 773 | + // Setup the download / copy log buttons |
| 774 | + const downloadLogsButton = document.getElementById('download-logs'); |
| 775 | + const copyLogsButton = document.getElementById('copy-logs'); |
| 776 | + |
| 777 | + /** |
| 778 | + * Window location and history joined with line breaks, stringifying any objects |
| 779 | + * |
| 780 | + * @return {string} Stringified history |
| 781 | + */ |
| 782 | + const stringifiedLogHistory = () => { |
| 783 | + const player = document.querySelector('video-js').player; |
| 784 | + const logs = [].concat(player.log.history()); |
| 785 | + const withVhs = !!player.tech(true).vhs; |
| 786 | + |
| 787 | + return [ |
| 788 | + window.location.href, |
| 789 | + window.navigator.userAgent, |
| 790 | + `Video.js ${window.videojs.VERSION}`, |
| 791 | + `Using VHS: ${withVhs}`, |
| 792 | + withVhs ? JSON.stringify(player.tech(true).vhs.version()) : '' |
| 793 | + ].concat(logs.map(entryArgs => { |
| 794 | + return entryArgs.map(item => { |
| 795 | + return typeof item === 'object' ? JSON.stringify(item) : item; |
| 796 | + }); |
| 797 | + })).join('\n'); |
| 798 | + }; |
| 799 | + |
| 800 | + /** |
| 801 | + * Turn a bootstrap button class on briefly then revert to btn-outline-ptimary |
| 802 | + * |
| 803 | + * @param {HTMLElement} el Element to add class to |
| 804 | + * @param {string} stateClass Bootstrap button class suffix |
| 805 | + */ |
| 806 | + const doneFeedback = (el, stateClass) => { |
| 807 | + el.classList.add(`btn-${stateClass}`); |
| 808 | + el.classList.remove('btn-outline-secondary'); |
| 809 | + |
| 810 | + window.setTimeout(() => { |
| 811 | + el.classList.add('btn-outline-secondary'); |
| 812 | + el.classList.remove(`btn-${stateClass}`); |
| 813 | + }, 1500); |
| 814 | + }; |
| 815 | + |
| 816 | + downloadLogsButton.addEventListener('click', function() { |
| 817 | + const logHistory = stringifiedLogHistory(); |
| 818 | + const a = document.createElement('a'); |
| 819 | + const href = URL.createObjectURL(new Blob([logHistory], { type: 'text/plain' })); |
| 820 | + |
| 821 | + a.setAttribute('download', 'vhs-player-logs.txt'); |
| 822 | + a.setAttribute('target', '_blank'); |
| 823 | + a.href = href; |
| 824 | + a.click(); |
| 825 | + a.remove(); |
| 826 | + URL.revokeObjectURL(href); |
| 827 | + doneFeedback(downloadLogsButton, 'success'); |
| 828 | + }); |
| 829 | + |
| 830 | + copyLogsButton.addEventListener('click', function() { |
| 831 | + const logHistory = stringifiedLogHistory(); |
| 832 | + |
| 833 | + window.navigator.clipboard.writeText(logHistory).then(z => { |
| 834 | + doneFeedback(copyLogsButton, 'success'); |
| 835 | + }).catch(e => { |
| 836 | + doneFeedback(copyLogsButton, 'danger'); |
| 837 | + console.log('Copy failed', e); |
| 838 | + }); |
| 839 | + |
| 840 | + }); |
772 | 841 | };
|
| 842 | + |
773 | 843 | }(window));
|
0 commit comments