// 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 song into the player when there is a song loaded in local files there and unload if there is not. and obey chromes content security policy. keep all my margins and sizing.
Certainly! Below are the completed updated files for your "Sync Waves" Chrome extension. These updates address the following:
Fixed Errors: Resolved the Uncaught TypeError by removing references to nonexistent buttons (load-audio and load-video).
Enhanced Audio Functionality: Automatically loads the selected local audio file into the audio player and unloads it if no file is selected.
Consistent Video Functionality: Added the video player and similar loading/unloading behavior for video files.
Content Security Policy Compliance: Ensured that all scripts are external and no inline scripts are used.
Maintained Styling: Preserved all existing margins and sizing as per your original styles.css.
Removed Load Buttons: Eliminated references to load-audio and load-video buttons since loading is now handled automatically when a file is selected.
Added Video Player: Included the <video id="video-player" controls></video> element to match the script's expectations.
Organized Media Containers: Wrapped audio and video players in .media-container divs for consistent styling.
2. 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');
// Audio File Input Listener
audioFileInput.addEventListener('change', () => {
const file = audioFileInput.files[0];
const startTime = parseInt(audioStartTimeInput.value, 10) || 0;
if (file) {
const reader = new FileReader();
reader.onload = function(e) {
audioPlayer.src = e.target.result;
audioPlayer.load();
audioPlayer.style.display = 'block';
audioPlayer.addEventListener('canplay', () => {
audioPlayer.currentTime = startTime;
}, { once: true });
};
reader.readAsDataURL(file);
} else {
// Unload the audio player if no file is selected
audioPlayer.pause();
audioPlayer.src = '';
audioPlayer.style.display = 'none';
}
});
// Video File Input Listener
videoFileInput.addEventListener('change', () => {
const file = videoFileInput.files[0];
const startTime = parseInt(videoStartTimeInput.value, 10) || 0;
if (file) {
const reader = new FileReader();
reader.onload = function(e) {
videoPlayer.src = e.target.result;
videoPlayer.load();
videoPlayer.style.display = 'block';
videoPlayer.addEventListener('canplay', () => {
videoPlayer.currentTime = startTime;
}, { once: true });
};
reader.readAsDataURL(file);
} else {
// Unload the video player if no file is selected
videoPlayer.pause();
videoPlayer.src = '';
videoPlayer.style.display = 'none';
}
});
// Playback Control Functionality
playButton.addEventListener('click', () => {
if (audioPlayer.src) {
audioPlayer.play();
}
if (videoPlayer.src) {
videoPlayer.play();
}
});
pauseButton.addEventListener('click', () => {
if (audioPlayer.src) {
audioPlayer.pause();
}
if (videoPlayer.src) {
videoPlayer.pause();
}
});
resumeButton.addEventListener('click', () => {
if (audioPlayer.src) {
audioPlayer.play();
}
if (videoPlayer.src) {
videoPlayer.play();
}
});
// Optional: Hide media players initially
audioPlayer.style.display = 'none';
videoPlayer.style.display = 'none';
});
Changes Made:
Removed Load Button References: Eliminated loadAudioButton and loadVideoButton since loading is handled via file input changes.
Added File Input Event Listeners: Implemented change event listeners on audio-file and video-file inputs to load or unload media automatically.
Controlled Display of Media Players: Showed the audio/video player when a file is loaded and hid it when no file is selected.
Error Prevention: Ensured that media actions (play, pause, resume) only occur if a source is set, preventing potential errors.
Initial State: Media players are hidden initially and only displayed when a file is loaded.
3. styles.css
No changes are needed for styles.css as per your requirements to maintain margins and sizing. However, to ensure that the media players are hidden when not in use, you might want to ensure that the styles accommodate the display: none; property set in script.js. If necessary, you can adjust styling here.
No changes are necessary for background.js as it properly listens for the extension installation and icon click.
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") });
});
5. manifest.json
No changes are necessary for manifest.json unless you want to update permissions or other metadata. Ensure that all paths and permissions are correctly set.
Content Security Policy (CSP): Since all scripts are loaded externally and no inline scripts are used, the current CSP should suffice. Ensure that you do not introduce any inline JavaScript or styles that might violate the CSP.
Error Handling: The updated script.js includes checks to ensure that media actions are only performed if a source is set, preventing runtime errors.
User Experience: Media players are hidden by default and only appear when a file is loaded, providing a cleaner interface.
Feel free to integrate these updated files into your extension. Test thoroughly to ensure all functionalities work as expected.