Creating a Chrome extension with the features you've described involves multiple steps, including using the Chrome Extension APIs, JavaScript for functionality, and HTML/CSS for the user interface. Below is a simplified outline of how you could implement this extension. This is a conceptual guide; integrating all parts may require additional resources and testing.
manifest.json
This is the manifest file that provides metadata about your extension.
{
"manifest_version": 3,
"name": "SimulPlay",
"version": "1.0",
"permissions": ["storage", "tabs", "activeTab", "scripting"],
"host_permissions": ["<all_urls>"],
"action": {
"default_popup": "popup.html",
"default_icon": {
"16": "icons/icon16.png",
"48": "icons/icon48.png",
"128": "icons/icon128.png"
}
},
"background": {
"service_worker": "background.js"
},
"content_scripts": [
{
"matches": ["*://*.youtube.com/*"],
"js": ["content.js"]
}
]
}
popup.html
A user interface to input URLs and set start times.
<!DOCTYPE html>
<html>
<head>
<title>SimulPlay</title>
<style>
/* Basic styling */
</style>
</head>
<body>
<div>
<h3>Music:</h3>
<input id="musicUrl" type="text" placeholder="Enter YouTube URL or MP3 file">
<input id="musicStartTime" type="number" placeholder="Start time in seconds">
</div>
<div>
<h3>Video:</h3>
<input id="videoUrl" type="text" placeholder="Enter YouTube URL or MP4 file">
<input id="videoStartTime" type="number" placeholder="Start time in seconds">
</div>
<button id="playButton" disabled>Master Play</button>
<script src="popup.js"></script>
</body>
</html>
popup.js
Handles the UI interactions and sends data to the background script.
document.getElementById('playButton').addEventListener('click', function() {
chrome.storage.sync.get(['musicData', 'videoData'], function(data) {
chrome.runtime.sendMessage({ action: 'playSimultaneously', musicData: data.musicData, videoData: data.videoData });
});
});
document.querySelectorAll('input').forEach(input => {
input.addEventListener('input', function() {
const musicUrl = document.getElementById('musicUrl').value;
const videoUrl = document.getElementById('videoUrl').value;
const musicStartTime = document.getElementById('musicStartTime').value;
const videoStartTime = document.getElementById('videoStartTime').value;
const musicData = { url: musicUrl, startTime: parseInt(musicStartTime) || 0 };
const videoData = { url: videoUrl, startTime: parseInt(videoStartTime) || 0 };
chrome.storage.sync.set({ musicData: musicData, videoData: videoData }, function() {
if (musicUrl && videoUrl) {
document.getElementById('playButton').disabled = false;
}
});
});
});
background.js
Manages communication between popup and content scripts, opens tabs, and checks when ads are done.
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
if (message.action === 'playSimultaneously') {
const { musicData, videoData } = message;
let musicTab, videoTab;
chrome.tabs.create({ url: musicData.url }, function(tab) {
musicTab = tab;
});
chrome.tabs.create({ url: videoData.url }, function(tab) {
videoTab = tab;
});
const checkAdsAndEnablePlay = () => {
// This function would use content scripts to check for ad completions and enable the play button
// Placeholder logic to enable play
chrome.tabs.sendMessage(musicTab.id, { action: 'checkAdStatus' }, function(response) {
if (response.adsDone) {
chrome.tabs.sendMessage(videoTab.id, { action: 'checkAdStatus' }, function(response) {
if (response.adsDone) {
chrome.tabs.update(videoTab.id, { active: true });
chrome.tabs.update(musicTab.id, { active: true }); // Optionally make the video tab active
chrome.scripting.executeScript({
target: { tabId: videoTab.id },
func: enablePlayButton
});
}
});
}
});
};
// Poll until ads are done
const intervalId = setInterval(() => {
checkAdsAndEnablePlay();
}, 1000);
const enablePlayButton = () => {
const button = document.getElementById('playButton');
if (button) {
button.disabled = false;
button.addEventListener('click', () => {
// Play both videos simultaneously
chrome.tabs.sendMessage(musicTab.id, { action: 'play', startTime: musicData.startTime });
chrome.tabs.sendMessage(videoTab.id, { action: 'play', startTime: videoData.startTime });
});
}
};
}
});
content.js
Script injected into YouTube pages to check ad status and control playback.
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
if (message.action === 'checkAdStatus') {
// Placeholder logic to check if ads are done
const adsDone = true; // Replace with actual check
sendResponse({ adsDone: adsDone });
}
if (message.action === 'play') {
const videoElement = document.querySelector('video');
if (videoElement) {
videoElement.currentTime = message.startTime;
videoElement.play();
}
}
});
Keep in mind, this is a basic outline to get you started. Real-world implementations will require handling ad detection more specifically, permissions for cross-origin content, error handling, ensuring URLs are correctly parsed and played, and additional considerations for YouTube's API and content scripts behavior. Be sure to test thoroughly and adhere to YouTube's terms of service.