Greasy Fork is available in English.

YouTube Timestamp URL Copier and Replacer (Shift+s)

A tampermonkey script that can detect and copy the timestamp of the current YouTube video and put it at the end of the URL when activated, and replace the current URL with the contents of your clipboard. Press the keybind again (Shift+s) to remove the old timestamp and replace it with a new one.

// ==UserScript==
// @name         YouTube Timestamp URL Copier and Replacer (Shift+s)
// @version      1.0
// @description  A tampermonkey script that can detect and copy the timestamp of the current YouTube video and put it at the end of the URL when activated, and replace the current URL with the contents of your clipboard. Press the keybind again (Shift+s) to remove the old timestamp and replace it with a new one.
// @icon         https://i.imgur.com/oUCcSbW.png
// @author       Misspent & OpenAI
// @namespace    https://chatbot.theb.ai
// @match        https://www.youtube.com/*
// @grant        GM_setClipboard
// @grant        GM_notification
// @icon         https://www.youtube.com/favicon.ico
// @license MIT
// ==/UserScript==


/* Detects the current timestamp of the youtube video being watched in &t=XXs format, @return {string} Current timestamp in &t=XXs format */
function getCurrentTimestamp() {
  const videoPlayer = document.querySelector('.html5-main-video');
  return videoPlayer ? `&t=${Math.floor(videoPlayer.currentTime)}s` : '';
}

/* Shows a notification message on the page for a short duration, @param {string} message The message to display in the notification */
function showNotification(message) {
  const notification = document.createElement('div');
  notification.textContent = message;
  notification.style.cssText = `
    position: fixed;
    top: 4%;
    right: 5px;
    padding: 8px 16px;
    background-color: green;
    color: #fff;
    font-size: 14px;
    z-index: 9999;
  `;
  document.body.appendChild(notification);

  setTimeout(() => {
    document.body.removeChild(notification);
  }, 4000);
}

let oldUrl;

/* Appends the current timestamp to the url and copies it to clipboard, replacing any existing timestamp */
function handleTimestamp() {
  const currentTimestamp = getCurrentTimestamp();
  if (currentTimestamp) {
    const currentUrl = window.location.href;
    let newUrl;
    if (oldUrl && oldUrl.includes('&t=')) {
      // Replace existing timestamp
      newUrl = currentUrl.replace(oldUrl, `${currentTimestamp}`);
    } else if (currentUrl.includes('&t=')) {
      // Replace existing timestamp at the end of the URL
      const index = currentUrl.lastIndexOf('&t=');
      newUrl = `${currentUrl.substring(0, index)}${currentTimestamp}`;
    } else {
      // Append new timestamp to the end of the URL
      newUrl = `${currentUrl}${currentTimestamp}`;
    }
    GM_setClipboard(newUrl);
    showNotification(`Success! Timestamp copied to clipboard`); // Add: '${currentTimestamp}' If you want it shown in notification
    window.location.href = newUrl;
    oldUrl = currentTimestamp;
  }
}

/* Removes the timestamp from the url and loads the old url */
function removeTimestamp() {
  const currentUrl = window.location.href;
  if (oldUrl && currentUrl.includes(oldUrl)) {
    const newUrl = currentUrl.replace(oldUrl, '');
    window.location.href = newUrl;
    oldUrl = '';
  }
}

// Bind shift+s to handleTimestamp and removeTimestamp functions
document.addEventListener('keydown', (event) => {
        if (event.shiftKey && event.key === 'S' && !isInputElement(document.activeElement)) {
    event.preventDefault();
    if (oldUrl) {
      removeTimestamp();
    } else {
      handleTimestamp();
    }
  }
    function isInputElement(element) {
        const inputTypes = ['input', 'textarea'];
        return (element.nodeName === 'INPUT' || element.nodeName === 'TEXTAREA') || (inputTypes.indexOf(element.nodeName.toLowerCase()) > -1);
    }
});




// Similar to what I wrote to get this result:

// Write a tampermonkey script for youtube that does the following:
// 1. Can detect what the timestamp of the video I'm currently watching is
// 2. Will copy the timestamp and put it at the end of the url and will copy it to clipboard when I press shift+s
// 3. Once it has been copied to my clipboard make it replace the current url with the contents on my clipboard and load the new url
// 4. If I press the keybind again make it remove the old timestamp and replace it with new one
// 5. Will notify me when It succesfully copies the timestamp to clipboard with document.createElement
// 6. Make the @namespace https://chatbot.theb.ai
// 7. Make the @author Misspent & OpenAI
// 8. Give each section/function a brief short description of what it is like you normally do
// I kept telling it to tweak some issue after it gave me the script