Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name YT Not Interested Text Option (Homepage + Sidebar)
- // @description Adds "Don't Like This" and "Already Watched" buttons to YouTube homepage and sidebar videos.
- // @version 1.1
- // @match https://www.youtube.com/*
- // @noframes
- // @grant none
- // @author wOxxOm (modified by quetzalcoatl)
- // @namespace wOxxOm.scripts
- // @license MIT License
- // ==/UserScript==
- 'use strict';
- console.log('[Script: Youtube 1-Click Not-Interested] Started...');
- const HOME_CUE = 'ytd-rich-item-renderer'; // Homepage video cue
- const SIDEBAR_CUE = 'ytd-compact-video-renderer'; // Sidebar video cue
- const CHECK_CUE_FREQUENCY = 2000; // How often to check for the cue
- const REPEAT_EVERY = 0; // How often to repeat doStuff; 0 for no repeat
- const CONCURRENCY_DELAY = 100;
- // Add necessary styling
- const styleSheet = document.createElement('style');
- styleSheet.type = 'text/css';
- styleSheet.innerText = `
- #metadata-line a {
- filter: brightness(85%);
- }
- `;
- document.head.appendChild(styleSheet);
- // This function sets the visibility attribute of the popups
- function setPopupVisibility(visible) {
- const popup = document.getElementsByTagName('ytd-popup-container')[0];
- popup.style.visibility = visible ? '' : 'hidden';
- }
- /* This function marks videos as i-dont-like or already-watched
- *
- * if iDontLike is set, the reason will be chosen accordingly. Otherwise,
- * reason will be chosen as 'I have already watched this video'.
- */
- function notInterested(videoBlock, iDontLike = false) {
- console.log('[Script: Youtube 1-Click Not-Interested] Processed:', videoBlock);
- // Set the popups to be invisible (this is temporary; we'll change it back later)
- setPopupVisibility(false);
- // Click the ellipsis to bring up the menu
- const menuEllipsis = videoBlock.getElementsByTagName('yt-icon-button')[0].getElementsByTagName('button')[0];
- menuEllipsis.click();
- // Wait a moment and click on 'Not Interested'
- setTimeout(() => {
- const menu = document.getElementsByTagName('ytd-menu-popup-renderer')[0];
- const notInterested = menu.getElementsByTagName('ytd-menu-service-item-renderer')[4];
- notInterested.click();
- // Wait a moment and select 'Tell Us Why'
- setTimeout(() => {
- const tellUsWhyButton = videoBlock.getElementsByTagName('ytd-button-renderer')[1]
- .getElementsByTagName('button')[0];
- tellUsWhyButton.click();
- // Wait a moment and choose the appropriate reason
- setTimeout(() => {
- const reasonPopup = document.getElementsByTagName('ytd-dismissal-follow-up-renderer')[0];
- const reasonCheckboxes = reasonPopup.getElementsByTagName('tp-yt-paper-checkbox');
- const iAlreadyWatchedCheckbox = reasonCheckboxes[0];
- const iDontLikeCheckbox = reasonCheckboxes[1];
- const submitButton = reasonPopup.getElementsByTagName('ytd-button-renderer')[1];
- // Click the appropriate checkbox
- iDontLike ? iDontLikeCheckbox.click() : iAlreadyWatchedCheckbox.click();
- // Click Submit
- submitButton.click();
- // Change the popups to visible again
- setPopupVisibility(true);
- }, CONCURRENCY_DELAY);
- }, CONCURRENCY_DELAY);
- }, CONCURRENCY_DELAY);
- }
- // The actual stuff; this function adds the new elements to the page
- function doStuff() {
- console.log('[Script: Youtube 1-Click Not-Interested] invoked main function');
- // Disable Trusted Types: stackoverflow.com/questions/62810553
- if (window.trustedTypes && window.trustedTypes.createPolicy) {
- window.trustedTypes.createPolicy('default', {
- createHTML: (string, sink) => string,
- });
- }
- // Find the blocks/thumbnails of videos on the homepage and sidebar
- const homepageVideoBlocks = [...document.getElementsByTagName(HOME_CUE)];
- const sidebarVideoBlocks = [...document.getElementsByTagName(SIDEBAR_CUE)];
- const videoBlocks = [...homepageVideoBlocks, ...sidebarVideoBlocks];
- // For every thumbnail...
- videoBlocks.forEach((videoBlock) => {
- // Find the line that says "X views, Y days ago"
- let metadataLine = videoBlock.querySelector('#metadata-line');
- // Check if the buttons have already been added
- const buttonsAlreadyAdded = videoBlock.querySelector('.custom-not-interested-buttons');
- if (metadataLine && !buttonsAlreadyAdded) {
- // Create the new div
- const newDiv = document.createElement('div');
- newDiv.className = 'custom-not-interested-buttons'; // Add a unique class
- newDiv.innerHTML = `
- <div id="metadata-line" class="style-scope ytd-video-meta-block">
- <a class="yt-simple-endpoint style-scope yt-formatted-string" href="javascript:void(0)">Don't Like This</a>
- <span style="padding: 0px 5px 0px 5px;">•</span>
- <a class="yt-simple-endpoint style-scope yt-formatted-string" href="javascript:void(0)">Already Watched</a>
- </div>
- `;
- // Insert the new div just below the metadata line we found
- metadataLine.parentNode.insertBefore(newDiv, null);
- // Attach onClick functions to both the links and set iDontLike parameter accordingly
- newDiv.getElementsByTagName('a')[0].onclick = () => notInterested(videoBlock, true);
- newDiv.getElementsByTagName('a')[1].onclick = () => notInterested(videoBlock);
- }
- });
- }
- // Function to initialize the MutationObserver
- function initMutationObserver() {
- const observer = new MutationObserver((mutationsList) => {
- for (let mutation of mutationsList) {
- if (mutation.type === 'childList' && mutation.addedNodes.length > 0) {
- // Check if any of the added nodes are video blocks
- const addedVideos = [...mutation.addedNodes].filter(
- (node) =>
- node.tagName === 'YTD-RICH-ITEM-RENDERER' ||
- node.tagName === 'YTD-COMPACT-VIDEO-RENDERER'
- );
- if (addedVideos.length > 0) {
- console.log('[Script: Youtube 1-Click Not-Interested] New videos detected, running doStuff...');
- doStuff();
- }
- }
- }
- });
- // Start observing the homepage and sidebar containers
- const homepageContainer = document.querySelector('ytd-rich-grid-renderer');
- const sidebarContainer = document.querySelector('ytd-watch-next-secondary-results-renderer');
- if (homepageContainer) {
- observer.observe(homepageContainer, { childList: true, subtree: true });
- console.log('[Script: Youtube 1-Click Not-Interested] Homepage MutationObserver initialized.');
- }
- if (sidebarContainer) {
- observer.observe(sidebarContainer, { childList: true, subtree: true });
- console.log('[Script: Youtube 1-Click Not-Interested] Sidebar MutationObserver initialized.');
- }
- }
- // Wait for the initial load of videos
- let waitTillLoad = setInterval(function () {
- if (document.querySelector(HOME_CUE) || document.querySelector(SIDEBAR_CUE)) {
- clearInterval(waitTillLoad);
- doStuff();
- initMutationObserver();
- if (REPEAT_EVERY) setInterval(doStuff, REPEAT_EVERY);
- }
- }, CHECK_CUE_FREQUENCY);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement