Creating a Chrome extension that fulfills your requirements involves several components. Below are the complete files needed for your extension. This extension listens for a specific link (a smiley face) in YouTube video descriptions. When the link is clicked, it opens a popup window displaying two YouTube videos stacked vertically along with a button beneath them. The popup also displays a floating message instructing users to press Esc to exit, and pressing the Esc key will close the popup.
1. manifest.json
This file defines the extension's configuration, permissions, and the scripts it uses.
{
"manifest_version": 3,
"name": "YouTube Video Popup Extension",
"version": "1.0",
"description": "Launches a popup with two YouTube videos from a smiley face link.",
"permissions": ["scripting", "tabs"],
"content_scripts": [
{
"matches": ["https://www.youtube.com/*"],
"js": ["content.js"]
}
],
"web_accessible_resources": [
{
"resources": ["popup.html", "popup.js", "popup.css"],
"matches": ["https://www.youtube.com/*"]
}
],
"icons": {
"16": "icon16.png",
"48": "icon48.png",
"128": "icon128.png"
}
}
2. content.js
This script runs on YouTube pages, searches for smiley face links, and sets up click handlers to open the popup with the specified YouTube videos.
// content.js
// Function to create a unique identifier for the popup
function getPopupURL(video1, video2) {
const url = chrome.runtime.getURL(
`popup.html?v1=${video1}&v2=${video2}`
);
return url;
}
// Function to find all smiley face links
function findSmileyLinks() {
const links = document.querySelectorAll('a');
const smileyLinks = [];
links.forEach(link => {
if (link.textContent.trim() === '😊') {
smileyLinks.push(link);
}
});
return smileyLinks;
}
// Setup click listeners on smiley links
function setupLinks() {
const smileyLinks = findSmileyLinks();
smileyLinks.forEach(link => {
// Avoid adding multiple listeners
if (!link.dataset.popupListener) {
link.dataset.popupListener = "true";
link.addEventListener('click', (e) => {
e.preventDefault();
const url = new URL(link.href);
const params = new URLSearchParams(url.search);
const video1 = params.get('v1');
const video2 = params.get('v2');
if (video1 && video2) {
chrome.windows.create({
url: getPopupURL(video1, video2),
type: 'popup',
width: 800,
height: 600
});
} else {
alert('Invalid video IDs in the link.');
}
});
}
});
}
// Observe for dynamic content changes (e.g., infinite scrolling on YouTube)
const observer = new MutationObserver(() => {
setupLinks();
});
observer.observe(document.body, { childList: true, subtree: true });
// Initial setup
setupLinks();
3. popup.html
This is the HTML structure for the popup window. It includes two YouTube video iframes, a button below them, and a floating message.
<!-- popup.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>YouTube Popup</title>
<link rel="stylesheet" href="popup.css">
</head>
<body>
<div id="floating-message">Press ESC to exit</div>
<div class="video-container">
<iframe id="video1" src="" frameborder="0" allowfullscreen></iframe>
<iframe id="video2" src="" frameborder="0" allowfullscreen></iframe>
</div>
<button id="action-button">Click Me</button>
<script src="popup.js"></script>
</body>
</html>
4. popup.css
This CSS file styles the popup, ensuring there are no extra GUI elements except the videos and the button. It also styles the floating message.
/* popup.css */
body {
margin: 0;
padding: 0;
background-color: #000;
color: #fff;
font-family: Arial, sans-serif;
position: relative;
}
.video-container {
display: flex;
flex-direction: column;
align-items: center;
}
iframe {
width: 760px;
height: 400px;
margin: 10px 0;
}
#action-button {
display: block;
margin: 20px auto;
padding: 10px 20px;
font-size: 16px;
}
#floating-message {
position: absolute;
top: 10px;
right: 10px;
background: rgba(0,0,0,0.7);
padding: 5px 10px;
border-radius: 5px;
font-size: 14px;
}
5. popup.js
This script initializes the popup by extracting video IDs from the URL, embedding the YouTube videos, handles the Esc key to close the popup, and ensures that clicking on videos replaces them within the popup instead of opening new tabs.
// popup.js
// Function to get query parameters
function getQueryParams() {
const params = new URLSearchParams(window.location.search);
return {
v1: params.get('v1'),
v2: params.get('v2')
};
}
// Initialize the popup with videos
function init() {
const { v1, v2 } = getQueryParams();
if (v1 && v2) {
const video1 = document.getElementById('video1');
const video2 = document.getElementById('video2');
video1.src = `https://www.youtube.com/embed/${v1}?enablejsapi=1`;
video2.src = `https://www.youtube.com/embed/${v2}?enablejsapi=1`;
} else {
alert('Missing video IDs.');
}
// Handle Esc key to close popup
document.addEventListener('keydown', (e) => {
if (e.key === 'Escape') {
window.close();
}
});
// Prevent videos from opening in new tabs
const iframes = document.querySelectorAll('iframe');
iframes.forEach(iframe => {
iframe.addEventListener('load', () => {
const iframeDoc = iframe.contentDocument || iframe.contentWindow.document;
iframeDoc.addEventListener('click', (e) => {
e.preventDefault();
const target = e.target.closest('a');
if (target && target.href) {
iframe.src = target.href.includes('youtube.com') ? target.href.replace('/watch?v=', '/embed/') : iframe.src;
}
});
});
});
}
document.addEventListener('DOMContentLoaded', init);
6. Icons
While not strictly necessary for functionality, it's good practice to include icons for your extension. Create three PNG images named icon16.png, icon48.png, and icon128.png in your extension directory with appropriate sizes (16x16, 48x48, and 128x128 pixels respectively).
7. Packaging the Extension
-
Directory Structure:
your-extension/
├── manifest.json
├── content.js
├── popup.html
├── popup.js
├── popup.css
├── icon16.png
├── icon48.png
└── icon128.png
-
Loading the Extension:
- Open Chrome and navigate to
chrome://extensions/.
- Enable "Developer mode" using the toggle in the top right corner.
- Click on "Load unpacked" and select your extension's directory (
your-extension/).
-
Using the Extension:
-
In your YouTube video description, add a link with the smiley face as the link text. The link should include the two YouTube video IDs as query parameters. For example:
[😊](https://example.com/?v1=VIDEOID1&v2=VIDEOID2)
Replace VIDEOID1 and VIDEOID2 with the actual YouTube video IDs you wish to display.
-
When a user clicks on the smiley face link while on YouTube, the extension will intercept the click and open the popup with the specified videos.
Notes:
-
URL Structure: The extension looks for links where the link text is exactly the smiley face 😊 and expects the URL to contain query parameters v1 and v2 representing the YouTube video IDs.
-
Popup Behavior: The popup window is a minimal interface containing only the two YouTube videos and a button beneath them. The floating message instructs users to press Esc to exit, which will close the popup when pressed.
-
Video Interaction: Clicking on any link within the embedded YouTube videos will replace the clicked video within the iframe instead of opening a new tab.
-
Security Considerations: Ensure that the extension handles URLs and video IDs securely to prevent any potential injection attacks or misuse.
Feel free to customize the button's functionality in popup.html and popup.js as needed. This setup provides a foundational structure based on your specified requirements.