Google - Middle Click Search

Opens search results in new tab when you middle click

目前為 2018-03-17 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name Google - Middle Click Search
  3. // @namespace https://greasyfork.org/users/649
  4. // @version 1.1.2
  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.rawgit.com/fuzetsu/userscripts/477063e939b9658b64d2f91878da20a7f831d98b/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}=.*?(&|#|$)(.*)`, 'gi');
  18. const hasValue = (typeof value !== 'undefined' && value !== null && value !== '');
  19. if (regex.test(url)) {
  20. if (hasValue) {
  21. return url.replace(regex, `$1${key}=${value}$2$3`);
  22. } else {
  23. let [path, hash] = url.split('#');
  24. url = path.replace(regex, '$1$3').replace(/(&|\?)$/, '');
  25. if (hash) url += `#${hash[1]}`;
  26. return url;
  27. }
  28. } else if (hasValue) {
  29. let separator = url.includes('?') ? '&' : '?';
  30. let [path, hash] = url.split('#');
  31. url = `${path + separator + key}=${value}`;
  32. if (hash) url += `#${hash[1]}`;
  33. return url;
  34. } else return url;
  35. };
  36.  
  37. const getUrl = function(value) {
  38. if (window.location.href.match(/^https?:\/\/www\.google\.[a-zA-Z]+\/search\/?\?.*$/)) {
  39. return setQueryParam('q', encodeURIComponent(value));
  40. } else {
  41. return `${location.protocol}//${location.host}/search?q=${encodeURIComponent(value)}`;
  42. }
  43. };
  44.  
  45. waitForElems({
  46. sel: '#_fZl',
  47. onmatch(btn) {
  48. let input = document.querySelector('#lst-ib');
  49.  
  50. btn.onmousedown = e => {
  51. if (e.button === 1) {
  52. e.preventDefault();
  53. }
  54. };
  55.  
  56. btn.onclick = e => {
  57. if (e.button === 1 && input.value.trim()) {
  58. e.preventDefault();
  59. e.stopImmediatePropagation();
  60. let url = getUrl(input.value);
  61. GM_openInTab(url, true);
  62. return false;
  63. }
  64. };
  65.  
  66. btn.onauxclick = btn.onclick;
  67. }
  68. });
  69.  
  70. waitForElems({
  71. sel: '.sbsb_b li .sbqs_c, .sbsb_b li .sbpqs_d',
  72. onmatch(elem) {
  73. elem.onclick = e => {
  74. if (e.button === 1) {
  75. e.preventDefault();
  76. e.stopImmediatePropagation();
  77. let text = elem.classList.contains('sbpqs_d') ? elem.querySelector('span').textContent : elem.textContent;
  78. let url = getUrl(text);
  79. GM_openInTab(url, true);
  80. return false;
  81. }
  82. };
  83. elem.onauxclick = elem.onclick;
  84. }
  85. });
  86. })();