My Friends,
I want to open source a small project I use from time to time: Video Extractor
This is a Google Chrome Extension, so its simple and easy to install, if you use Google Chrome. Its early days, there is a lot that can be done to extend this tool, and make it better! It does not work with video blobs yet. One day soon maybe I will include Blob Videos.
Inside a file: Google Chrome Video Extractor, create the file structure:
In the File: background.js
chrome.runtime.onInstalled.addListener(() => {
console.log('Extension installed');
});
In the File: content.js
function extractAllVideos() {
const videos = document.querySelectorAll('video');
const videoList = Array.from(videos).map((video, index) => {
const sourceUrl = video.src || (video.querySelector('source') ? video.querySelector('source').src : '');
const poster = video.poster || '';
const width = video.width || video.videoWidth;
const height = video.height || video.videoHeight;
const videoName = video.getAttribute('title') || `Video ${index + 1}`;
const videoTag = vide
uterHTML;
return { videoTag: videoTag, videoName: videoName, sourceUrl: sourceUrl, poster: poster, width: width, height: height };
}).filter(video => video.sourceUrl !== ''); // Filter out videos without source URLs
return videoList;
}
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === 'extractVideos') {
const videoInfo = extractAllVideos();
sendResponse(videoInfo);
}
});
In the File: manifest.json
{
"manifest_version": 3,
"name": "Video Extractor",
"version": "1.0",
"description": "Extracts video tags and their source URLs, then opens videos in a new tab",
"permissions": [
"activeTab",
"scripting"
],
"background": {
"service_worker": "background.js"
},
"action": {
"default_popup": "popup.html"
},
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["content.js"]
}
]
}
In the File: popup.html
<!DOCTYPE html>
<html>
<head>
<title>Video Extractor</title>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet">
<style>
body {
font-family: Arial, sans-serif;
padding: 20px;
border-radius: 3px;
width: 473px;
background-color: #343a40;
color: white;
}
.T {
margin-top: 3x;
padding-left: 20px;
padding-right: 20px;
border: #454d55 1px solid;
border-radius: 7px;
}
table {
border: none;
}
thead {
display: table-header-group;
vertical-align: middle;
unicode-bidi: isolate;
border-color: none;
}
.video-list {
max-height: 400px;
overflow-y: auto;
}
.thumbnail {
width: 124px;
height: 70px;
object-fit: cover;
border-radius: 3px;
}
.scrollbar-dark::-webkit-scrollbar {
width: 8px;
background-color: #343a40;
}
.scrollbar-dark::-webkit-scrollbar-thumb {
background-color: #6c757d;
border-radius: 10px;
}
</style>
</head>
<body>
<h3 class="text-center">Video Extractor</h3>
<div class="T">
<table class="table table-dark table-striped">
<thead>
<tr>
<th scope="col">Thumbnail</th>
<th scope="col">Details</th>
</tr>
</thead>
<tbody id="videoList" class="video-list scrollbar-dark"></tbody>
</table>
</div>
<script src="popup.js"></script>
</body>
</html>
In the File: popup.js
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
const activeTab = tabs[0];
chrome.scripting.executeScript({
target: { tabId: activeTab.id },
function: extractAllVideosInTab
}, (results) => {
if (results && results[0] && results[0].result) {
const videoList = results[0].result;
displayVideos(videoList);
} else {
alert('No videos found on this page.');
}
});
});
function extractAllVideosInTab() {
const videos = document.querySelectorAll('video');
const videoList = Array.from(videos).map((video, index) => {
const sourceUrl = video.src || (video.querySelector('source') ? video.querySelector('source').src : '');
const poster = video.poster || '';
const width = video.width || video.videoWidth;
const height = video.height || video.videoHeight;
const videoName = video.getAttribute('title') || `Video ${index + 1}`;
const videoTag = vide
uterHTML;
return { videoTag: videoTag, videoName: videoName, sourceUrl: sourceUrl, poster: poster, width: width, height: height };
}).filter(video => video.sourceUrl !== ''); // Filter out videos without source URLs
return videoList;
}
function displayVideos(videoList) {
const videoListElement = document.getElementById('videoList');
videoListElement.innerHTML = '';
videoList.forEach(video => {
const row = document.createElement('tr');
const thumbnailCell = document.createElement('td');
const thumbnail = document.createElement('img');
thumbnail.src = video.poster || 'https://via.placeholder.com/124x70?text=No+Thumbnail';
thumbnail.className = 'thumbnail';
thumbnailCell.appendChild(thumbnail);
const detailsCell = document.createElement('td');
detailsCell.innerHTML = `
<strong>${video.videoName}</strong><br>
<small>Width: ${video.width}px, Height: ${video.height}px</small><br>
<a href="${video.sourceUrl}" target="_blank" style="color: #17a2b8;">Source URL</a>
`;
row.appendChild(thumbnailCell);
row.appendChild(detailsCell);
videoListElement.appendChild(row);
});
}
Easy, then once you have this done, all you need to do is add the extension:
Adding a Google Chrome extension is pretty straightforward! Here's a step-by-step guide:
- Open Chrome: Make sure you're using the Google Chrome browser.
- Go to the Chrome Web Store: You can access it by clicking on this [link](https://support.google.com/chrome_webstore/answer/2664769?hl=en).
- Find an Extension: Use the search bar or browse through categories to find the extension you want.
- Click "Add to Chrome": Once you've found an extension you like, click the "Add to Chrome" button.
- Confirm Installation: A pop-up will appear asking for confirmation. Click "Add Extension" to install it.
- Access the Extension: After installation, you'll see the extension's icon next to the address bar. Click on it to start using the extension.
And that's it! You're all set to enjoy your new extension. 😊
Once done, you will be able to access Video Extractor like so:
Enjoy!
Best Wishes,
Chris