Youtube Middle Click Search

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

当前为 2019-10-22 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Youtube Middle Click Search
  3. // @namespace https://greasyfork.org/users/649
  4. // @version 2.1.6
  5. // @description Middle clicking the search on youtube opens the results in a new tab
  6. // @author Adrien Pyke
  7. // @match *://www.youtube.com/*
  8. // @require https://gitcdn.link/repo/fuzetsu/userscripts/b38eabf72c20fa3cf7da84ecd2cefe0d4a2116be/wait-for-elements/wait-for-elements.js
  9. // @grant GM_openInTab
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. const SCRIPT_NAME = 'YMCS';
  16.  
  17. const Util = {
  18. log(...args) {
  19. args.unshift(`%c${SCRIPT_NAME}:`, 'font-weight: bold;color: #233c7b;');
  20. console.log(...args);
  21. },
  22. q(query, context = document) {
  23. return context.querySelector(query);
  24. },
  25. qq(query, context = document) {
  26. return Array.from(context.querySelectorAll(query));
  27. },
  28. getQueryParameter(name, url = window.location.href) {
  29. name = name.replace(/[[\]]/gu, '\\$&');
  30. const regex = new RegExp(`[?&]${name}(=([^&#]*)|&|#|$)`, 'u'),
  31. results = regex.exec(url);
  32. if (!results) return null;
  33. if (!results[2]) return '';
  34. return decodeURIComponent(results[2].replace(/\+/gu, ' '));
  35. },
  36. encodeURIWithPlus(string) {
  37. return encodeURIComponent(string).replace(/%20/gu, '+');
  38. }
  39. };
  40.  
  41. waitForElems({
  42. sel: '#search-icon-legacy',
  43. stop: true,
  44. onmatch(btn) {
  45. btn.onmousedown = function(e) {
  46. if (e.button === 1) {
  47. e.preventDefault();
  48. }
  49. };
  50. btn.onclick = function(e) {
  51. e.preventDefault();
  52. e.stopImmediatePropagation();
  53.  
  54. const input = Util.q('input#search').value.trim();
  55. if (!input) return false;
  56.  
  57. const url = `${location.origin}/results?search_query=${Util.encodeURIWithPlus(
  58. input
  59. )}`;
  60. if (e.button === 1) {
  61. GM_openInTab(url, true);
  62. } else if (e.button === 0) {
  63. window.location.href = url;
  64. }
  65.  
  66. return false;
  67. };
  68. btn.onauxclick = btn.onclick;
  69. }
  70. });
  71.  
  72. waitForElems({
  73. sel: '.sbsb_c',
  74. onmatch(result) {
  75. result.onclick = function(e) {
  76. if (!e.target.classList.contains('sbsb_i')) {
  77. const search = Util.q('.sbpqs_a, .sbqs_c', result).textContent;
  78.  
  79. const url = `${location.origin}/results?search_query=${Util.encodeURIWithPlus(
  80. search
  81. )}`;
  82. if (e.button === 1) {
  83. GM_openInTab(url, true);
  84. } else if (e.button === 0) {
  85. window.location.href = url;
  86. }
  87. } else if (e.button === 1) {
  88. // prevent opening in new tab if they middle click the remove button
  89. e.preventDefault();
  90. e.stopImmediatePropagation();
  91. }
  92. };
  93. result.onauxclick = result.onclick;
  94. }
  95. });
  96. })();