Youtube Middle Click Search

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

当前为 2018-02-19 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Youtube Middle Click Search
  3. // @namespace https://greasyfork.org/users/649
  4. // @version 2.0.1
  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://cdn.rawgit.com/fuzetsu/userscripts/477063e939b9658b64d2f91878da20a7f831d98b/wait-for-elements/wait-for-elements.js
  9. // @grant GM_openInTab
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. var SCRIPT_NAME = 'YMCS';
  16.  
  17. var Util = {
  18. log: function() {
  19. var args = [].slice.call(arguments);
  20. args.unshift('%c' + SCRIPT_NAME + ':', 'font-weight: bold;color: #233c7b;');
  21. console.log.apply(console, args);
  22. },
  23. q: function(query, context) {
  24. return (context || document).querySelector(query);
  25. },
  26. qq: function(query, context) {
  27. return [].slice.call((context || document).querySelectorAll(query));
  28. },
  29. getQueryParameter: function(name, url) {
  30. if (!url) url = window.location.href;
  31. name = name.replace(/[\[\]]/g, "\\$&");
  32. var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
  33. results = regex.exec(url);
  34. if (!results) return null;
  35. if (!results[2]) return '';
  36. return decodeURIComponent(results[2].replace(/\+/g, " "));
  37. },
  38. encodeURIWithPlus: function(string) {
  39. return encodeURIComponent(string).replace(/%20/g, '+');
  40. }
  41. };
  42.  
  43. waitForElems({
  44. sel: '#search-icon-legacy',
  45. stop: true,
  46. onmatch: function(btn) {
  47. btn.onmousedown = function(e) {
  48. if (e.button === 1) {
  49. e.preventDefault();
  50. }
  51. };
  52. btn.onclick = function(e) {
  53. e.preventDefault();
  54. e.stopImmediatePropagation();
  55.  
  56. var input = Util.q("input#search").value.trim();
  57. if (!input) return false;
  58.  
  59. var url = location.origin + '/results?search_query=' + Util.encodeURIWithPlus(input);
  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: function(result) {
  75. result.onclick = function(e) {
  76. if (!e.target.classList.contains('sbsb_i')) {
  77. var search = Util.q('.sbpqs_a, .sbqs_c', result).textContent;
  78.  
  79. var url = location.origin + '/results?search_query=' + Util.encodeURIWithPlus(search);
  80. if (e.button === 1) {
  81. GM_openInTab(url, true);
  82. } else if(e.button === 0) {
  83. window.location.href = url;
  84. }
  85. } else {
  86. if (e.button === 1) {
  87. // prevent opening in new tab if they middle click the remove button
  88. e.preventDefault();
  89. e.stopImmediatePropagation();
  90. }
  91. }
  92. };
  93. result.onauxclick = result.onclick;
  94. }
  95. });
  96. })();