// Listen for the extension icon click
chrome.action.onClicked.addListener(() => {
// Open index.html in a new tab
chrome.tabs.create({ url: chrome.runtime.getURL("index.html") });
});
// Helper Function to Extract YouTube Video ID
function extractYouTubeID(url) {
const regex = /(?:https?://)?(?:www.)?(?:youtube.com/watch?v=|youtu.be/)([\w-]{11})/;
const match = url.match(regex);
return match ? match[1] : null;
}
// Placeholder Function for YouTube Audio Loading
function loadYouTubeAudio(url, player, startTime) {
alert('Loading audio from YouTube is not supported in this version.');
// Future implementation can use YouTube Data API and OAuth for advanced features
}
});
// Helper Function to Extract YouTube Video ID
function extractYouTubeID(url) {
const regex = /(?:https?://)?(?:www.)?(?:youtube.com/watch?v=|youtu.be/)([\w-]{11})/;
const match = url.match(regex);
return match ? match[1] : null;
}
// Placeholder Function for YouTube Audio Loading
function loadYouTubeAudio(url, player, startTime) {
alert('Loading audio from YouTube is not supported in this version.');
// Future implementation can use YouTube Data API and OAuth for advanced features
}
});
i only want the completed updated files. in the audio section can you load the player when there is a song loaded in local files there and unload if there is not.
Certainly! Below are the complete, updated files for your Sync Waves Chrome extension. These updates address the issues you've encountered and implement the requested functionality where the audio player is loaded only when a local audio file is selected and unloaded when no file is present.
Added Video Player: Included the <video id="video-player" controls class="hidden"></video> element within the Video section.
Removed Unused Elements: Any references to YouTube URLs or load buttons for URLs have been removed to prevent errors.
3. background.js
Updated background.js:
chrome.runtime.onInstalled.addListener(() => {
console.log('Sync Waves Extension Installed');
});
// Listen for the extension icon click
chrome.action.onClicked.addListener(() => {
// Open index.html in a new tab
chrome.tabs.create({ url: chrome.runtime.getURL("index.html") });
});
Notes:
No Changes Needed: The original background.js correctly handles the installation event and icon click to open the extension's popup.
4. script.js
Updated script.js:
document.addEventListener('DOMContentLoaded', () => {
// Audio Elements and Controls
const audioFileInput = document.getElementById('audio-file');
const audioStartTimeInput = document.getElementById('audio-start-time');
// Video Elements and Controls
const videoFileInput = document.getElementById('video-file');
const videoStartTimeInput = document.getElementById('video-start-time');
// Playback Control Buttons
const playButton = document.getElementById('play');
const pauseButton = document.getElementById('pause');
const resumeButton = document.getElementById('resume');
// Media Players
const audioPlayer = document.getElementById('audio-player');
const videoPlayer = document.getElementById('video-player');
// CSS Class to Toggle Visibility
const HIDDEN_CLASS = 'hidden';
// Initially hide audio and video players
audioPlayer.classList.add(HIDDEN_CLASS);
videoPlayer.classList.add(HIDDEN_CLASS);
// Function to Load Media File
const loadMedia = (fileInput, player, startTimeInput) => {
const file = fileInput.files[0];
if (file) {
const startTime = parseInt(startTimeInput.value, 10) || 0;
const reader = new FileReader();
reader.onload = function(e) {
player.src = e.target.result;
player.load();
player.classList.remove(HIDDEN_CLASS);
player.addEventListener('canplay', () => {
player.currentTime = startTime;
}, { once: true });
};
reader.readAsDataURL(file);
} else {
// No file selected, hide player
player.pause();
player.src = '';
player.classList.add(HIDDEN_CLASS);
}
};
// Load Audio File
audioFileInput.addEventListener('change', () => {
loadMedia(audioFileInput, audioPlayer, audioStartTimeInput);
});
// Load Video File
videoFileInput.addEventListener('change', () => {
loadMedia(videoFileInput, videoPlayer, videoStartTimeInput);
});
// Playback Control Functionality
playButton.addEventListener('click', () => {
if (!audioPlayer.classList.contains(HIDDEN_CLASS)) {
audioPlayer.play();
}
if (!videoPlayer.classList.contains(HIDDEN_CLASS)) {
videoPlayer.play();
}
});
pauseButton.addEventListener('click', () => {
if (!audioPlayer.classList.contains(HIDDEN_CLASS)) {
audioPlayer.pause();
}
if (!videoPlayer.classList.contains(HIDDEN_CLASS)) {
videoPlayer.pause();
}
});
resumeButton.addEventListener('click', () => {
if (!audioPlayer.classList.contains(HIDDEN_CLASS)) {
audioPlayer.play();
}
if (!videoPlayer.classList.contains(HIDDEN_CLASS)) {
videoPlayer.play();
}
});
});
Changes Made:
Removed Unused Elements: Eliminated references to audio-url, load-audio, video-url, and load-video since they're not present in the HTML.
Unified Media Loading Function: Created a reusable loadMedia function to handle both audio and video file loading, reducing code duplication.
Toggle Visibility: Added logic to show the media player only when a file is loaded and hide it otherwise.
Hidden Class: Added .hidden { display: none; } to control the visibility of media players.
Applied Hidden Class: Ensured that both <audio> and <video> elements have the hidden class initially to keep them hidden until a file is loaded.
6. Handling Errors
Issue Encountered:
You encountered the following error:
Uncaught TypeError: Cannot read properties of null (reading 'addEventListener')
Cause:
This error was due to the script.js attempting to access elements with IDs like audio-url and load-audio that do not exist in your index.html. By removing these references and aligning the JavaScript with the existing HTML structure, this error has been resolved.
7. Final Notes
Audio and Video Loading: The audio and video players will only become visible when a local file is selected. If the user deselects the file or selects a new one, the players will update accordingly.
Playback Controls: The Play, Pause, and Resume buttons will control both audio and video players simultaneously, but only if they are active (i.e., a file is loaded).
Future Enhancements: If you plan to add support for loading media via URLs or integrating with APIs like YouTube in future versions, you can reintroduce those functionalities with appropriate UI elements and JavaScript logic.
Feel free to integrate these updated files into your extension directory. Ensure that all file paths (like icons) are correctly set up in your project structure. If you encounter any further issues or have additional requests, don't hesitate to ask!