YouTube Short Link Copier

Copy the short YouTube link directly when pressing Shift + S.

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

  1. // ==UserScript==
  2. // @name YouTube Short Link Copier
  3. // @namespace https://greasyfork.org/en/scripts/474134-youtube-short-link-copier
  4. // @version 3.1
  5. // @description Copy the short YouTube link directly when pressing Shift + S.
  6. // @author You
  7. // @match https://www.youtube.com/*
  8. // @match https://music.youtube.com/*
  9. // @grant GM_setClipboard
  10. // @license MIT
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15.  
  16. function getVideoIDFromQuery() {
  17. const match = window.location.search.match(/v=([^&]+)/);
  18. return match ? match[1] : null;
  19. }
  20.  
  21. document.addEventListener('keydown', function(e) {
  22. if (e.key !== 'S' || !e.shiftKey) return;
  23.  
  24. let shortURL;
  25.  
  26. switch (window.location.host) {
  27. case 'www.youtube.com':
  28. if (window.location.pathname.startsWith('/shorts/')) {
  29. shortURL = window.location.href;
  30. } else {
  31. const videoID = getVideoIDFromQuery();
  32. if (videoID) shortURL = `https://youtu.be/${videoID}`;
  33. }
  34. break;
  35. case 'music.youtube.com':
  36. const videoID = getVideoIDFromQuery();
  37. if (videoID) shortURL = `https://music.youtube.com/watch?v=${videoID}`;
  38. break;
  39. }
  40.  
  41. if (shortURL) GM_setClipboard(shortURL);
  42. });
  43. })();
  44.