Skip to content

Instantly share code, notes, and snippets.

@gledos
Created December 6, 2023 08:54
Show Gist options
  • Save gledos/497812ca96ac480c99e29ad2f2fab3c4 to your computer and use it in GitHub Desktop.
Save gledos/497812ca96ac480c99e29ad2f2fab3c4 to your computer and use it in GitHub Desktop.
YouTube Share Link Cleaner
// ==UserScript==
// @name YouTube Share Link Cleaner
// @namespace http://tampermonkey.net/
// @version 0.0.1
// @description Cleaner YouTube share links to remove tracking parameters
// @author ChatGPT
// @match https://www.youtube.com/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
// Function to extract video ID and time from the URL
function extractVideoInfo(url) {
const videoIdMatch = url.match(/\/([a-zA-Z0-9_-]{11})/);
const timeMatch = url.match(/&t=(\d+)/);
const videoId = videoIdMatch ? videoIdMatch[1] : '';
const time = timeMatch ? '&t=' + timeMatch[1] : '';
return videoId + time;
}
// Override the copy-to-clipboard function
function overrideCopyFunction() {
const copyButton = document.getElementById('copy-button');
const originalCopyFunction = copyButton.onclick;
copyButton.onclick = function() {
const shareUrlInput = document.getElementById('share-url');
const originalUrl = shareUrlInput.value;
// Extract video information and modify the URL
const modifiedUrl = 'https://youtu.be/' + extractVideoInfo(originalUrl);
// Copy the modified URL to the clipboard
navigator.clipboard.writeText(modifiedUrl)
.then(() => {
console.log('Modified URL copied to clipboard:', modifiedUrl);
})
.catch(error => {
console.error('Error copying modified URL:', error);
});
// Call the original copy function
if (originalCopyFunction) {
originalCopyFunction.apply(this, arguments);
}
};
}
// Wait for the share button to be present and override the copy function
function waitForShareButton() {
const shareButton = document.getElementById('copy-button');
if (shareButton) {
overrideCopyFunction();
} else {
setTimeout(waitForShareButton, 500);
}
}
// Initial setup
waitForShareButton();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment