Creating YouTube Shortlinks without Tracking Parameters Using a Bookmarklet
Sharing YouTube video links is a common practice across the internet, whether it’s via email, messaging apps, or social media platforms. However, YouTube links often come laden with tracking parameters and other unnecessary information that clutter the URL, making it longer than needed. Fortunately, there’s a nifty workaround that allows you to create clean, short YouTube URLs without tracking parameters using a simple JavaScript bookmarklet.
A bookmarklet is a small JavaScript code stored as a bookmark in your browser. It executes a particular function when clicked, and in this case, it transforms a lengthy YouTube video URL into a short, neat version. Let’s dive into how you can create a bookmarklet to do just that.
The JavaScript Snippet Explained
javascript:(function(){
let url = window.location.href;
let regexp = /^(https?:\/\/)?(www\.)?youtube\.com\/watch\?v=([^&]+)/;
let match = url.match(regexp);
if(match && match[3]){
let shortUrl = `https://youtu.be/${match[3]}`;
navigator.clipboard.writeText(shortUrl).then(function() {
alert('Short URL copied to clipboard: ' + shortUrl);
}, function(err) {
console.error('Could not copy text: ', err);
});
} else {
alert('Not a YouTube video URL or already a short link.');
}
})();
The script starts by grabbing the current page’s URL. It then uses a regular expression to check if the URL is a YouTube video link and extracts the video ID. If successful, it constructs a new shortened URL using the “youtu.be” domain followed by the video ID. This new URL is then copied to your clipboard, and a confirmation alert is shown. If the URL doesn’t match a YouTube video link or is already a short link, it alerts the user accordingly.
How to Create and Use the Bookmarklet
-
Create the Bookmarklet: To create the bookmarklet, simply right-click your bookmarks bar in your web browser and select “Add page”, “New Bookmark”, or whatever option your browser provides for adding a new bookmark.
-
Name Your Bookmarklet: In the name field, you can enter something descriptive, such as “YouTube Shortlink”.
-
Add the JavaScript Code: In the URL field, paste the entire JavaScript snippet provided above. Make sure it starts with
javascript:
which is crucial for the browser to recognize it as a bookmarklet. -
Using the Bookmarklet: Whenever you’re on a YouTube video page and you want to generate a shortlink without any tracking parameters, simply click the bookmarklet in your bookmarks bar. The script will execute, and if successful, you will get an alert confirming the short URL has been copied to your clipboard.