YouTube Selected-Text search.

Search the selected text on YouTube.

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         YouTube Selected-Text search.
// @namespace    http://tampermonkey.net/
// @version      0.1
// @grant        GM_registerMenuCommand
// @description  Search the selected text on YouTube.
// @author       ftk789
// @include      https://*
// @include      http://*
// @icon         https://cdn-icons-png.flaticon.com/512/1384/1384060.png
// @grant        GM_openInTab
// ==/UserScript==

// 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?

// Instructions:
//   highlight text on any page, and press CTRL + Y to search the Highlighted text on YouTube.

(function() {
  'use strict';

    (function() {
    'use strict';
    function onAltQ() {
     if (event.defaultPrevented ||
        /(input|textarea)/i.test(document.activeElement.nodeName)) {
        return;
      }
    searchYouTubeForSelectedText()
    }
        // Here: By pressing CTRL + Y , it instantly opens a new tab searching on YouTube what you highlighted.
        // 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.
    function onKeydown(evt) {
        // Use https://keycode.info/ to get keys
        if (evt.ctrlKey && evt.keyCode == 89) {
            onAltQ();
        }
    }
    document.addEventListener('keydown', onKeydown, true);
})();



  function searchYouTubeForSelectedText() {
    let selectedText = getSelection()
      .toString()
      .trim()
      .replace(/ /g, '+');
    if (selectedText) {
        window.open("https://www.youtube.com/results?search_query=" + selectedText, '_blank');
        // The comment under this comment is when you want to open the page on the current active page instead of opening a new tab.
    //GM_openInTab("https://www.youtube.com/results?search_query=" + selectedText, "active")
    }
  }

 // Register a button on the Tampermonkey menu.
GM_registerMenuCommand("Search on Youtube", () => {


    if (event.defaultPrevented ||
        /(input|textarea)/i.test(document.activeElement.nodeName)) {
        return;
      }
    searchYouTubeForSelectedText()

});

})();