i want a chrome extension that launches from a link i can paste in a youtube video description. i do not want to have to host a webpage to make it function. it should load a popup window when launched. the popup window should stack two youtube videos on top of each other then have a button bellow both. i want the link to use two youtube video ids that i can paste into the link. make the link a smiley face. the popup window should have no gui at all except for the videos and the minimize, fullscreen, and exit controls, and the one button.
Assistant
Creating a Chrome extension that launches from a smiley face link in a YouTube video description and displays a popup with two stacked YouTube videos and a button involves several steps. Below is a comprehensive guide to help you build this extension without needing to host a webpage.
Overview
Extension Structure: Define the necessary files and folder structure.
Manifest File: Configure manifest.json with required permissions and settings.
Content Script: Detect and handle the smiley face link in YouTube descriptions.
Popup Window: Create the popup that displays the two YouTube videos and a button.
Packaging and Installation: Load the extension into Chrome for testing.
Step 1: Extension Structure
Create a new folder for your Chrome extension, for example, youtube-popup-extension. Inside this folder, create the following files:
manifest.json
content.js
popup.html
popup.js
popup.css(optional for styling)
Step 2: Configure manifest.json
The manifest.json file defines the extension's properties, permissions, and resources. Here's how you can set it up:
json
{
"manifest_version": 3,
"name": "YouTube Video Popup",
"version": "1.0",
"description": "Launches a popup with two YouTube videos from a smiley face link.",
"permissions": [
"activeTab",
"scripting"
],
"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": "icons/icon16.png",
"48": "icons/icon48.png",
"128": "icons/icon128.png"
}
}
Notes:
Permissions: activeTab allows the extension to interact with the current tab, and scripting enables injecting scripts.
Content Scripts: content.js will run on YouTube pages to detect the custom link.
Web Accessible Resources: Allows the popup files to be loaded when the link is clicked.
Icons: You can add your own icons or omit if not needed.
Step 3: Create the Smiley Face Link
In your YouTube video description, add a link that uses a custom protocol or query parameters to pass the two YouTube video IDs. Here's an example using query parameters:
Replace VIDEO_ID1 and VIDEO_ID2 with the actual YouTube video IDs you want to display.
Step 4: Develop content.js
This script will detect clicks on links with the custom protocol (e.g., myextension://) and handle opening the popup window.
javascript
// content.js
// Function to parse URL and extract video IDs
function parseURL(url) {
try {
let urlObj = new URL(url);
let video1 = urlObj.searchParams.get('video1');
let video2 = urlObj.searchParams.get('video2');
return { video1, video2 };
} catch (e) {
console.error('Invalid URL:', e);
return null;
}
}
// Listen for click events on the page
document.addEventListener('click', function(event) {
let target = event.target;
// Traverse up the DOM tree to find the anchor tag
while (target && target !== document) {
if (target.tagName === 'A') break;
target = target.parentNode;
}
if (target && target.tagName === 'A') {
let href = target.getAttribute('href');
// Check if the link uses the custom protocol
if (href && href.startsWith('myextension://popup')) {
event.preventDefault(); // Prevent default link behavior
let params = parseURL(href);
if (params && params.video1 && params.video2) {
// Send a message to the background or open the popup
chrome.runtime.sendMessage({
action: 'openPopup',
video1: params.video1,
video2: params.video2
});
} else {
alert('Invalid video IDs.');
}
}
}
}, true);
Step 5: Handle Message in background.js
Since manifest_version is 3, background scripts are service workers. However, for simplicity, we'll handle the popup directly in content.js by injecting the popup HTML. Alternatively, you can create a background.js and declare it in the manifest.
For brevity, let's modify content.js to open a new window with the popup HTML.
javascript
// content.js (continued)
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if (request.action === 'openPopup') {
// Create a URL for the popup with video IDs as query parameters
let popupURL = chrome.runtime.getURL(`popup.html?video1=${request.video1}&video2=${request.video2}`);
// Open a new window with minimal UI
window.open(popupURL, 'YouTube Video Popup', 'width=800,height=600,toolbar=no,location=no,menubar=no');
}
});
Ensure that you have the correct messaging system if you opt for a separate background script. For simplicity, you can keep all logic within content.js.
Step 6: Create popup.html
This HTML file defines the structure of the popup window, displaying two YouTube videos stacked vertically and a button below them.
This script will extract the video IDs from the URL and set the src of the iframes accordingly. It will also handle the button's functionality.
javascript
// popup.js
// Function to parse query parameters
function getQueryParams() {
let params = {};
let queryString = window.location.search.substring(1);
let vars = queryString.split('&');
vars.forEach(function(v) {
let pair = v.split('=');
if(pair.length === 2){
params[pair[0]] = decodeURIComponent(pair[1]);
}
});
return params;
}
// Set video sources
document.addEventListener('DOMContentLoaded', function() {
let params = getQueryParams();
if(params.video1 && params.video2){
document.getElementById('video1').src = `https://www.youtube.com/embed/${params.video1}`;
document.getElementById('video2').src = `https://www.youtube.com/embed/${params.video2}`;
} else {
alert('Missing video IDs.');
}
// Handle button click
document.getElementById('actionButton').addEventListener('click', function() {
alert('Button Clicked!');
// Add your desired functionality here
});
});
Step 9: Add Icons (Optional)
If you specified icons in the manifest.json, add icon files (icon16.png, icon48.png, icon128.png) inside an icons folder within your extension directory.
Step 10: Load the Extension into Chrome
Open Chrome and navigate to chrome://extensions/.
Enable Developer mode by toggling the switch in the top right corner.
Click on Load unpacked and select your extension's directory (youtube-popup-extension).
Ensure the extension is enabled.
Step 11: Test the Extension
Upload or edit a YouTube video description to include the smiley face link with actual YouTube video IDs:
A popup window should open, displaying the two specified YouTube videos stacked vertically and a button below them.
Additional Considerations
Custom Protocols: Chrome has restrictions on custom protocols. Using myextension:// as shown may not work directly. Instead, consider using a unique URL pattern (e.g., https://open-popup?video1=...&video2=...) and ensure the content script detects this pattern.
Security: Ensure that the extension properly validates and sanitizes input to prevent security issues such as XSS attacks.
Optimizing UI: You can further refine the popup's appearance and behavior based on your specific needs.
Example Link with Smiley Face
Since using myextension:// might not work as intended, here's an alternative approach using query parameters with a standard HTTP or unique path:
And modify the content.js to detect popup=true in the URL and extract video1 and video2 parameters accordingly.
Final Thoughts
Building a Chrome extension that interacts seamlessly with YouTube descriptions involves careful handling of permissions, content scripts, and user interactions. The guide above provides a foundational approach, but you may need to adjust based on specific requirements or Chrome's evolving extension APIs.
Feel free to extend the functionality, improve the UI/UX, and add more features as needed!