YouTube Quick Actions

Adds quick-action buttons like Hide, Save to Playlist, Not Interested, and Don’t Recommend

当前为 2025-05-06 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name YouTube Quick Actions
  3. // @description Adds quick-action buttons like Hide, Save to Playlist, Not Interested, and Don’t Recommend
  4. // @version 1.5.0
  5. // @match https://www.youtube.com/*
  6. // @license Unlicense
  7. // @icon https://www.youtube.com/s/desktop/c722ba88/img/logos/favicon_144x144.png
  8. // @grant GM_addStyle
  9. // @compatible firefox
  10. // @namespace https://greasyfork.org/users/1223791
  11. // ==/UserScript==
  12.  
  13. "use strict";
  14.  
  15. console.log("🫡 [Youtube Quick Actions] Script initialized");
  16.  
  17. const css = String.raw;
  18. const style = css`
  19. #quick-actions {
  20. position: absolute;
  21. display: none;
  22. flex-direction: column;
  23. gap: 0.2em;
  24. align-items: flex-start;
  25. }
  26.  
  27. .location-01 {
  28. top: 0.8em;
  29. left: 0.8em;
  30. }
  31.  
  32. .location-02 {
  33. top: 0.4em;
  34. left: 0.4em;
  35. }
  36.  
  37. .qa-button {
  38. background-color: rgba(0, 0, 0, 0.9);
  39. /* box-shadow: inset 2px 3px 5px #000, 0px 0px 8px #d0d0d02e; */
  40. z-index: 1000;
  41. border: 1px solid #f0f0f05c;
  42. width: 26px;
  43. height: 26px;
  44. display: flex;
  45. justify-content: center;
  46. align-items: center;
  47. color: white;
  48. font-size: 16px;
  49. font-weight: bold;
  50. border-radius: 4px;
  51. cursor: pointer;
  52. flex-shrink: unset;
  53. }
  54.  
  55. .qa-button:hover {
  56. border: 1px solid rgba(255, 255, 255, 0.2);
  57. opacity: 0.9;
  58. background-color: rgba(55, 55, 55, 0.9);
  59. }
  60.  
  61. .qa-icon {
  62. width: 1em;
  63. height: 1em;
  64. vertical-align: -0.125em;
  65. }
  66.  
  67. YTD-RICH-ITEM-RENDERER:hover:not(:has(ytd-rich-grid-media[is-dismissed])):not(:has(.ytDismissibleItemReplacedContent)) #quick-actions,
  68. YTD-COMPACT-VIDEO-RENDERER:hover:not([is-dismissed]):not(:has(#dismissed-content)) #quick-actions,
  69. YTM-SHORTS-LOCKUP-VIEW-MODEL-V2:hover:not(:has(.ytDismissibleItemReplacedContent)) #quick-actions,
  70. YT-LOCKUP-VIEW-MODEL:hover:not(:has(.ytDismissibleItemReplacedContent)) #quick-actions,
  71. YTD-PLAYLIST-VIDEO-RENDERER:hover #quick-actions,
  72. YTD-VIDEO-RENDERER:hover #quick-actions,
  73. YTD-GRID-VIDEO-RENDERER:hover #quick-actions {
  74. display: flex;
  75. }
  76.  
  77. /*
  78. #dismissible:hover:not(:has(ytm-shorts-lockup-view-model-v2)) > #quick-actions {
  79. display: flex;
  80. }
  81. */
  82.  
  83. YT-LOCKUP-VIEW-MODEL:hover:has(#quick-actions),
  84. YTD-PLAYLIST-VIDEO-RENDERER:hover:has(#quick-actions) {
  85. position: relative;
  86. }
  87. `;
  88.  
  89. GM_addStyle(style);
  90.  
  91. /* -------------------------------------------------------------------------- */
  92. /* Variables */
  93. /* -------------------------------------------------------------------------- */
  94.  
  95. const isLoggingEnabled = true;
  96.  
  97. // Elem to search for
  98. const normalVideoTagName = "YTD-RICH-ITEM-RENDERER";
  99. const searchVideoTagName = "YTD-VIDEO-RENDERER";
  100. const gridVideoTagName = "YTD-GRID-VIDEO-RENDERER";
  101. const compactVideoTagName = "YTD-COMPACT-VIDEO-RENDERER";
  102. const shortsV2VideoTagName = "YTM-SHORTS-LOCKUP-VIEW-MODEL-V2";
  103. const shortsVideoTagName = "YTM-SHORTS-LOCKUP-VIEW-MODEL";
  104. const compactPlaylistContainer = "YTD-ITEM-SECTION-RENDERER";
  105. const compactPlaylistSelector = ".yt-lockup-view-model-wiz";
  106. const playlistVideoTagName = "YT-LOCKUP-VIEW-MODEL";
  107. const playlistVideoTagName2 = "YTD-PLAYLIST-VIDEO-RENDERER";
  108. const memberVideoTagName = "YTD-MEMBERSHIP-BADGE-RENDERER";
  109. const memberVideoSelector = ".badge-style-type-members-only";
  110. const thumbnailElementSelector = "img.yt-core-image";
  111. const normalHamburgerMenuSelector = "button#button.style-scope.yt-icon-button";
  112. const shortsAndPlaylistHamburgerMenuSelector = "button.yt-spec-button-shape-next";
  113. const dropdownMenuTagName = "TP-YT-IRON-DROPDOWN";
  114. const popupMenuItemsSelector = "yt-formatted-string.style-scope.ytd-menu-service-item-renderer, yt-list-item-view-model[role='menuitem']";
  115. //Menu Extractions / Properties Path
  116. const searchMenuPropertyPath = "menu.menuRenderer.items";
  117. const gridMenuPropertyPath = "menu.menuRenderer.items";
  118. const shortsMenuPropertyPath = "content.shortsLockupViewModel.menuOnTap.innertubeCommand.showSheetCommand.panelLoadingStrategy.inlineContent.sheetViewModel.content.listViewModel.listItems";
  119. const shortsV2MenuPropertyPath = "menuOnTap.innertubeCommand.showSheetCommand.panelLoadingStrategy.inlineContent.sheetViewModel.content.listViewModel.listItems";
  120. const normalMenuPropertyPath = "content.videoRenderer.menu.menuRenderer.items";
  121. const playlistMenuPropertyPath = "content.lockupViewModel.metadata.lockupMetadataViewModel.menuButton.buttonViewModel.onTap.innertubeCommand.showSheetCommand.panelLoadingStrategy.inlineContent.sheetViewModel.content.listViewModel.listItems";
  122. const playlistMenuPropertyPath2 = "menu.menuRenderer.items";
  123. const compactPlaylistMenuPropertyPath = "metadata.lockupMetadataViewModel.menuButton.buttonViewModel.onTap.innertubeCommand.showSheetCommand.panelLoadingStrategy.inlineContent.sheetViewModel.content.listViewModel.listItems";
  124. const compactMenuPropertyPath = "menu.menuRenderer.items";
  125. const membersOnlyMenuPropertyPath = "content.feedEntryRenderer.item.videoRenderer.menu.menuRenderer.items";
  126. const membersOnlyMenuPropertyPath2 = "content.videoRenderer.menu.menuRenderer.items";
  127. const availableMenuItemsList1 = "listItemViewModel?.title?.content";
  128. const availableMenuItemsList2 = "menuServiceItemRenderer?.text?.runs?.[0]?.text";
  129. const normalVideoRichThumbnailPath = "content?.videoRenderer?.richThumbnail?.movingThumbnailRenderer?.movingThumbnailDetails?.thumbnails?.[0]?.url";
  130. const compactVideoRichThumbnailPath = "richThumbnail?.movingThumbnailRenderer?.movingThumbnailDetails?.thumbnails?.[0]?.url";
  131.  
  132. // <!--!Font Awesome Free 6.7.2 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free Copyright 2025 Fonticons, Inc.-->
  133. const notInterestedIcon = `<svg class="qa-icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" fill="currentColor"><!--!Font Awesome Free 6.7.2 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free Copyright 2025 Fonticons, Inc.--><path d="M256 512A256 256 0 1 0 256 0a256 256 0 1 0 0 512zM159.3 388.7c-2.6 8.4-11.6 13.2-20 10.5s-13.2-11.6-10.5-20C145.2 326.1 196.3 288 256 288s110.8 38.1 127.3 91.3c2.6 8.4-2.1 17.4-10.5 20s-17.4-2.1-20-10.5C340.5 349.4 302.1 320 256 320s-84.5 29.4-96.7 68.7zM144.4 208a32 32 0 1 1 64 0 32 32 0 1 1 -64 0zm192-32a32 32 0 1 1 0 64 32 32 0 1 1 0-64z"/></svg>`;
  134. const frownIcon = `<svg class="qa-icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" fill="currentColor"><!--!Font Awesome Free 6.7.2 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free Copyright 2025 Fonticons, Inc.--><path d="M256 512A256 256 0 1 0 256 0a256 256 0 1 0 0 512zM176.4 176a32 32 0 1 1 0 64 32 32 0 1 1 0-64zm128 32a32 32 0 1 1 64 0 32 32 0 1 1 -64 0zm-122 174.5c-12.4 5.2-26.5-4.1-21.1-16.4c16-36.6 52.4-62.1 94.8-62.1s78.8 25.6 94.8 62.1c5.4 12.3-8.7 21.6-21.1 16.4c-22.4-9.5-47.4-14.8-73.7-14.8s-51.3 5.3-73.7 14.8z"/></svg>`;
  135. const saveIcon = `<svg class="qa-icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" fill="currentColor"><!--!Font Awesome Free 6.7.2 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free Copyright 2025 Fonticons, Inc.--><path d="M512 416c0 35.3-28.7 64-64 64L64 480c-35.3 0-64-28.7-64-64L0 96C0 60.7 28.7 32 64 32l128 0c20.1 0 39.1 9.5 51.2 25.6l19.2 25.6c6 8.1 15.5 12.8 25.6 12.8l160 0c35.3 0 64 28.7 64 64l0 256zM232 376c0 13.3 10.7 24 24 24s24-10.7 24-24l0-64 64 0c13.3 0 24-10.7 24-24s-10.7-24-24-24l-64 0 0-64c0-13.3-10.7-24-24-24s-24 10.7-24 24l0 64-64 0c-13.3 0-24 10.7-24 24s10.7 24 24 24l64 0 0 64z"/></svg>`;
  136. const dontRecommendChannelIcon = `<svg class="qa-icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" fill="currentColor"><!--!Font Awesome Free 6.7.2 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free Copyright 2025 Fonticons, Inc.--><path d="M367.2 412.5L99.5 144.8C77.1 176.1 64 214.5 64 256c0 106 86 192 192 192c41.5 0 79.9-13.1 111.2-35.5zm45.3-45.3C434.9 335.9 448 297.5 448 256c0-106-86-192-192-192c-41.5 0-79.9 13.1-111.2 35.5L412.5 367.2zM0 256a256 256 0 1 1 512 0A256 256 0 1 1 0 256z"/></svg>`;
  137. const hideIcon = `<svg class="qa-icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 640 512" fill="currentColor"><!--!Font Awesome Free 6.7.2 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free Copyright 2025 Fonticons, Inc.--><path d="M38.8 5.1C28.4-3.1 13.3-1.2 5.1 9.2S-1.2 34.7 9.2 42.9l592 464c10.4 8.2 25.5 6.3 33.7-4.1s6.3-25.5-4.1-33.7L525.6 386.7c39.6-40.6 66.4-86.1 79.9-118.4c3.3-7.9 3.3-16.7 0-24.6c-14.9-35.7-46.2-87.7-93-131.1C465.5 68.8 400.8 32 320 32c-68.2 0-125 26.3-169.3 60.8L38.8 5.1zM223.1 149.5C248.6 126.2 282.7 112 320 112c79.5 0 144 64.5 144 144c0 24.9-6.3 48.3-17.4 68.7L408 294.5c8.4-19.3 10.6-41.4 4.8-63.3c-11.1-41.5-47.8-69.4-88.6-71.1c-5.8-.2-9.2 6.1-7.4 11.7c2.1 6.4 3.3 13.2 3.3 20.3c0 10.2-2.4 19.8-6.6 28.3l-90.3-70.8zM373 389.9c-16.4 6.5-34.3 10.1-53 10.1c-79.5 0-144-64.5-144-144c0-6.9 .5-13.6 1.4-20.2L83.1 161.5C60.3 191.2 44 220.8 34.5 243.7c-3.3 7.9-3.3 16.7 0 24.6c14.9 35.7 46.2 87.7 93 131.1C174.5 443.2 239.2 480 320 480c47.8 0 89.9-12.9 126.2-32.5L373 389.9z"/></svg>`;
  138. const pooIcon = `<svg class="qa-icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" fill="currentColor"><!--!Font Awesome Free 6.7.2 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free Copyright 2025 Fonticons, Inc.--><path d="M268.9 .9c-5.5-.7-11 1.4-14.5 5.7s-4.6 10.1-2.8 15.4c2.8 8.2 4.3 16.9 4.3 26.1c0 44.1-35.7 79.9-79.8 80L160 128c-35.3 0-64 28.7-64 64c0 19.1 8.4 36.3 21.7 48L104 240c-39.8 0-72 32.2-72 72c0 23.2 11 43.8 28 57c-34.1 5.7-60 35.3-60 71c0 39.8 32.2 72 72 72l368 0c39.8 0 72-32.2 72-72c0-35.7-25.9-65.3-60-71c17-13.2 28-33.8 28-57c0-39.8-32.2-72-72-72l-13.7 0c13.3-11.7 21.7-28.9 21.7-48c0-35.3-28.7-64-64-64l-5.5 0c3.5-10 5.5-20.8 5.5-32c0-48.6-36.2-88.8-83.1-95.1zM192 256a32 32 0 1 1 0 64 32 32 0 1 1 0-64zm96 32a32 32 0 1 1 64 0 32 32 0 1 1 -64 0zm64 108.3c0 2.4-.7 4.8-2.2 6.7c-8.2 10.5-39.5 45-93.8 45s-85.6-34.6-93.8-45c-1.5-1.9-2.2-4.3-2.2-6.7c0-6.8 5.5-12.3 12.3-12.3l167.4 0c6.8 0 12.3 5.5 12.3 12.3z"/></svg>`;
  139. const mehIcon = `<svg class="qa-icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" fill="currentColor"><!--!Font Awesome Free 6.7.2 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free Copyright 2025 Fonticons, Inc.--><path d="M256 512A256 256 0 1 0 256 0a256 256 0 1 0 0 512zM176.4 176a32 32 0 1 1 0 64 32 32 0 1 1 0-64zm128 32a32 32 0 1 1 64 0 32 32 0 1 1 -64 0zM160 336l192 0c8.8 0 16 7.2 16 16s-7.2 16-16 16l-192 0c-8.8 0-16-7.2-16-16s7.2-16 16-16z"/></svg>`;
  140. const trashIcon = `<svg class="qa-icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512" fill="currentColor"><!--!Font Awesome Free 6.7.2 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free Copyright 2025 Fonticons, Inc.--><path d="M135.2 17.7C140.6 6.8 151.7 0 163.8 0L284.2 0c12.1 0 23.2 6.8 28.6 17.7L320 32l96 0c17.7 0 32 14.3 32 32s-14.3 32-32 32L32 96C14.3 96 0 81.7 0 64S14.3 32 32 32l96 0 7.2-14.3zM32 128l384 0 0 320c0 35.3-28.7 64-64 64L96 512c-35.3 0-64-28.7-64-64l0-320zm96 64c-8.8 0-16 7.2-16 16l0 224c0 8.8 7.2 16 16 16s16-7.2 16-16l0-224c0-8.8-7.2-16-16-16zm96 0c-8.8 0-16 7.2-16 16l0 224c0 8.8 7.2 16 16 16s16-7.2 16-16l0-224c0-8.8-7.2-16-16-16zm96 0c-8.8 0-16 7.2-16 16l0 224c0 8.8 7.2 16 16 16s16-7.2 16-16l0-224c0-8.8-7.2-16-16-16z"/></svg>`;
  141.  
  142.  
  143. /* -------------------------------------------------------------------------- */
  144. /* Functions */
  145. /* -------------------------------------------------------------------------- */
  146.  
  147. function log(...args)
  148. {
  149. if (isLoggingEnabled)
  150. {
  151. console.log(...args);
  152. }
  153. }
  154.  
  155.  
  156. function getByPathReduce(target, path)
  157. {
  158. return path.split('.').reduce((result, key) => result?.[key], target) ?? [];
  159. }
  160.  
  161. //Same result as getByPathReduce()
  162. function getByPathFunction(object, path)
  163. {
  164. try
  165. {
  166. return new Function('object', `return object.${path}`)(object) ?? [];
  167. } catch
  168. {
  169. return [];
  170. }
  171. }
  172.  
  173. function getDataProperty(origin, videoType)
  174. {
  175. const childQuerySelectors = {
  176. "shorts-v2": shortsVideoTagName,
  177. "compact-playlist": compactPlaylistSelector,
  178. };
  179. const selector = childQuerySelectors[videoType];
  180. const target = selector ? origin.querySelector(selector) : origin;
  181. return target?.data;
  182. }
  183.  
  184. function getMenuList(target)
  185. {
  186. return target.map(item =>
  187. {
  188. const first = getByPathFunction(item, availableMenuItemsList1);
  189. if (first.length) return first;
  190.  
  191. const second = getByPathFunction(item, availableMenuItemsList2);
  192. if (second.length) return second;
  193.  
  194. return null;
  195. }).filter(Boolean);
  196. }
  197.  
  198. function findElemInParentDomTree(originElem, targetSelector)
  199. {
  200. log(`🔍 Starting search from:`, originElem);
  201.  
  202. let node = originElem;
  203. while (node)
  204. {
  205. log(`👆 Checking ancestor:`, node);
  206. const found = Array.from(node.children).find(
  207. (child) => child.matches(targetSelector) || child.querySelector(targetSelector)
  208. );
  209.  
  210. if (found)
  211. {
  212. const result = found.matches(targetSelector) ? found : found.querySelector(targetSelector);
  213. log(`✅ Found target:`, result);
  214. return result;
  215. }
  216.  
  217. node = node.parentElement;
  218. }
  219.  
  220. log("⚠️ No matching element found.");
  221. return null;
  222. }
  223.  
  224. function getVisibleElem(targetSelector)
  225. {
  226. const elements = document.querySelectorAll(targetSelector);
  227. for (const element of elements)
  228. {
  229. const rect = element.getBoundingClientRect();
  230. if (element.offsetParent !== null && rect.width > 0 && rect.height > 0)
  231. {
  232. log("👀 Menu is visible and ready:", element);
  233. return element;
  234. }
  235. }
  236. log("⚠️ No visible menu found.");
  237. return null;
  238. }
  239.  
  240. async function waitUntil(conditionFunction, { interval = 100, timeout = 3000 } = {})
  241. {
  242. const startTime = Date.now();
  243. while (Date.now() - startTime < timeout)
  244. {
  245. const result = conditionFunction();
  246. if (result) return result;
  247. await new Promise((resolve) => setTimeout(resolve, interval));
  248. }
  249. throw new Error("⏰ Timeout: Target element is not visible in time");
  250. }
  251.  
  252. function retryClick(element, { maxAttempts = 5, interval = 300 } = {})
  253. {
  254. return new Promise((resolve) =>
  255. {
  256. let attempts = 0;
  257.  
  258. function tryClick()
  259. {
  260. if (!element || attempts >= maxAttempts)
  261. {
  262. log("⚠️ Retry failed or element missing.");
  263. return resolve();
  264. }
  265.  
  266. const rect = element.getBoundingClientRect();
  267. const isVisible = rect.width > 0 && rect.height > 0;
  268.  
  269. if (isVisible)
  270. {
  271. element.dispatchEvent(
  272. new MouseEvent("click", {
  273. view: document.defaultView,
  274. bubbles: true,
  275. cancelable: true,
  276. }),
  277. );
  278. log("👇 Clicked matching menu item");
  279. return resolve();
  280. } else
  281. {
  282. attempts++;
  283. setTimeout(tryClick, interval);
  284. }
  285. }
  286.  
  287. tryClick();
  288. });
  289. }
  290.  
  291.  
  292. function appendButtons(element, menuItems, type, position)
  293. {
  294. let className, titleText, icon;
  295. let buttonsToAppend = [];
  296.  
  297. const finalMenuItems = [...new Set(menuItems)];
  298.  
  299. //If menu is empty, proceed and still append the container to prevent looping of menu data probe.
  300. //Probe will only skip if #quick-action exist.
  301.  
  302. for (const item of finalMenuItems)
  303. {
  304. if (!item) continue;
  305.  
  306. let className;
  307. let titleText;
  308. let icon;
  309.  
  310. if (item.startsWith("Remove from "))
  311. {
  312. className = "remove";
  313. titleText = "Remove from playlist";
  314. icon = trashIcon;
  315. } else
  316. {
  317. switch (item)
  318. {
  319. case "Not interested":
  320. className = "not_interested";
  321. titleText = "Not interested";
  322. icon = notInterestedIcon;
  323. break;
  324. case "Don't recommend channel":
  325. className = "dont_recommend_channel";
  326. titleText = "Don't recommend channel";
  327. icon = dontRecommendChannelIcon;
  328. break;
  329. case "Hide":
  330. className = "hide";
  331. titleText = "Hide video";
  332. icon = hideIcon;
  333. break;
  334. case "Save to playlist":
  335. className = "save";
  336. titleText = "Save to playlist";
  337. icon = saveIcon;
  338. break;
  339. default:
  340. continue;
  341. }
  342. }
  343.  
  344. buttonsToAppend.push(
  345. `<button class="qa-button ${className}" data-icon="${className}" title="${titleText}" data-text="${titleText}">${icon}</button>`,
  346. );
  347. }
  348.  
  349. const buttonsContainer = document.createElement("div");
  350. buttonsContainer.id = "quick-actions";
  351. buttonsContainer.classList.add(position, type);
  352. buttonsContainer.innerHTML = buttonsToAppend.join("");
  353.  
  354. //element.insertAdjacentElement("afterend", buttonsContainer);
  355. const exist = element.querySelector("#quick-actions");
  356. if (exist) return;
  357. element.insertAdjacentElement("beforeend", buttonsContainer);
  358. }
  359.  
  360. /* -------------------------------------------------------------------------- */
  361. /* Listeners */
  362. /* -------------------------------------------------------------------------- */
  363.  
  364. // Remove all existing quick-action elements. On certain pages, like channel tabs, content is updated in-place
  365. // without removing the grid/container. If not cleared, old quick-action buttons will remain attached to unrelated items.
  366. // This ensures that if the content is updated, new hover actions will fetch fresh, relevant data.
  367. // I have not take a closer look at yt-made events. propably have some things we can customized and fire to speed things up
  368. // skip querying and fired the action straight up via their internal events
  369. document.addEventListener("yt-action", (event) =>
  370. {
  371. if (event.detail.actionName === "ytd-update-grid-state-action")
  372. {
  373. log("🐛 Page updated.");
  374. document.querySelectorAll("#quick-actions").forEach((element) => element.remove());
  375. }
  376.  
  377. });
  378.  
  379. document.addEventListener("mouseover", (event) =>
  380. {
  381. const path = event.composedPath();
  382. for (let element of path)
  383. {
  384. if (
  385. (element.tagName === normalVideoTagName ||
  386. element.tagName === compactVideoTagName ||
  387. element.tagName === shortsV2VideoTagName ||
  388. element.tagName === searchVideoTagName ||
  389. element.tagName === gridVideoTagName ||
  390. element.tagName === playlistVideoTagName ||
  391. element.tagName === playlistVideoTagName2) &&
  392. !element.querySelector("#quick-actions")
  393. )
  394. {
  395. let type, data;
  396.  
  397. // Determine element type
  398. // Hierarchy might need tweaking to simplify detection. nah this whole listener block,
  399. // cause i'm already confused which tag is needed for which video, what need extra query, then which path
  400. // and specific video type wont get shown unless specific step is done, even then rarely replicable to debug
  401. // some of this type no longer valid as i go, cause i can't keep track no more
  402. if (element.tagName === shortsV2VideoTagName)
  403. {
  404. type = "shorts-v2";
  405. }
  406. else if (element.tagName === playlistVideoTagName && element.parentElement.parentElement.tagName === compactPlaylistContainer)
  407. {
  408. type = "compact-playlist";
  409. }
  410. else if (element.tagName === gridVideoTagName)
  411. {
  412. type = "grid-video";
  413. }
  414. else if (element.tagName === searchVideoTagName)
  415. {
  416. type = "search-video";
  417. }
  418. else if (element.tagName === playlistVideoTagName && element.parentElement.parentElement.tagName === normalVideoTagName)
  419. {
  420. //hover listener will land on playlistVideoTagName instead of normalVideoTagName for playlist/mixes on homepage
  421. //so manually change back to normalVideoTagName as data is there.
  422. element = element.parentElement.parentElement;
  423. if (element.querySelector("#quick-actions")) return;
  424. type = "playlist";
  425. }
  426. else if (element.tagName === playlistVideoTagName2)
  427. {
  428. type = "playlist2";
  429. }
  430. else
  431. {
  432. const isShort = element.querySelector(shortsVideoTagName) !== null;
  433. const isPlaylist = element.querySelector(playlistVideoTagName) !== null;
  434. const isMemberOnly =
  435. element.querySelector(memberVideoTagName) !== null ||
  436. element.querySelector(memberVideoSelector) !== null;
  437.  
  438. type = isShort ? "shorts" :
  439. element.tagName === compactVideoTagName ? "compact" :
  440. isPlaylist ? "collection" :
  441. isMemberOnly ? "members_only" : //TODO: Doesn't come by often to debug so no idea if it is working.
  442. "normal";
  443. }
  444.  
  445. log("⭐ Video Elem: ", element.tagName, element);
  446. log("ℹ️ Video Type: ", type);
  447.  
  448. data = getDataProperty(element, type);
  449. const thumbnailElement = element.querySelector(thumbnailElementSelector);
  450. const thumbnailSize =
  451. thumbnailElement?.getClientRects?.().length > 0
  452. ? parseInt(thumbnailElement.getClientRects()[0].width)
  453. : 100;
  454. log("🖼️ Thumbnail Size: ", thumbnailSize);
  455. const containerPosition = thumbnailSize < 211 ? "location-02" : "location-01";
  456.  
  457. if (!data)
  458. {
  459. log("⚠️ No props data found.");
  460. return;
  461. }
  462.  
  463. log("🎥 Video Props: ", data);
  464.  
  465. // Process menus based on video type
  466. let menulist;
  467. switch (type)
  468. {
  469. case "normal":
  470. menulist = getByPathFunction(data, normalMenuPropertyPath);
  471. break;
  472. case "search-video":
  473. menulist = getByPathFunction(data, searchMenuPropertyPath);
  474. break;
  475. case "grid-video":
  476. menulist = getByPathFunction(data, gridMenuPropertyPath);
  477. break;
  478. case "shorts":
  479. menulist = getByPathFunction(data, shortsMenuPropertyPath);
  480. break;
  481. case "shorts-v2":
  482. menulist = getByPathFunction(data, shortsV2MenuPropertyPath);
  483. break;
  484. case "compact":
  485. menulist = getByPathFunction(data, compactMenuPropertyPath);
  486. break;
  487. case "collection":
  488. menulist = getByPathFunction(data, playlistMenuPropertyPath);
  489. break;
  490. case "playlist":
  491. menulist = getByPathFunction(data, playlistMenuPropertyPath);
  492. break;
  493. case "playlist2":
  494. menulist = getByPathFunction(data, playlistMenuPropertyPath2);
  495. break;
  496. case "compact-playlist":
  497. menulist = getByPathFunction(data, compactPlaylistMenuPropertyPath);
  498. break;
  499. case "members_only":
  500. menulist = getByPathFunction(data, membersOnlyMenuPropertyPath);
  501. if (!menulist.length)
  502. {
  503. menulist = getByPathFunction(data, membersOnlyMenuPropertyPath2);
  504. }
  505. break;
  506. default:
  507. menulist = getByPathFunction(data, normalMenuPropertyPath);
  508. break;
  509. }
  510.  
  511.  
  512. const menulistItems = getMenuList(menulist);
  513. log("📃 Menu items: ", menulistItems);
  514. appendButtons(element, menulistItems, type, containerPosition);
  515.  
  516. // Rich Thumbnail
  517. const normalRichThumbnail = getByPathFunction(data, normalVideoRichThumbnailPath);
  518. const compactRichThumbnail = getByPathFunction(data, compactVideoRichThumbnailPath);
  519. const richThumbnail =
  520. (typeof normalRichThumbnail === 'string' && normalRichThumbnail) ||
  521. (typeof compactRichThumbnail === 'string' && compactRichThumbnail) ||
  522. undefined;
  523. const thumbnailImageOriginal = thumbnailElement.src;
  524. if (thumbnailElement && richThumbnail)
  525. {
  526. thumbnailElement.addEventListener('mouseenter', () =>
  527. {
  528. thumbnailElement.src = richThumbnail;
  529. });
  530.  
  531. thumbnailElement.addEventListener('mouseleave', () =>
  532. {
  533. thumbnailElement.src = thumbnailImageOriginal;
  534. });
  535. }
  536. }
  537. }
  538. }, true);
  539.  
  540. document.addEventListener("click", async function (event)
  541. {
  542. const button = event.target.closest(".qa-button");
  543. if (!button) return;
  544.  
  545. event.stopPropagation();
  546. event.stopImmediatePropagation();
  547. event.preventDefault();
  548.  
  549. const actionType = button.dataset.icon;
  550. let response;
  551.  
  552. switch (actionType)
  553. {
  554. case "not_interested":
  555. response = "Not interested";
  556. log("😴 Marking as not interested");
  557. break;
  558. case "dont_recommend_channel":
  559. response = "Don't recommend channel";
  560. log("🚫 Don't recommend channel");
  561. break;
  562. case "hide":
  563. response = "Hide";
  564. log("🗑️ Hiding video");
  565. break;
  566. case "remove":
  567. response = "Remove from";
  568. log("🗑️ Remove from playlist");
  569. break;
  570. case "save":
  571. response = "Save to playlist";
  572. log("📂 Saving to playlist");
  573. break;
  574. default:
  575. log("☠️ Unknown action");
  576. }
  577.  
  578. let menupath;
  579.  
  580. if (button.parentElement.parentElement.tagName === shortsV2VideoTagName || button.parentElement.parentElement.querySelector(playlistVideoTagName))
  581. {
  582. menupath = shortsAndPlaylistHamburgerMenuSelector;
  583. }
  584. else if (button.parentElement.classList.contains("shorts"))
  585. {
  586. //shorts but not inside shortsv2 container idk where i found this its gone now crazy i was crazy once
  587. alert("shorts!");
  588. menupath = shortsAndPlaylistHamburgerMenuSelector;
  589. }
  590. else if (button.parentElement.classList.contains("compact-playlist"))
  591. {
  592. menupath = shortsAndPlaylistHamburgerMenuSelector;
  593. }
  594. else
  595. {
  596. menupath = normalHamburgerMenuSelector;
  597. }
  598.  
  599. const menus = findElemInParentDomTree(button, menupath);
  600. if (!menus)
  601. {
  602. log("❌ Menu button not found.");
  603. return;
  604. }
  605.  
  606. menus.dispatchEvent(new MouseEvent("click", { bubbles: true }));
  607. log("👇 Button clicked, waiting for menu...");
  608.  
  609. try
  610. {
  611. const visibleMenu = await waitUntil(() => getVisibleElem(dropdownMenuTagName), {
  612. interval: 100,
  613. timeout: 3000,
  614. });
  615. if (visibleMenu)
  616. {
  617. try
  618. {
  619. const targetItem = await waitUntil(
  620. () =>
  621. {
  622. const items = visibleMenu.querySelectorAll(popupMenuItemsSelector);
  623. return items.length > 0 ? items : null;
  624. },
  625. {
  626. interval: 100,
  627. timeout: 5000,
  628. },
  629. );
  630.  
  631. if (targetItem)
  632. {
  633. log("🎉 Target items found:", targetItem);
  634.  
  635. for (const item of targetItem)
  636. {
  637. if (
  638. item.textContent === response ||
  639. (response === "Remove from" && item.textContent.startsWith("Remove from"))
  640. )
  641. {
  642. log(`✅ Matched: (${response} = ${item.textContent})`);
  643. log(`✅`, item);
  644.  
  645. const button = item;
  646. await retryClick(button, { maxAttempts: 5, interval: 300 }).finally(() =>
  647. {
  648. document.body.click();
  649. });
  650. break;
  651. } else
  652. {
  653. log(`❌ Not a match: (${response} = ${item.textContent})`);
  654. }
  655. }
  656. }
  657. } catch (error)
  658. {
  659. log("🛑 !", error.message);
  660. //document.body.click()
  661. }
  662. }
  663.  
  664. //setTimeout(() => document.body.click(), 200);
  665. } catch (error)
  666. {
  667. log("🛑 !!", error.message);
  668. //document.body.click()
  669. }
  670. });