Copy YouTube Channel ID

Adds a 'Copy Channel ID' button to the top of YouTube channels.

נכון ליום 25-02-2023. ראה הגרסה האחרונה.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name     Copy YouTube Channel ID
// @description Adds a 'Copy Channel ID' button to the top of YouTube channels.
// @version  1.1
// @match    https://*.youtube.com/@*
// @match    https://www.youtube.com/@*
// @match    https://youtube.com/@*
// @match    https://www.youtube.com/@
// @match    https://www.youtube.com/c/*
// @grant    GM_setClipboard
// @grant    GM_registerMenuCommand
// @icon     https://www.google.com/s2/favicons?domain=youtube.com
// @namespace https://github.com/Vandekieft/MonkeyScripts/
// @license  MIT
// ==/UserScript==

// Define a function to copy the channel ID to the clipboard
function copyChannelId() {
  var justTheId = getChannelId();
  var textarea = document.createElement("textarea");
  textarea.value = justTheId;
  document.body.appendChild(textarea);
  textarea.select();
  var range = document.createRange();
  range.selectNodeContents(textarea);
  var selection = window.getSelection();
  selection.removeAllRanges();
  selection.addRange(range);
  document.execCommand("copy");
  selection.removeAllRanges(); // Clear the current selection
  document.body.removeChild(textarea); // Remove the temporary textarea element
}

// Define a function to copy the channel URL to the clipboard
function copyChannelUrl() {
  var justTheId = getChannelId();
  var channelUrl = "http://www.youtube.com/channel/" + justTheId;

  // Copy the URL to the clipboard
  GM_setClipboard(channelUrl);
}

// Register a menu command to copy the channel URL
GM_registerMenuCommand("Copy Channel URL", copyChannelUrl);

// Define a function to redirect to the channel URL
function redirectToChannel() {
  var justTheId = getChannelId();
  if (justTheId) {
    window.location.replace("http://www.youtube.com/channel/" + justTheId);
  }
}

// Register a menu command to redirect to the channel URL
GM_registerMenuCommand("Go to Channel URL", redirectToChannel);

// Register a function to get the channel ID
function getChannelId() {
  var channelUrl = window.location.href;
  var justTheId;

  if (channelUrl.indexOf("youtube.com/channel/") !== -1) {
    // Extract the channel ID from the channel URL
    justTheId = channelUrl.slice(channelUrl.indexOf("youtube.com/channel/") + 20);
  } else {
    // Parse the HTML to extract the channel ID
    var entirePage = document.getElementsByTagName("html")[0].innerHTML;
    var restOfThePage = entirePage.slice(entirePage.indexOf("},\"channelUrl\"") + 48);
    justTheId = restOfThePage.slice(0, restOfThePage.indexOf("\""));
  }

  return justTheId;
}

// Listen for changes to the URL using MutationObserver
var observer = new MutationObserver(function(mutations) {
  mutations.forEach(function(mutation) {
    if (mutation.type === 'attributes' && mutation.attributeName === 'href') {
      // The URL has changed, so update the channel ID
      var justTheId = getChannelId();
    }
  });
});

// Observe changes to the href attribute of the <a> tag containing the channel name
var targetNode = document.querySelector('#text > a');
var config = { attributes: true, childList: false, subtree: false };
observer.observe(targetNode, config);