Youtube Middle Click Search

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

当前为 2016-09-04 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Youtube Middle Click Search
  3. // @namespace https://greasyfork.org/users/649
  4. // @version 1.4.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://greasyfork.org/scripts/5679-wait-for-elements/code/Wait%20For%20Elements.js?version=122976
  9. // @grant GM_openInTab
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. console.log('started YMCS');
  16. var processBtn = function(element) {
  17. console.log('found search button');
  18. // setup references
  19. var oldButton = document.querySelector('#search-btn'),
  20. button = document.createElement('button'),
  21. input = document.querySelector('#masthead-search-term'),
  22. initSearch = input.value.trim();
  23. // imitate old button style
  24. button.appendChild(oldButton.firstChild.cloneNode(true));
  25. button.firstChild.style.margin = '0 25px';
  26. button.style.padding = '0';
  27. button.className = oldButton.className;
  28. button.setAttribute('type', 'button');
  29. // insert new button and hide old (as opposed to remove, removing was conflicting with another script I use)
  30. oldButton.parentNode.insertBefore(button, oldButton.nextSibling);
  31. oldButton.style.display = 'none';
  32. // bind events
  33. button.onmousedown = function(e) {
  34. if (e.button === 1) {
  35. e.preventDefault();
  36. }
  37. };
  38. button.onclick = function(e) {
  39. e.preventDefault();
  40. e.stopImmediatePropagation();
  41. if (input.value.trim() === '' || input.value.trim() === initSearch && e.button !== 1) return false;
  42. var url = location.origin + '/results?search_query=' + encodeURIComponent(input.value);
  43. if (e.button === 1) {
  44. console.log('opening');
  45. GM_openInTab(url, true);
  46. } else if(e.button === 0) {
  47. window.location.href = url;
  48. }
  49. return false;
  50. };
  51. };
  52.  
  53. var processResults = function() {
  54. var elements = document.querySelectorAll('.gssb_e .gsq_a');
  55. [].forEach.call(elements, function(element) {
  56. if (element) {
  57. element.onmousedown = function(e) {
  58. if (e.button === 1) {
  59. e.preventDefault();
  60. }
  61. };
  62. element.onclick = function(e) {
  63. var url = location.origin + '/results?search_query=' + encodeURIComponent(element.querySelector('span').textContent);
  64. if (e.button === 1) {
  65. console.log('opening');
  66. GM_openInTab(url, true);
  67. } else if(e.button === 0) {
  68. window.location.href = url;
  69. }
  70. e.preventDefault();
  71. return false;
  72. };
  73. }
  74. });
  75. };
  76.  
  77. waitForElems('#search-btn', processBtn, true);
  78. waitForElems('.gssb_e', function(table) {
  79. var tick = new MutationObserver(processResults);
  80. tick.observe(table, { subtree: true, childList: true, attributes: true });
  81. });
  82. })();