Creating a Chrome extension to achieve the functionality you've described involves multiple components, including manifest files, HTML for the popup, JavaScript for handling interactions, and management of permissions. Below is a simplified version of how you can structure this extension. Due to space and complexity constraints, not all edge cases or error handling are included.
File Structure
my-chrome-extension/
│
├── manifest.json
├── popup.html
├── popup.js
└── background.js
Step-by-Step Implementation
- Manifest File (
manifest.json):
{
"manifest_version": 3,
"name": "Media Loader Extension",
"version": "1.0",
"permissions": [
"tabs",
"activeTab",
"scripting",
"storage"
],
"action": {
"default_popup": "popup.html",
"default_icon": "icon.png"
},
"background": {
"service_worker": "background.js"
},
"host_permissions": ["*://*.youtube.com/*"]
}
- Popup HTML (
popup.html):
<!DOCTYPE html>
<html>
<head>
<title>Media Loader</title>
</head>
<body>
<div>
<label for="music-url">Music URL/MP3: </label>
<input type="text" id="music-url">
<input type="file" id="music-file" accept=".mp3">
<input type="number" id="music-time" placeholder="Start time in seconds">
</div>
<div>
<label for="video-url">Video URL/MP4: </label>
<input type="text" id="video-url">
<input type="file" id="video-file" accept=".mp4">
<input type="number" id="video-time" placeholder="Start time in seconds">
</div>
<button id="load" disabled>Load</button>
<p id="status"></p>
<button id="master-play" disabled>Play</button>
<script src="popup.js"></script>
</body>
</html>
- Popup JavaScript (
popup.js):
document.addEventListener('DOMContentLoaded', function () {
const musicUrlInput = document.getElementById('music-url');
const videoUrlInput = document.getElementById('video-url');
const loadButton = document.getElementById('load');
const masterPlayButton = document.getElementById('master-play');
const statusText = document.getElementById('status');
let musicFile = null;
let videoFile = null;
let musicTabId = null;
let videoTabId = null;
document.getElementById('music-file').addEventListener('change', function(e){
musicFile = e.target.files[0];
checkInputs();
});
document.getElementById('video-file').addEventListener('change', function(e){
videoFile = e.target.files[0];
checkInputs();
});
function checkInputs() {
if ((musicUrlInput.value || musicFile) && (videoUrlInput.value || videoFile)) {
loadButton.disabled = false;
} else {
loadButton.disabled = true;
}
}
musicUrlInput.addEventListener('input', checkInputs);
videoUrlInput.addEventListener('input', checkInputs);
loadButton.addEventListener('click', async function() {
statusText.textContent = "Loading...";
const musicTime = document.getElementById('music-time').value || 0;
const videoTime = document.getElementById('video-time').value || 0;
// Create tabs
if (musicFile) {
// Loading and handling local file is complex and it might be best served by fetching via server or converting it to an accessible format
} else {
const musicTab = await chrome.tabs.create({ url: musicUrlInput.value, active: false });
musicTabId = musicTab.id;
}
if (videoFile) {
// Similar handling for local video file
} else {
const videoTab = await chrome.tabs.create({ url: videoUrlInput.value, active: false });
videoTabId = videoTab.id;
}
// Check if all resources are loaded, then enable play
// Note: Implement logic here for checking if ads are done loading, potentially by listening to certain events or timers.
statusText.textContent = "Ready to Play!";
masterPlayButton.disabled = false;
});
masterPlayButton.addEventListener('click', async function() {
await chrome.tabs.update(videoTabId, { active: true });
// Inject scripts in both tabs to control playback
if (musicTabId) {
chrome.scripting.executeScript({
target: { tabId: musicTabId },
func: (startTime) => {
// Assuming music is a YouTube video
document.querySelector('video').currentTime = startTime;
document.querySelector('video').play();
},
args: [parseInt(document.getElementById('music-time').value || 0)]
});
}
if (videoTabId) {
chrome.scripting.executeScript({
target: { tabId: videoTabId },
func: (startTime) => {
const video = document.querySelector('video');
video.currentTime = startTime;
video.play();
video.requestFullscreen();
},
args: [parseInt(document.getElementById('video-time').value || 0)]
});
}
});
});
- Background JS (
background.js):
This file will largely be left blank unless there is specific logic you need to handle in the background, as communication between tabs and potentially content scripts may be managed here with more complexity.
Important Notes:
- Handling Local Files: Chrome Extensions have limited capabilities when it comes to interacting directly with local files. You might need additional work in handling local file loading, potentially involving file readers or conversion of local files to accessible URLs.
- Ad Handling: The instruction for ensuring ads are done loading is non-trivial, as ads can vary greatly. This requires additional handling, possibly via watching network requests or tracking the time duration of ads.
- Security Policy: Avoid inline scripts by keeping JavaScript in separate files, as shown here. This conforms to Content Security Policy (CSP) restrictions in modern Chrome extensions.
- Full Implementation: This outline provides a structure but does not cover every edge case or exhaustive testing for robustness and security, especially around asynchronous behavior and tab management.
This setup provides a starting point for developing the extension. It may need experimentation and further development based on testing in different scenarios, particularly with handling network requests and specific user interactions.