YouTube - Add Watch on yewtu.be button to each video

Add "yewtu.be" button YouTube videos

  1. // ==UserScript==
  2. // @name YouTube - Add Watch on yewtu.be button to each video
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1
  5. // @description Add "yewtu.be" button YouTube videos
  6. // @author cam
  7. // @match https://www.youtube.com/*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. // Function to replace 'www.youtube.com' with 'piped.kavin.rocks'
  16. function replaceYouTubeURL(url) {
  17. return url.replace('www.youtube.com', 'yewtu.be');
  18. }
  19.  
  20. // Function to handle the "Watch on Piped" button click
  21. function watchOnPipedButtonClick(event) {
  22. // Get the link to the video
  23. var videoLink = event.currentTarget.parentNode.querySelector('a');
  24.  
  25. // Check if the link exists
  26. if (videoLink) {
  27. // Replace the YouTube URL with the Piped URL
  28. var pipedURL = replaceYouTubeURL(videoLink.href);
  29.  
  30. // Open the Piped URL in a new tab
  31. window.open(pipedURL, '_blank');
  32. }
  33. }
  34.  
  35. // Add "Watch on Piped" button below each specified video
  36. function addWatchOnPipedButton(video) {
  37. // Check if the video container has the correct id and class
  38. if (video.id === 'content' && video.classList.contains('style-scope', 'ytd-rich-item-renderer')) {
  39. // Check if the button already exists
  40. var existingButton = video.querySelector('.watch-on-piped-button');
  41. if (!existingButton) {
  42. // Create a new button element
  43. var button = document.createElement('button');
  44. button.innerText = 'yewtu.be';
  45. button.classList.add('watch-on-piped-button');
  46.  
  47. // Add event listener to handle the click
  48. button.addEventListener('click', watchOnPipedButtonClick);
  49.  
  50. // Append the new button below the video
  51. video.appendChild(button);
  52. }
  53. }
  54. }
  55.  
  56. // Function to handle changes in the DOM
  57. function handleDOMChanges(mutationsList) {
  58. for (const mutation of mutationsList) {
  59. if (mutation.type === 'childList') {
  60. // Check if videos were added to the DOM
  61. const addedVideos = Array.from(document.querySelectorAll('.style-scope.ytd-rich-item-renderer'));
  62.  
  63. // Add the "Watch on Piped" button for each video
  64. addedVideos.forEach(addWatchOnPipedButton);
  65. }
  66. }
  67. }
  68.  
  69. // Observe changes in the DOM to handle dynamically added videos
  70. const observer = new MutationObserver(handleDOMChanges);
  71. observer.observe(document.body, { childList: true, subtree: true });
  72.  
  73. // Add the "Watch on Piped" button for existing videos
  74. const existingVideos = document.querySelectorAll('.style-scope.ytd-rich-item-renderer');
  75. existingVideos.forEach(addWatchOnPipedButton);
  76. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址