Google - Middle Click Search

Opens search results in new tab when you middle click

  1. // ==UserScript==
  2. // @name Google - Middle Click Search
  3. // @namespace https://greasyfork.org/users/649
  4. // @version 1.2.1
  5. // @description Opens search results in new tab when you middle click
  6. // @author Adrien Pyke
  7. // @include /^https?:\/\/www\.google\.[a-zA-Z]+\/?(?:\?.*)?$/
  8. // @include /^https?:\/\/www\.google\.[a-zA-Z]+\/search\/?\?.*$/
  9. // @require https://cdn.jsdelivr.net/gh/fuzetsu/userscripts@ec863aa92cea78a20431f92e80ac0e93262136df/wait-for-elements/wait-for-elements.js
  10. // @grant GM_openInTab
  11. // ==/UserScript==
  12.  
  13. (() => {
  14. 'use strict';
  15.  
  16. const setQueryParam = function (key, value, url = location.href) {
  17. const regex = new RegExp(`([?&])${key}=.*?(&|#|$)(.*)`, 'giu');
  18. const hasValue =
  19. typeof value !== 'undefined' && value !== null && value !== '';
  20. if (regex.test(url)) {
  21. if (hasValue) {
  22. return url.replace(regex, `$1${key}=${value}$2$3`);
  23. } else {
  24. const [path, hash] = url.split('#');
  25. url = path.replace(regex, '$1$3').replace(/(&|\?)$/u, '');
  26. if (hash) url += `#${hash[1]}`;
  27. return url;
  28. }
  29. } else if (hasValue) {
  30. const separator = url.includes('?') ? '&' : '?';
  31. const [path, hash] = url.split('#');
  32. url = `${path + separator + key}=${value}`;
  33. if (hash) url += `#${hash[1]}`;
  34. return url;
  35. } else return url;
  36. };
  37.  
  38. const getUrl = function (value) {
  39. if (
  40. window.location.href.match(
  41. /^https?:\/\/www\.google\.[a-zA-Z]+\/search\/?\?.*$/u
  42. )
  43. ) {
  44. return setQueryParam('q', encodeURIComponent(value));
  45. } else {
  46. return `${location.protocol}//${
  47. location.host
  48. }/search?q=${encodeURIComponent(value)}`;
  49. }
  50. };
  51.  
  52. waitForElems({
  53. sel: '#_fZl',
  54. onmatch(btn) {
  55. const input = document.querySelector('#lst-ib');
  56.  
  57. btn.onmousedown = e => {
  58. if (e.button === 1) {
  59. e.preventDefault();
  60. }
  61. };
  62.  
  63. btn.onclick = e => {
  64. if (e.button === 1 && input.value.trim()) {
  65. e.preventDefault();
  66. e.stopImmediatePropagation();
  67. const url = getUrl(input.value);
  68. GM_openInTab(url, true);
  69. return false;
  70. }
  71. };
  72.  
  73. btn.onauxclick = btn.onclick;
  74. }
  75. });
  76.  
  77. waitForElems({
  78. sel: '.sbsb_b li .sbqs_c, .sbsb_b li .sbpqs_d',
  79. onmatch(elem) {
  80. elem.onclick = e => {
  81. if (e.button === 1) {
  82. e.preventDefault();
  83. e.stopImmediatePropagation();
  84. const text = elem.classList.contains('sbpqs_d')
  85. ? elem.querySelector('span').textContent
  86. : elem.textContent;
  87. const url = getUrl(text);
  88. GM_openInTab(url, true);
  89. return false;
  90. }
  91. };
  92. elem.onauxclick = elem.onclick;
  93. }
  94. });
  95. })();