Here are the completed and updated files for your Chrome extension "Sync Waves":
manifest.json
{
"manifest_version": 3,
"name": "Sync Waves",
"version": "1.0",
"description": "Synchronize audio and video files with YouTube URLs.",
"permissions": [
"tabs",
"storage"
],
"chrome_url_overrides": {
"newtab": "index.html"
},
"icons": {
"16": "icons/icon16.png",
"48": "icons/icon48.png",
"128": "icons/icon128.png"
}
}
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Sync Waves</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>Sync Waves</h1>
<div class="container">
<div class="section" id="audio-section">
<h2>Audio</h2>
<label for="audio-youtube-url">YouTube URL:</label>
<input type="text" id="audio-youtube-url" placeholder="Enter YouTube URL">
<label for="audio-file">Local Audio File:</label>
<input type="file" id="audio-file" accept="audio/*">
<label for="audio-start-time">Start Time (seconds):</label>
<input type="number" id="audio-start-time" min="0" value="0">
<button class="load-button" id="load-audio">Load</button>
<div id="audio-player-container" class="player-container">
<audio id="audio-player" controls></audio>
</div>
</div>
<div class="section" id="video-section">
<h2>Video</h2>
<label for="video-youtube-url">YouTube URL:</label>
<input type="text" id="video-youtube-url" placeholder="Enter YouTube URL">
<label for="video-file">Local Video File:</label>
<input type="file" id="video-file" accept="video/*">
<label for="video-start-time">Start Time (seconds):</label>
<input type="number" id="video-start-time" min="0" value="0">
<button class="load-button" id="load-video">Load</button>
<div id="video-player-container" class="player-container">
<video id="video-player" controls width="100%"></video>
</div>
</div>
<div class="controls">
<button id="play-btn">Play</button>
<button id="pause-btn">Pause</button>
<button id="resume-btn">Resume</button>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
styles.css
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 20px;
box-sizing: border-box;
background-color: #f0f0f0;
}
h1 {
text-align: center;
}
.container {
width: 90%;
margin: 0 auto;
}
.section {
background-color: #ffffff;
padding: 20px;
margin-bottom: 20px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
.section h2 {
margin-top: 0;
}
label {
display: block;
margin-top: 10px;
}
input[type="text"],
input[type="number"],
input[type="file"] {
width: 100%;
padding: 8px;
margin-top: 5px;
box-sizing: border-box;
}
.load-button {
margin-top: 15px;
padding: 10px 20px;
background-color: #4285f4;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
.load-button:hover {
background-color: #357ae8;
}
.controls {
text-align: center;
margin-top: 20px;
}
.controls button {
padding: 10px 20px;
margin: 0 10px;
background-color: #34a853;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
.controls button:hover {
background-color: #2c8e46;
}
.player-container {
margin-top: 20px;
}
@media (max-width: 768px) {
.controls button {
display: block;
width: 100%;
margin: 10px 0;
}
}
script.js
document.addEventListener('DOMContentLoaded', () => {
const loadAudioButton = document.getElementById('load-audio');
const loadVideoButton = document.getElementById('load-video');
const playButton = document.getElementById('play-btn');
const pauseButton = document.getElementById('pause-btn');
const resumeButton = document.getElementById('resume-btn');
const audioPlayer = document.getElementById('audio-player');
const videoPlayer = document.getElementById('video-player');
let audioStartTime = 0;
let videoStartTime = 0;
loadAudioButton.addEventListener('click', () => {
const youtubeURL = document.getElementById('audio-youtube-url').value;
const localFile = document.getElementById('audio-file').files[0];
audioStartTime = parseInt(document.getElementById('audio-start-time').value) || 0;
if (youtubeURL) {
loadYouTubeAudio(youtubeURL, audioStartTime);
} else if (localFile) {
const url = URL.createObjectURL(localFile);
audioPlayer.src = url;
audioPlayer.load();
audioPlayer.play().then(() => {
setTimeout(() => {
audioPlayer.pause();
audioPlayer.currentTime = audioStartTime;
}, 125); // 1/8 second = 125ms
}).catch(error => {
console.error('Error playing audio:', error);
});
}
});
loadVideoButton.addEventListener('click', () => {
const youtubeURL = document.getElementById('video-youtube-url').value;
const localFile = document.getElementById('video-file').files[0];
videoStartTime = parseInt(document.getElementById('video-start-time').value) || 0;
if (youtubeURL) {
loadYouTubeVideo(youtubeURL, videoStartTime);
} else if (localFile) {
const url = URL.createObjectURL(localFile);
videoPlayer.src = url;
videoPlayer.load();
videoPlayer.play().then(() => {
setTimeout(() => {
videoPlayer.pause();
videoPlayer.currentTime = videoStartTime;
}, 125); // 1/8 second = 125ms
}).catch(error => {
console.error('Error playing video:', error);
});
}
});
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();
}
});
function loadYouTubeAudio(url, startTime) {
// Extract YouTube video ID
const videoId = extractYouTubeID(url);
if (!videoId) {
alert('Invalid YouTube URL for audio.');
return;
}
// Create a hidden video element to extract audio
const tempVideo = document.createElement('video');
tempVideo.src = `https://www.youtube.com/embed/${videoId}?enablejsapi=1&origin=${window.location.origin}`;
tempVideo.style.display = 'none';
document.body.appendChild(tempVideo);
tempVideo.addEventListener('loadeddata', () => {
audioPlayer.src = tempVideo.captureStream().getAudioTracks()[0];
audioPlayer.load();
audioPlayer.play().then(() => {
setTimeout(() => {
audioPlayer.pause();
audioPlayer.currentTime = startTime;
}, 125);
}).catch(error => {
console.error('Error playing YouTube audio:', error);
});
document.body.removeChild(tempVideo);
});
}
function loadYouTubeVideo(url, startTime) {
// Extract YouTube video ID
const videoId = extractYouTubeID(url);
if (!videoId) {
alert('Invalid YouTube URL for video.');
return;
}
videoPlayer.src = `https://www.youtube.com/embed/${videoId}?start=${startTime}&autoplay=1&controls=1`;
videoPlayer.load();
videoPlayer.play().then(() => {
setTimeout(() => {
videoPlayer.pause();
videoPlayer.currentTime = startTime;
}, 125);
}).catch(error => {
console.error('Error playing YouTube video:', error);
});
}
function extractYouTubeID(url) {
const regex = /(?:youtube\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|.*[?&]v=)|youtu\.be\/)([a-zA-Z0-9_-]{11})/;
const match = url.match(regex);
return match ? match[1] : null;
}
});
Folder Structure and Additional Notes
-
Icons: Create an icons folder in your extension directory and add icon files named icon16.png, icon48.png, and icon128.png. These icons represent your extension in different places within Chrome.
-
Loading YouTube Content: The provided script.js includes basic handling for YouTube URLs by embedding them. However, due to YouTube's restrictions and Content Security Policy, full functionality may require using the YouTube IFrame API and appropriate permissions. Ensure compliance with YouTube's terms of service.
-
Responsiveness: The styles.css file includes media queries to ensure that buttons stack vertically on smaller screens for better usability.
-
Permissions: The manifest.json includes permissions for tabs and storage, which may be required for more advanced functionalities. Adjust permissions based on your specific needs.
-
CSP Compliance: All JavaScript is included via external files, and there are no inline scripts or styles, ensuring compliance with Chrome's Content Security Policy.
-
Testing: After setting up all files and folders, load the unpacked extension in Chrome via chrome://extensions/ to test its functionality.
This completes the Chrome extension "Sync Waves" as per your specifications. Make sure to test each feature thoroughly and adjust the code as needed to fit any additional requirements or handle edge cases.