Make me the code for a chrome extension that allow me to download all the webm videos in a thread in 4chan.org / boards.4chan.org as a zip file containing the webms.. Made it so there is a button named "Download all WEBMS" to trigger the download. Add an option to give the extension an icon.
Assistant
Certainly! Below is the code for a Chrome extension that adds a "Download all WEBMS" button to 4chan thread pages. When clicked, it collects all the .webm video URLs in the thread, downloads them, and packages them into a ZIP file for you to download. The extension also includes an icon.
Overview
Extension Name: 4chan WEBM Downloader
Description: Downloads all .webm videos from a 4chan thread as a ZIP file.
Features:
Adds a "Download all WEBMS" button to 4chan thread pages.
Downloads all .webm files in the thread.
Packages the files into a ZIP for easy download.
Includes an icon for the extension.
Files and Code
1. manifest.json
This file tells Chrome about your extension, what it does, and the permissions it needs.
json
{
"manifest_version": 3,
"name": "4chan WEBM Downloader",
"description": "Download all WEBM videos from a 4chan thread as a zip file.",
"version": "1.0",
"permissions": [
"activeTab",
"downloads",
"scripting"
],
"host_permissions": [
"*://i.4cdn.org/*"
],
"icons": {
"128": "icon.png"
},
"action": {
"default_title": "Download WEBMs",
"default_icon": "icon.png"
},
"content_scripts": [
{
"matches": ["*://boards.4chan.org/*/thread/*"],
"js": ["content.js"]
}
]
}
Explanation:
manifest_version: Specifies the version of the manifest file format.
permissions: Declares the permissions the extension needs.
"activeTab": Allows the extension to access the active tab.
"downloads": Allows the extension to download files.
"scripting": Allows execution of scripts.
host_permissions: Grants access to specific hosts (needed for fetching resources).
icons: Specifies the extension's icon.
action: Configures the extension's action (icon in the toolbar).
content_scripts: Injects JavaScript into matching pages (4chan threads).
2. content.js
This script runs on the thread pages and handles the downloading process.
javascript
// Function to download all WEBM files
async function downloadAllWebms() {
// Collect all WEBM URLs
const webmElements = document.querySelectorAll('a.fileThumb[href$=".webm"]');
const webmUrls = Array.from(webmElements).map(el => el.href);
if (webmUrls.length === 0) {
alert("No WEBM files found on this page.");
return;
}
// Create a zip file using JSZip
const zip = new JSZip();
let count = 0;
for (const url of webmUrls) {
try {
const response = await fetch(url);
const blob = await response.blob();
const filename = url.substring(url.lastIndexOf('/') + 1);
zip.file(filename, blob);
count++;
} catch (error) {
console.error('Error fetching ' + url);
}
}
if (count === 0) {
alert("Failed to download WEBM files.");
return;
}
// Generate the zip file and initiate download
zip.generateAsync({ type: "blob" }).then(content => {
const downloadUrl = URL.createObjectURL(content);
const a = document.createElement('a');
a.href = downloadUrl;
a.download = 'webms.zip';
document.body.appendChild(a);
a.click();
a.remove();
URL.revokeObjectURL(downloadUrl);
});
}
// Create and insert the "Download all WEBMS" button
function createDownloadButton() {
// Check if the button already exists
if (document.getElementById('download-webms-button')) return;
const button = document.createElement('button');
button.id = 'download-webms-button';
button.textContent = 'Download all WEBMS';
button.style.position = 'fixed';
button.style.bottom = '20px';
button.style.right = '20px';
button.style.padding = '10px 15px';
button.style.backgroundColor = '#4CAF50';
button.style.color = '#fff';
button.style.border = 'none';
button.style.borderRadius = '5px';
button.style.cursor = 'pointer';
button.style.zIndex = '1000';
button.addEventListener('click', downloadAllWebms);
document.body.appendChild(button);
}
// Inject JSZip library
function injectJSZip(callback) {
if (window.JSZip) {
callback();
return;
}
const script = document.createElement('script');
script.src = 'https://cdnjs.cloudflare.com/ajax/libs/jszip/3.7.1/jszip.min.js';
script.onload = callback;
document.head.appendChild(script);
}
// Initialize the extension functionality
injectJSZip(createDownloadButton);
Explanation:
downloadAllWebms():
Collects all .webm links on the page.
Uses fetch() to retrieve each file.
Adds each file to a ZIP archive using JSZip.
Initiates a download of the ZIP file.
createDownloadButton():
Creates a "Download all WEBMS" button.
Styles it and appends it to the page.
Attaches an event listener to trigger the download.
injectJSZip():
Dynamically loads the JSZip library from a CDN.
Ensures the library is loaded before executing the callback.
Script Execution:
The script checks if JSZip is already loaded.
If not, it injects the script and then creates the button.
3. icon.png
Include an icon image for your extension.
Size: Ideally 128x128 pixels.
Location: Place the icon.png file in the root of your extension folder.
Installation Instructions
Prepare the Extension Folder:
Create a new folder on your computer (e.g., 4chan-webm-downloader).
Place the manifest.json, content.js, and icon.png files inside this folder.
Load the Extension in 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 the extension folder you created.
Verify the Extension:
The extension should now appear in your list of installed extensions.
An icon should appear in the toolbar (it might be in the extensions menu).
Usage Instructions
Navigate to a 4chan Thread:
Go to https://boards.4chan.org/ and open any thread that contains .webm files.
Download the WEBM Files:
On the thread page, you should see a "Download all WEBMS" button in the bottom-right corner.
Click the button to initiate the download.
A ZIP file named webms.zip will be downloaded, containing all the .webm files from the thread.
Notes and Considerations
Permissions:
The extension requires permissions to interact with the active tab, download files, and execute scripts.
Host permissions are specified to allow fetching .webm files from i.4cdn.org.
JSZip Library:
The script dynamically loads the JSZip library from a CDN to handle ZIP file creation.
If you prefer to include the library locally, download jszip.min.js and adjust the script accordingly.
Content Security Policy (CSP):
Chrome extensions have strict CSPs.
By dynamically injecting the script, we comply with the CSP without needing to relax security settings.
Potential Issues:
If the "Download all WEBMS" button doesn't appear, ensure that you're on a thread page (/thread/ URL).
Check the console for any errors related to script execution or fetching resources.
Extension Icon
Creating an icon enhances the visual appeal of your extension.
Creating an Icon:
You can design an icon using any image editing software.
Ensure it's saved as icon.png and placed in the extension folder.
Icon Sizes:
You can include different icon sizes in the manifest.json if desired.
You now have a fully functional Chrome extension that allows you to download all .webm videos from a 4chan thread with a single click. Remember to use this extension responsibly and respect the website's terms of service.
Disclaimer: This extension is for personal use. Be sure to comply with 4chan's terms of service and any applicable laws when downloading content.