Copy YouTube Channel ID

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

Verze ze dne 25. 02. 2023. Zobrazit nejnovější verzi.

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

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

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

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

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

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.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

// ==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);