|
1 | 1 | // ==UserScript== |
2 | 2 | // @name JPDB Userscript (6a67) |
3 | 3 | // @namespace http://tampermonkey.net/ |
4 | | -// @version 0.1.159 |
| 4 | +// @version 0.1.160 |
5 | 5 | // @description Script for JPDB that adds some styling and functionality |
6 | 6 | // @match *://jpdb.io/* |
7 | 7 | // @grant GM_addStyle |
|
2524 | 2524 | sectionsHTML += ` |
2525 | 2525 | <div style="margin-left: ${extraIndent};"${hiddenClass}> |
2526 | 2526 | <label for="${setting.getName()}">${setting.getShortDescription()}</label> |
2527 | | - ${setting.getLongDescription() ? `<p style="opacity: 0.8; margin-top: 0.5rem">\n${setting.getLongDescription()}\n</p>` : ''} |
| 2527 | + ${ |
| 2528 | + setting.getLongDescription() |
| 2529 | + ? `<p style="opacity: 0.8; margin-top: 0.5rem">\n${setting.getLongDescription()}\n</p>` |
| 2530 | + : '' |
| 2531 | + } |
2528 | 2532 | <textarea id="${setting.getName()}" name="${setting.getName()}" style="width: 100%; height: 10rem; margin-top: 0.5rem;" spellcheck="false">${setting()}</textarea> |
2529 | 2533 | </div> |
2530 | 2534 | `; |
|
3689 | 3693 | uploadForm.insertAdjacentElement('afterend', div); |
3690 | 3694 |
|
3691 | 3695 | // Helper function to fetch blob and create URL |
| 3696 | + const audioCache = new Map(); |
3692 | 3697 | async function fetchAudioBlob(url) { |
| 3698 | + if (audioCache.has(url)) { |
| 3699 | + return audioCache.get(url); |
| 3700 | + } |
3693 | 3701 | console.log('Fetching audio blob from:', url); |
3694 | 3702 | const response = await httpRequest(url, -1, false, false, false, false, 'blob'); |
3695 | 3703 | const blob = await response.response; |
3696 | | - return URL.createObjectURL(blob); |
| 3704 | + const blobUrl = URL.createObjectURL(blob); |
| 3705 | + audioCache.set(url, { blob, blobUrl }); |
| 3706 | + return { blob, blobUrl }; |
3697 | 3707 | } |
3698 | 3708 |
|
3699 | 3709 | // Helper function to add audio elements |
|
3707 | 3717 | if (resultBox.textContent === 'Loading extra audio sources...') { |
3708 | 3718 | resultBox.textContent = ''; |
3709 | 3719 | } |
3710 | | - if (div.textContent === 'Loading extra audio sources...') { |
3711 | | - div.textContent = ''; |
3712 | | - } |
3713 | 3720 | for (const source of sources) { |
3714 | 3721 | try { |
3715 | | - const blobUrl = await fetchAudioBlob(source.url); |
3716 | 3722 | successCount++; |
3717 | 3723 | const container = document.createElement('div'); |
3718 | 3724 | container.style.cssText = ` |
|
3722 | 3728 | padding: 5px; |
3723 | 3729 | `; |
3724 | 3730 |
|
3725 | | - // Add source name |
3726 | 3731 | const sourceName = document.createElement('span'); |
3727 | 3732 | sourceName.textContent = source.name || 'Audio'; |
3728 | 3733 | sourceName.style.marginRight = '10px'; |
3729 | 3734 | container.appendChild(sourceName); |
3730 | 3735 |
|
3731 | 3736 | const audio = document.createElement('audio'); |
3732 | | - audio.src = blobUrl; |
3733 | | - |
3734 | 3737 | const playButton = document.createElement('a'); |
3735 | 3738 | playButton.classList.add('icon-link'); |
3736 | 3739 | playButton.innerHTML = '<i class="ti ti-volume"></i>'; |
3737 | 3740 | playButton.style.cursor = 'pointer'; |
3738 | | - playButton.onclick = (e) => { |
| 3741 | + |
| 3742 | + let audioData = null; |
| 3743 | + playButton.onclick = async (e) => { |
3739 | 3744 | e.preventDefault(); |
| 3745 | + if (!audioData) { |
| 3746 | + audioData = await fetchAudioBlob(source.url); |
| 3747 | + audio.src = audioData.blobUrl; |
| 3748 | + } |
3740 | 3749 | if (audio.paused) { |
3741 | 3750 | audio.play(); |
3742 | 3751 | } else { |
|
3745 | 3754 | } |
3746 | 3755 | }; |
3747 | 3756 |
|
3748 | | - // Add plus button |
3749 | 3757 | const plusButton = document.createElement('a'); |
3750 | 3758 | plusButton.classList.add('icon-link'); |
3751 | 3759 | plusButton.innerHTML = '+'; |
|
3755 | 3763 |
|
3756 | 3764 | plusButton.onclick = async (e) => { |
3757 | 3765 | e.preventDefault(); |
3758 | | - |
| 3766 | + if (!audioData) { |
| 3767 | + audioData = await fetchAudioBlob(source.url); |
| 3768 | + audio.src = audioData.blobUrl; |
| 3769 | + } |
3759 | 3770 | audio.play(); |
3760 | 3771 |
|
3761 | | - // Fetch the blob from blob URL |
3762 | | - const response = await fetch(blobUrl); |
3763 | | - const blob = await response.blob(); |
3764 | | - |
3765 | | - // Create file from blob |
3766 | 3772 | const fileName = source.url.split('/').pop() || 'audio.mp3'; |
3767 | | - const file = new File([blob], fileName, { type: blob.type }); |
| 3773 | + const file = new File([audioData.blob], fileName, { type: audioData.blob.type }); |
3768 | 3774 |
|
3769 | | - // Create DataTransfer and add file |
3770 | 3775 | const dataTransfer = new DataTransfer(); |
3771 | 3776 | dataTransfer.items.add(file); |
3772 | 3777 |
|
3773 | | - // Set input files and trigger change |
3774 | 3778 | fileInput.files = dataTransfer.files; |
3775 | 3779 | fileInput.dispatchEvent(new Event('change', { bubbles: true })); |
3776 | 3780 | }; |
|
3780 | 3784 | container.appendChild(plusButton); |
3781 | 3785 | div.appendChild(container); |
3782 | 3786 | } catch (error) { |
3783 | | - console.error(`Error fetching audio blob for ${source}:`, error); |
3784 | | - } |
3785 | | - if (successCount === 0) { |
3786 | | - resultBox.textContent = 'Failed to load any audio sources.'; |
3787 | | - resultBox.style.color = '#ff4444'; |
| 3787 | + console.error(`Error setting up audio source for ${source.url}:`, error); |
3788 | 3788 | } |
3789 | 3789 | } |
| 3790 | + if (successCount === 0) { |
| 3791 | + resultBox.textContent = 'Failed to load any audio sources.'; |
| 3792 | + resultBox.style.color = '#ff4444'; |
| 3793 | + } |
3790 | 3794 | } |
3791 | 3795 | } |
3792 | 3796 |
|
|
0 commit comments