YouTube Shorts URL 轉換按鈕

將 YouTube Shorts 網址轉換為常規的 YouTube 影片網址。

目前為 2023-10-18 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name YouTube Shorts URL Conversion Button
  3. // @name:zh-TW YouTube Shorts URL 轉換按鈕
  4. // @name:ja YouTube Shorts URL コンバーター
  5. // @name:zh-CN YouTube Shorts URL 转换按钮
  6. // @namespace http://tampermonkey.net/
  7. // @version 1.1
  8. // @description Convert YouTube Shorts URL to regular YouTube video URL.
  9. // @description:zh-TW 將 YouTube Shorts 網址轉換為常規的 YouTube 影片網址。
  10. // @description:ja YouTube Shorts URLを通常のYouTubeビデオURLに変換します。
  11. // @description:zh-CN 将 YouTube Shorts 网址转换为常规的 YouTube 视频网址。
  12. // @author 鮪魚大師
  13. // @match https://www.youtube.com/*
  14. // @grant none
  15. // @license MIT
  16. // ==/UserScript==
  17.  
  18.  
  19. (function() {
  20. 'use strict';
  21.  
  22. let convertButton;
  23.  
  24. function createConvertButton() {
  25. if (!convertButton) {
  26. convertButton = document.createElement('button');
  27. convertButton.textContent = 'Shorts網址轉換';
  28. convertButton.style.position = 'fixed';
  29. convertButton.style.top = '150px';
  30. convertButton.style.right = '10px';
  31. convertButton.style.zIndex = '9999';
  32. convertButton.style.backgroundColor = '#FF0000';
  33. convertButton.style.color = '#FFFFFF';
  34. convertButton.style.fontSize = '24px';
  35. convertButton.style.padding = '10px 20px';
  36. convertButton.style.border = 'none';
  37. convertButton.style.borderRadius = '5px';
  38. convertButton.title = '將Shorts網址轉換成一般影片網址';
  39. document.body.appendChild(convertButton);
  40.  
  41. convertButton.addEventListener('click', convertURL);
  42. }
  43. }
  44.  
  45. function convertURL() {
  46. const currentURL = window.location.href;
  47. if (currentURL.includes("youtube.com/shorts/")) {
  48. const match = currentURL.match(/https:\/\/www\.youtube\.com\/shorts\/([A-Za-z0-9_-]+)/);
  49. if (match) {
  50. const videoID = match[1];
  51. const videoURL = `https://www.youtube.com/watch?v=${videoID}`;
  52. window.location.href = videoURL;
  53. }
  54. }
  55. }
  56.  
  57. function removeConvertButton() {
  58. if (convertButton) {
  59. convertButton.remove();
  60. convertButton = null;
  61. }
  62. }
  63.  
  64. function checkAndCreateButton() {
  65. if (window.location.href.includes("youtube.com/shorts/")) {
  66. createConvertButton();
  67. } else {
  68. removeConvertButton();
  69. }
  70. }
  71.  
  72. checkAndCreateButton();
  73.  
  74. window.addEventListener('popstate', checkAndCreateButton);
  75.  
  76. const observer = new MutationObserver(function(mutationsList, observer) {
  77. for (const mutation of mutationsList) {
  78. if (mutation.type === 'childList') {
  79. checkAndCreateButton();
  80. }
  81. }
  82. });
  83.  
  84. observer.observe(document.documentElement, { childList: true, subtree: true });
  85. })();