diff --git a/submissions/YT-Playlist-Length/LICENSE.md b/submissions/YT-Playlist-Length/LICENSE.md new file mode 100644 index 00000000..4f724c5c --- /dev/null +++ b/submissions/YT-Playlist-Length/LICENSE.md @@ -0,0 +1,22 @@ + +The MIT License (MIT) + +Copyright (c) 2025 M. Umar Shahbaz + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/submissions/YT-Playlist-Length/README.md b/submissions/YT-Playlist-Length/README.md new file mode 100644 index 00000000..8c6b3b37 --- /dev/null +++ b/submissions/YT-Playlist-Length/README.md @@ -0,0 +1,36 @@ +# YT-Playlist-Length + +![Example](Screenshot.png) + +A simple browser extension for people who use YouTube to watch courses. +Displays total duration of any opened playlist at the top right corner of the page. + +## Features + +1. No Setup +2. No load - Consumes negligble amount of processing power +3. Auto-detect - Just open a playlist and it'll start the code. + +## Demo + +![Demo](demo.gif) + +### ⚠ Note about the API Key + +Yes, the API key is visible — that’s because this is a 100% client-side project and hiding it is not technically possible. + +However, using my key is pointless. It’s tied to strict usage limits, domain restrictions, and quotas. If you’re trying to actually use the extension or build on top of it, just create your own key — it’s free and takes two minutes on [Google Cloud Console](https://console.cloud.google.com/apis/credentials). + +## Install + +You can find the extension `.crx` file [here](https://github.com/MUmarShahbaz/YT-Playlist-Length/releases/tag/v1.0.0) + +- On most browsers you can just use the the .crx file +- On Chrome, you need to go a roundabout method. + +#### Chrome Installation +- Download the `src/` folder from this repo +- Open Extensions Manager on Chrome and enable developer mode +- Choose the `Load Unpacked` feature, it'll ask you to open a folder +- Open the `src/` folder you downloaded +- Done \ No newline at end of file diff --git a/submissions/YT-Playlist-Length/Screenshot.png b/submissions/YT-Playlist-Length/Screenshot.png new file mode 100644 index 00000000..b2549104 Binary files /dev/null and b/submissions/YT-Playlist-Length/Screenshot.png differ diff --git a/submissions/YT-Playlist-Length/demo.gif b/submissions/YT-Playlist-Length/demo.gif new file mode 100644 index 00000000..797ff2ce Binary files /dev/null and b/submissions/YT-Playlist-Length/demo.gif differ diff --git a/submissions/YT-Playlist-Length/src/icons/icon128.png b/submissions/YT-Playlist-Length/src/icons/icon128.png new file mode 100644 index 00000000..f6ff5f72 Binary files /dev/null and b/submissions/YT-Playlist-Length/src/icons/icon128.png differ diff --git a/submissions/YT-Playlist-Length/src/icons/icon16.png b/submissions/YT-Playlist-Length/src/icons/icon16.png new file mode 100644 index 00000000..01be2529 Binary files /dev/null and b/submissions/YT-Playlist-Length/src/icons/icon16.png differ diff --git a/submissions/YT-Playlist-Length/src/icons/icon32.png b/submissions/YT-Playlist-Length/src/icons/icon32.png new file mode 100644 index 00000000..9b35681c Binary files /dev/null and b/submissions/YT-Playlist-Length/src/icons/icon32.png differ diff --git a/submissions/YT-Playlist-Length/src/icons/icon48.png b/submissions/YT-Playlist-Length/src/icons/icon48.png new file mode 100644 index 00000000..20915c56 Binary files /dev/null and b/submissions/YT-Playlist-Length/src/icons/icon48.png differ diff --git a/submissions/YT-Playlist-Length/src/manifest.json b/submissions/YT-Playlist-Length/src/manifest.json new file mode 100644 index 00000000..3b8a2947 --- /dev/null +++ b/submissions/YT-Playlist-Length/src/manifest.json @@ -0,0 +1,21 @@ +{ + "manifest_version": 3, + "name": "YT Playlist Total Duration", + "version": "1.0.0", + "description": "Displays the total duration of a YouTube playlist.", + "icons": { + "16": "icons/icon16.png", + "32": "icons/icon32.png", + "48": "icons/icon48.png", + "128": "icons/icon128.png" + }, + "content_scripts": [ + { + "matches": ["https://www.youtube.com/playlist*"], + "js": ["playlist_length.js"], + "run_at": "document_idle" + } + ], + "permissions": [], + "host_permissions": ["https://www.youtube.com/playlist*"] +} diff --git a/submissions/YT-Playlist-Length/src/playlist_length.js b/submissions/YT-Playlist-Length/src/playlist_length.js new file mode 100644 index 00000000..6f6807c3 --- /dev/null +++ b/submissions/YT-Playlist-Length/src/playlist_length.js @@ -0,0 +1,117 @@ +function renderBadge(total_formatted) { + const badge = document.createElement('div'); + badge.className = 'yt-total-duration-fixed'; + badge.textContent = `⏱ Total Duration: ${total_formatted}`; + + Object.assign(badge.style, { + position: 'fixed', + top: '12px', + right: '12px', + zIndex: 10000, + backgroundColor: '#212121', + color: '#fff', + padding: '8px 12px', + borderRadius: '6px', + fontSize: '14px', + fontWeight: '500', + boxShadow: '0 2px 8px rgba(0,0,0,0.4)', + opacity: '0', + cursor: 'move', + userSelect: 'none', + transition: 'opacity 0.5s ease' + }); + + const closeBtn = document.createElement('span'); + closeBtn.textContent = ' ×'; + closeBtn.style.marginLeft = '8px'; + closeBtn.style.cursor = 'pointer'; + closeBtn.style.fontWeight = 'bold'; + closeBtn.onclick = () => badge.remove(); + badge.appendChild(closeBtn); + + let isDragging = false, offsetX, offsetY; + badge.addEventListener('mousedown', (e) => { + isDragging = true; + offsetX = e.clientX - badge.offsetLeft; + offsetY = e.clientY - badge.offsetTop; + e.preventDefault(); + }); + document.addEventListener('mousemove', (e) => { + if (isDragging) { + badge.style.left = `${e.clientX - offsetX}px`; + badge.style.top = `${e.clientY - offsetY}px`; + badge.style.right = 'auto'; + } + }); + document.addEventListener('mouseup', () => isDragging = false); + + document.body.appendChild(badge); + requestAnimationFrame(() => badge.style.opacity = '1'); +} + +async function getTotalPlaylistDuration(playlistId, apiKey) { + let videoIds = []; + let nextPageToken = ''; + + do { + const url = `https://www.googleapis.com/youtube/v3/playlistItems?part=contentDetails&maxResults=50&playlistId=${playlistId}&key=${apiKey}&pageToken=${nextPageToken}`; + const res = await fetch(url); + const data = await res.json(); + + data.items.forEach(item => { + videoIds.push(item.contentDetails.videoId); + }); + + nextPageToken = data.nextPageToken || ''; + } while (nextPageToken); + + let totalSeconds = 0; + for (let i = 0; i < videoIds.length; i += 50) { + const batch = videoIds.slice(i, i + 50); + const url = `https://www.googleapis.com/youtube/v3/videos?part=contentDetails&id=${batch.join(',')}&key=${apiKey}`; + const res = await fetch(url); + const data = await res.json(); + + data.items.forEach(item => { + const duration = item.contentDetails.duration; + totalSeconds += isoDurationToSeconds(duration); + }); + } + + // Convert total seconds to H:M:S + const hours = Math.floor(totalSeconds / 3600); + const minutes = Math.floor((totalSeconds % 3600) / 60); + const seconds = totalSeconds % 60; + + const duration = `${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`; + return duration; +} + +function isoDurationToSeconds(duration) { + const regex = /PT(?:(\d+)H)?(?:(\d+)M)?(?:(\d+)S)?/; + const [, hours, minutes, seconds] = duration.match(regex).map(x => parseInt(x || 0)); + return (hours * 3600) + (minutes * 60) + seconds; +} + +async function displayDuration(playlist) { + const API_KEY = 'AIzaSyCl1NjFBnvTCthuX-EmgmrEkXbXWWjNN-U'; + + const time = await getTotalPlaylistDuration(playlist, API_KEY); + + renderBadge(time); + +} + +let displayed = false; +setInterval(() => { + let url = location.href; + let badge = document.querySelector('.yt-total-duration-fixed'); + if (!url.includes('playlist?list=') || (/playlist\?list=WL(&|$)/).test(url) || (/playlist\?list=LL(&|$)/).test(url)) { + displayed = false; + badge?.remove(); + } else if (!displayed) { + displayed = true; + const playlist = url.match(/playlist\?list=([^&]+)/)[1]; + displayDuration(playlist); + } +}, 500); \ No newline at end of file