Youtube Middle Click Search

Middle clicking the search on youtube opens the results in a new tab

目前為 2015-10-28 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name       Youtube Middle Click Search
// @version    1.3.6
// @description  Middle clicking the search on youtube opens the results in a new tab
// @match      *://www.youtube.com/*
// @require https://greasyfork.org/scripts/5679-wait-for-elements/code/Wait%20For%20Elements.js?version=22195
// @namespace https://greasyfork.org/users/649
// @grant GM_openInTab
// ==/UserScript==
console.log('started YMCS');
var process = function(element) {
  console.log('found search button');
  // setup references
  var oldButton = document.querySelector("#search-btn"),
      button = document.createElement('button'),
      input = document.querySelector('#masthead-search-term');
  // imitate old button style
  button.appendChild(oldButton.firstChild.cloneNode(true));
  button.firstChild.style.margin = '0 25px';
  button.style.padding = '0';
  button.className = oldButton.className;
  button.setAttribute('type', 'button');
  // insert new button and remove old
  oldButton.parentNode.insertBefore(button, oldButton);
  oldButton.remove();
  // bind events
  button.addEventListener('mousedown', function(e) {
    if(e.button === 1) {
      e.preventDefault();
    }
  }, false);
  document.addEventListener('click', function(e) {
    if(button.isEqualNode(e.target)) {
      e.preventDefault();
      if (input.value.trim() === '') return false;
      var url = '/results?search_query=' + encodeURIComponent(input.value);
      if(e.button === 1) {
        console.log('opening');
        GM_openInTab(url, false);
      }	else if(e.button === 0) {
        window.location.href = url;
      }
      return false;
    }
  });
};

waitForElems('#search-btn', process, true);