One Click Copy on YouTube

Replaces the YouTube share button with a copy button that copies the shortlinks using a single click

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         One Click Copy on YouTube
// @version      1.2
// @description  Replaces the YouTube share button with a copy button that copies the shortlinks using a single click
// @homepage     https://github.com/SinTan1729/userscripts
// @author       SinTan
// @license      GPL-3.0-only
// @namespace    YouTube
// @icon         https://upload.wikimedia.org/wikipedia/commons/f/fd/YouTube_full-color_icon_%282024%29.svg
// @match        *://*.youtube.com/*
// @grant        GM.setClipboard
// ==/UserScript==

onUrlChange();

// Run the code on each navigation event
if (self.navigation) {
  navigation.addEventListener('navigatesuccess', onUrlChange);
} else {
  let u = location.href;
  new MutationObserver(() => u !== (u = location.href) && onUrlChange())
    .observe(document, {subtree: true, childList: true});
}

function onUrlChange() {
  if (!location.pathname.startsWith('/watch')) {
    // deactivate();
    return;
  }
  console.log('Processing', location.href);
  // activate();
  var intv = setInterval(function() {
    // Wait for the svg element to appear
    const svgs = document.querySelectorAll('#actions yt-button-view-model button[aria-label="Share"] svg');
    if (svgs.length < 1) {
      return false;
    }
    // Change the text
    clearInterval(intv);
    console.log('Replacing the share button with a copy button.');
    const btn = document.querySelector('#actions yt-button-view-model button[aria-label="Share"]');
    // btn.title = 'Copy the URL'; // This is no longer consistent with the rest of the UI
    btn.ariaLabel = 'Copy';
    btn.getElementsByClassName("yt-spec-button-shape-next__button-text-content")[0].innerHTML = 'Copy';
    // Change the icon
    const svg = svgs[0];
    svg.innerHTML = '<path fill-rule="evenodd" d="M21 8a3 3 0 0 0-3-3h-8a3 3 0 0 0-3 3v12a3 3 0 0 0 3 3h8a3 3 0 0 0 3-3V8Zm-2 0a1 1 0 0 0-1-1h-8a1 1 0 0 0-1 1v12a1 1 0 0 0 1 1h8a1 1 0 0 0 1-1V8Z" clip-rule="evenodd"/>';
    svg.innerHTML += '<path d="M6 3h10a1 1 0 1 0 0-2H6a3 3 0 0 0-3 3v14a1 1 0 1 0 2 0V4a1 1 0 0 1 1-1Z"/>';
    // Change the click function
    const urlParams = new URLSearchParams(window.location.search);
    const videoID = urlParams.get('v');
    const url = 'https://youtu.be/' + videoID;
    btn.onclick = function() {
      GM.setClipboard(url, 'text/plain');
      const btn = document.querySelector('#actions yt-button-view-model button[aria-label="Copy"]');
      btn.getElementsByClassName("yt-spec-button-shape-next__button-text-content")[0].innerHTML = "Copied!";
      setTimeout(function() {
        btn.getElementsByClassName("yt-spec-button-shape-next__button-text-content")[0].innerHTML = "Copy"
      }, 1000);
      console.log('Copied video url to clipboard!');
    };
  }, 100);
}