YouTube Selected-Text search.

Search the selected text on YouTube.

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 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()

});

})();