Greasy Fork is available in English.

YouTube Selected-Text search.

Search the selected text on YouTube.

  1. // ==UserScript==
  2. // @name YouTube Selected-Text search.
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1
  5. // @grant GM_registerMenuCommand
  6. // @description Search the selected text on YouTube.
  7. // @author ftk789
  8. // @include https://*
  9. // @include http://*
  10. // @icon https://cdn-icons-png.flaticon.com/512/1384/1384060.png
  11. // @grant GM_openInTab
  12. // ==/UserScript==
  13.  
  14. // Chrome has the ability to search the highlighted text on Google when right-clicking a highlighted text, Have you ever wanted to add another option for that but for YouTube?
  15.  
  16. // Instructions:
  17. // highlight text on any page, and press CTRL + Y to search the Highlighted text on YouTube.
  18.  
  19. (function() {
  20. 'use strict';
  21.  
  22. (function() {
  23. 'use strict';
  24. function onAltQ() {
  25. if (event.defaultPrevented ||
  26. /(input|textarea)/i.test(document.activeElement.nodeName)) {
  27. return;
  28. }
  29. searchYouTubeForSelectedText()
  30. }
  31. // Here: By pressing CTRL + Y , it instantly opens a new tab searching on YouTube what you highlighted.
  32. // You can change if you want something other than CTRL or another key instead of Y, You'll have to head to Use https://keycode.info/ To get the KeyCode and replace it with the current one.
  33. function onKeydown(evt) {
  34. // Use https://keycode.info/ to get keys
  35. if (evt.ctrlKey && evt.keyCode == 89) {
  36. onAltQ();
  37. }
  38. }
  39. document.addEventListener('keydown', onKeydown, true);
  40. })();
  41.  
  42.  
  43.  
  44. function searchYouTubeForSelectedText() {
  45. let selectedText = getSelection()
  46. .toString()
  47. .trim()
  48. .replace(/ /g, '+');
  49. if (selectedText) {
  50. window.open("https://www.youtube.com/results?search_query=" + selectedText, '_blank');
  51. // The comment under this comment is when you want to open the page on the current active page instead of opening a new tab.
  52. //GM_openInTab("https://www.youtube.com/results?search_query=" + selectedText, "active")
  53. }
  54. }
  55.  
  56. // Register a button on the Tampermonkey menu.
  57. GM_registerMenuCommand("Search on Youtube", () => {
  58.  
  59.  
  60. if (event.defaultPrevented ||
  61. /(input|textarea)/i.test(document.activeElement.nodeName)) {
  62. return;
  63. }
  64. searchYouTubeForSelectedText()
  65.  
  66. });
  67.  
  68. })();