Skip to content

Commit b87d0b9

Browse files
committed
change behaviour to only fetch audio sources when played
1 parent bde6408 commit b87d0b9

File tree

1 file changed

+30
-26
lines changed

1 file changed

+30
-26
lines changed

script.user.js

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// ==UserScript==
22
// @name JPDB Userscript (6a67)
33
// @namespace http://tampermonkey.net/
4-
// @version 0.1.159
4+
// @version 0.1.160
55
// @description Script for JPDB that adds some styling and functionality
66
// @match *://jpdb.io/*
77
// @grant GM_addStyle
@@ -2524,7 +2524,11 @@
25242524
sectionsHTML += `
25252525
<div style="margin-left: ${extraIndent};"${hiddenClass}>
25262526
<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+
}
25282532
<textarea id="${setting.getName()}" name="${setting.getName()}" style="width: 100%; height: 10rem; margin-top: 0.5rem;" spellcheck="false">${setting()}</textarea>
25292533
</div>
25302534
`;
@@ -3689,11 +3693,17 @@
36893693
uploadForm.insertAdjacentElement('afterend', div);
36903694

36913695
// Helper function to fetch blob and create URL
3696+
const audioCache = new Map();
36923697
async function fetchAudioBlob(url) {
3698+
if (audioCache.has(url)) {
3699+
return audioCache.get(url);
3700+
}
36933701
console.log('Fetching audio blob from:', url);
36943702
const response = await httpRequest(url, -1, false, false, false, false, 'blob');
36953703
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 };
36973707
}
36983708

36993709
// Helper function to add audio elements
@@ -3707,12 +3717,8 @@
37073717
if (resultBox.textContent === 'Loading extra audio sources...') {
37083718
resultBox.textContent = '';
37093719
}
3710-
if (div.textContent === 'Loading extra audio sources...') {
3711-
div.textContent = '';
3712-
}
37133720
for (const source of sources) {
37143721
try {
3715-
const blobUrl = await fetchAudioBlob(source.url);
37163722
successCount++;
37173723
const container = document.createElement('div');
37183724
container.style.cssText = `
@@ -3722,21 +3728,24 @@
37223728
padding: 5px;
37233729
`;
37243730

3725-
// Add source name
37263731
const sourceName = document.createElement('span');
37273732
sourceName.textContent = source.name || 'Audio';
37283733
sourceName.style.marginRight = '10px';
37293734
container.appendChild(sourceName);
37303735

37313736
const audio = document.createElement('audio');
3732-
audio.src = blobUrl;
3733-
37343737
const playButton = document.createElement('a');
37353738
playButton.classList.add('icon-link');
37363739
playButton.innerHTML = '<i class="ti ti-volume"></i>';
37373740
playButton.style.cursor = 'pointer';
3738-
playButton.onclick = (e) => {
3741+
3742+
let audioData = null;
3743+
playButton.onclick = async (e) => {
37393744
e.preventDefault();
3745+
if (!audioData) {
3746+
audioData = await fetchAudioBlob(source.url);
3747+
audio.src = audioData.blobUrl;
3748+
}
37403749
if (audio.paused) {
37413750
audio.play();
37423751
} else {
@@ -3745,7 +3754,6 @@
37453754
}
37463755
};
37473756

3748-
// Add plus button
37493757
const plusButton = document.createElement('a');
37503758
plusButton.classList.add('icon-link');
37513759
plusButton.innerHTML = '+';
@@ -3755,22 +3763,18 @@
37553763

37563764
plusButton.onclick = async (e) => {
37573765
e.preventDefault();
3758-
3766+
if (!audioData) {
3767+
audioData = await fetchAudioBlob(source.url);
3768+
audio.src = audioData.blobUrl;
3769+
}
37593770
audio.play();
37603771

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
37663772
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 });
37683774

3769-
// Create DataTransfer and add file
37703775
const dataTransfer = new DataTransfer();
37713776
dataTransfer.items.add(file);
37723777

3773-
// Set input files and trigger change
37743778
fileInput.files = dataTransfer.files;
37753779
fileInput.dispatchEvent(new Event('change', { bubbles: true }));
37763780
};
@@ -3780,13 +3784,13 @@
37803784
container.appendChild(plusButton);
37813785
div.appendChild(container);
37823786
} 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);
37883788
}
37893789
}
3790+
if (successCount === 0) {
3791+
resultBox.textContent = 'Failed to load any audio sources.';
3792+
resultBox.style.color = '#ff4444';
3793+
}
37903794
}
37913795
}
37923796

0 commit comments

Comments
 (0)