Copy YouTube Channel ID

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

当前为 2023-02-25 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

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