// 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
}
});
i only want the completed updated files. the extension is opening a new copy of itself every time i open a new tab i only want it to open a new tab when i first press the icon button.
Certainly! To resolve the issue where the extension opens a new copy of itself every time a new tab is opened, you need to remove the chrome_url_overrides section from the manifest.json. This section is responsible for overriding the default new tab page with your extension's index.html, causing the behavior you're experiencing.
Below are the updated and completed files for your Chrome extension:
1. manifest.json
Changes Made:
Removed the chrome_url_overrides section to prevent the extension from overriding every new tab.
Verified that necessary permissions remain intact.
Removed the chrome_url_overrides section from manifest.json to prevent the extension from overriding every new tab.
Ensured that clicking the extension icon (chrome.action.onClicked) correctly opens index.html in a new tab without interfering with the default new tab behavior.
Final Notes
After making these changes, follow these steps to update your extension:
Update the Files:
Replace your existing manifest.json with the updated version provided above.
Ensure that background.js, index.html, script.js, and styles.css are correctly saved with their respective content.
Reload the Extension:
Go to chrome://extensions/ in your Chrome browser.
Enable "Developer mode" if it's not already enabled.
Click the "Reload" (↻) button on your extension to apply the changes.
Test the Extension:
Open a new tab to confirm that the extension no longer overrides it.
Click the extension icon to ensure that index.html opens correctly in a new tab.
This should resolve the issue of the extension opening a new copy of itself with every new tab and ensure that index.html only opens when you click the extension icon.
If you encounter any further issues or need additional functionality, feel free to ask!
Not yet included in a published release.
// script.js
document.addEventListener('DOMContentLoaded', () => {
// Audio Elements and Controls
const audioUrlInput = document.getElementById('audio-url');
const audioFileInput = document.getElementById('audio-file');
const audioStartTimeInput = document.getElementById('audio-start-time');
const loadAudioButton = document.getElementById('load-audio');
// Video Elements and Controls
const videoUrlInput = document.getElementById('video-url');
const videoFileInput = document.getElementById('video-file');
const videoStartTimeInput = document.getElementById('video-start-time');
const loadVideoButton = document.getElementById('load-video');
// 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');
// Load Audio Functionality
loadAudioButton.addEventListener('click', () => {
const youtubeUrl = audioUrlInput.value.trim();
const file = audioFileInput.files[0];
const startTime = parseInt(audioStartTimeInput.value, 10) || 0;
if (youtubeUrl) {
// Load YouTube Audio via YouTube API
loadYouTubeAudio(youtubeUrl, audioPlayer, startTime);
} else if (file) {
const reader = new FileReader();
reader.onload = function(e) {
audioPlayer.src = e.target.result;
audioPlayer.load();
audioPlayer.addEventListener('canplay', () => {
audioPlayer.currentTime = startTime;
}, { once: true });
};
reader.readAsDataURL(file);
} else {
alert('Please provide a YouTube URL or select an audio file.');
}
});
// Load Video Functionality
loadVideoButton.addEventListener('click', () => {
const youtubeUrl = videoUrlInput.value.trim();
const file = videoFileInput.files[0];
const startTime = parseInt(videoStartTimeInput.value, 10) || 0;
if (youtubeUrl) {
// Load YouTube Video via YouTube Embed
const videoID = extractYouTubeID(youtubeUrl);
if (videoID) {
videoPlayer.src = `https://www.youtube.com/embed/${videoID}?autoplay=1&controls=1&enablejsapi=1`;
videoPlayer.addEventListener('loadeddata', () => {
setTimeout(() => {
// Attempt to set start time using YouTube API
// Note: Full integration requires additional API setup
videoPlayer.contentWindow.postMessage(JSON.stringify({
event: 'command',
func: 'seekTo',
args: [startTime, true]
}), '*');
}, 500);
}, { once: true });
} else {
alert('Invalid YouTube URL.');
}
} else if (file) {
const reader = new FileReader();
reader.onload = function(e) {
videoPlayer.src = e.target.result;
videoPlayer.load();
videoPlayer.addEventListener('canplay', () => {
videoPlayer.currentTime = startTime;
}, { once: true });
};
reader.readAsDataURL(file);
} else {
alert('Please provide a YouTube URL or select a video file.');
}
});
// Playback Control Functionality
playButton.addEventListener('click', () => {
audioPlayer.play();
videoPlayer.play();
});
pauseButton.addEventListener('click', () => {
audioPlayer.pause();
videoPlayer.pause();
});
resumeButton.addEventListener('click', () => {
audioPlayer.play();
videoPlayer.play();
});
// 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
}
});